Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from googletrans import Translator
|
| 5 |
+
from rouge_score import rouge_scorer
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# -------------------------------
|
| 9 |
+
# Page Config
|
| 10 |
+
# -------------------------------
|
| 11 |
+
st.set_page_config(page_title="Multilingual Summarization Dashboard", layout="wide")
|
| 12 |
+
|
| 13 |
+
# -------------------------------
|
| 14 |
+
# Style
|
| 15 |
+
# -------------------------------
|
| 16 |
+
with open("style.css") as f:
|
| 17 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 18 |
+
|
| 19 |
+
# -------------------------------
|
| 20 |
+
# Model Loading
|
| 21 |
+
# -------------------------------
|
| 22 |
+
@st.cache_resource(show_spinner=True)
|
| 23 |
+
def load_models():
|
| 24 |
+
models = {}
|
| 25 |
+
models['urT5-base'] = pipeline(
|
| 26 |
+
"summarization",
|
| 27 |
+
model="mbshr/urt5-base-finetuned",
|
| 28 |
+
device=0 if torch.cuda.is_available() else -1
|
| 29 |
+
)
|
| 30 |
+
models['mT5-small'] = pipeline(
|
| 31 |
+
"summarization",
|
| 32 |
+
model="google/mt5-small",
|
| 33 |
+
device=0 if torch.cuda.is_available() else -1
|
| 34 |
+
)
|
| 35 |
+
models['mT5-base'] = pipeline(
|
| 36 |
+
"summarization",
|
| 37 |
+
model="google/mt5-base",
|
| 38 |
+
device=0 if torch.cuda.is_available() else -1
|
| 39 |
+
)
|
| 40 |
+
return models
|
| 41 |
+
|
| 42 |
+
models = load_models()
|
| 43 |
+
translator = Translator()
|
| 44 |
+
scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True)
|
| 45 |
+
|
| 46 |
+
# -------------------------------
|
| 47 |
+
# Sidebar
|
| 48 |
+
# -------------------------------
|
| 49 |
+
st.sidebar.title("Settings")
|
| 50 |
+
selected_model = st.sidebar.selectbox("Choose Summarization Model", list(models.keys()))
|
| 51 |
+
max_length = st.sidebar.slider("Max summary length", 50, 500, 150)
|
| 52 |
+
min_length = st.sidebar.slider("Min summary length", 10, 300, 40)
|
| 53 |
+
target_lang = st.sidebar.selectbox("Translate summary to", ["None", "English", "Urdu", "French", "Spanish"])
|
| 54 |
+
show_comparison = st.sidebar.checkbox("Compare models")
|
| 55 |
+
show_rouge = st.sidebar.checkbox("Show ROUGE Score (requires reference)")
|
| 56 |
+
|
| 57 |
+
# -------------------------------
|
| 58 |
+
# Main Interface
|
| 59 |
+
# -------------------------------
|
| 60 |
+
st.title("🌐 Multilingual Summarization Dashboard")
|
| 61 |
+
st.write("Enter text to summarize, optionally translate, compare models, and evaluate with ROUGE.")
|
| 62 |
+
|
| 63 |
+
text = st.text_area("Enter text to summarize:", height=200)
|
| 64 |
+
reference_text = ""
|
| 65 |
+
if show_rouge:
|
| 66 |
+
reference_text = st.text_area("Reference summary for ROUGE evaluation:", height=100)
|
| 67 |
+
|
| 68 |
+
# -------------------------------
|
| 69 |
+
# Generate Summary
|
| 70 |
+
# -------------------------------
|
| 71 |
+
if st.button("Generate Summary"):
|
| 72 |
+
if not text.strip():
|
| 73 |
+
st.error("Please enter some text!")
|
| 74 |
+
else:
|
| 75 |
+
# Handle long texts with chunking
|
| 76 |
+
chunk_size = 500 # characters
|
| 77 |
+
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 78 |
+
full_summary = ""
|
| 79 |
+
for chunk in chunks:
|
| 80 |
+
summ = models[selected_model](chunk, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text']
|
| 81 |
+
full_summary += summ + " "
|
| 82 |
+
|
| 83 |
+
st.subheader(f"Summary ({selected_model}):")
|
| 84 |
+
st.write(full_summary)
|
| 85 |
+
|
| 86 |
+
# Translation
|
| 87 |
+
if target_lang != "None":
|
| 88 |
+
lang_code = target_lang[:2].lower()
|
| 89 |
+
try:
|
| 90 |
+
translation = translator.translate(full_summary, dest=lang_code).text
|
| 91 |
+
st.subheader(f"Summary in {target_lang}:")
|
| 92 |
+
st.write(translation)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.warning(f"Translation failed: {str(e)}")
|
| 95 |
+
|
| 96 |
+
# Model comparison
|
| 97 |
+
if show_comparison:
|
| 98 |
+
st.subheader("Comparison with other models:")
|
| 99 |
+
for model_name, model_pipeline in models.items():
|
| 100 |
+
if model_name != selected_model:
|
| 101 |
+
comp_summary = ""
|
| 102 |
+
for chunk in chunks:
|
| 103 |
+
comp_summary += model_pipeline(chunk, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text'] + " "
|
| 104 |
+
st.markdown(f"**{model_name} Summary:** {comp_summary}")
|
| 105 |
+
|
| 106 |
+
# ROUGE Evaluation
|
| 107 |
+
if show_rouge and reference_text.strip():
|
| 108 |
+
scores = scorer.score(reference_text, full_summary)
|
| 109 |
+
st.subheader("ROUGE Scores:")
|
| 110 |
+
for k, v in scores.items():
|
| 111 |
+
st.write(f"{k}: Precision: {v.precision:.3f}, Recall: {v.recall:.3f}, F1: {v.fmeasure:.3f}")
|