waqasali1707's picture
Update app.py
5dc438c verified
raw
history blame
1.32 kB
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GenerationConfig
# Define the URL of your model on Hugging Face Spaces
model_url = 'https://huggingface.co/your-username/your-model-name/resolve/main/'
# Load the model and tokenizer directly from the URL
try:
tokenizer = AutoTokenizer.from_pretrained(model_url)
model = AutoModelForSeq2SeqLM.from_pretrained(model_url)
except Exception as e:
st.error(f"Failed to load model: {e}")
# Streamlit UI
st.title("Text Summarizer")
text = st.text_area("Enter the text to generate its Summary:")
# Configuration for generation
generation_config = GenerationConfig(max_new_tokens=100, do_sample=True, temperature=0.7)
if text:
try:
# Encode input
inputs_encoded = tokenizer(text, return_tensors='pt')
# Generate output
with torch.no_grad():
model_output = model.generate(inputs_encoded["input_ids"], generation_config=generation_config)[0]
# Decode output
output = tokenizer.decode(model_output, skip_special_tokens=True)
# Display results in a box with a title
with st.expander("Output", expanded=True):
st.write(output)
except Exception as e:
st.error(f"An error occurred during summarization: {e}")