Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load Hugging Face tokenizer and model for re-punctuation
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_re_punctuate_model():
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("SJ-Ray/Re-Punctuate")
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("SJ-Ray/Re-Punctuate")
|
| 10 |
+
return tokenizer, model
|
| 11 |
+
|
| 12 |
+
# Load Hugging Face tokenizer and model for headline generation
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_headline_model():
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("Michau/t5-base-en-generate-headline")
|
| 16 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Michau/t5-base-en-generate-headline")
|
| 17 |
+
return tokenizer, model
|
| 18 |
+
|
| 19 |
+
# Function to re-punctuate text
|
| 20 |
+
def re_punctuate_text(tokenizer, model, text):
|
| 21 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
| 22 |
+
outputs = model.generate(inputs["input_ids"], max_length=512, num_beams=4, early_stopping=True)
|
| 23 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
+
|
| 25 |
+
# Function to generate a headline
|
| 26 |
+
def generate_headline_text(tokenizer, model, text, max_length=50):
|
| 27 |
+
inputs = tokenizer(f"headline: {text}", return_tensors="pt", truncation=True, padding=True)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model.generate(
|
| 30 |
+
**inputs,
|
| 31 |
+
max_length=max_length,
|
| 32 |
+
num_beams=5,
|
| 33 |
+
no_repeat_ngram_size=2,
|
| 34 |
+
early_stopping=True
|
| 35 |
+
)
|
| 36 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
# Streamlit app layout
|
| 39 |
+
st.title("Model Selection: Re-Punctuate or Generate Headline")
|
| 40 |
+
|
| 41 |
+
# Model selection dropdown
|
| 42 |
+
model_options = ["Re-Punctuate Text", "Generate Headline"]
|
| 43 |
+
selected_model = st.selectbox("Choose a model to use:", model_options)
|
| 44 |
+
|
| 45 |
+
# User input text
|
| 46 |
+
input_text = st.text_area("Enter text:", placeholder="Type your input here...")
|
| 47 |
+
|
| 48 |
+
# Button to process text based on the selected model
|
| 49 |
+
if st.button("Process Text") and input_text:
|
| 50 |
+
with st.spinner("Processing..."):
|
| 51 |
+
if selected_model == "Re-Punctuate Text":
|
| 52 |
+
tokenizer, model = load_re_punctuate_model()
|
| 53 |
+
result = re_punctuate_text(tokenizer, model, input_text)
|
| 54 |
+
else: # Generate Headline
|
| 55 |
+
tokenizer, model = load_headline_model()
|
| 56 |
+
result = generate_headline_text(tokenizer, model, input_text)
|
| 57 |
+
|
| 58 |
+
# Display result
|
| 59 |
+
st.subheader(f"Result from {selected_model}:")
|
| 60 |
+
st.write(result)
|
| 61 |
+
|
| 62 |
+
# Footer
|
| 63 |
+
st.write("---")
|
| 64 |
+
st.write("Powered by Hugging Face Models.")
|