Subayyal commited on
Commit
544ba03
·
verified ·
1 Parent(s): f58a09d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -24
app.py CHANGED
@@ -1,13 +1,9 @@
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
  # -------------------------------
@@ -17,47 +13,74 @@ 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)
@@ -69,11 +92,13 @@ if show_rouge:
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:
@@ -85,26 +110,29 @@ if st.button("Generate 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():
 
1
  # app.py
2
  import streamlit as st
3
  from transformers import pipeline
 
4
  from rouge_score import rouge_scorer
5
  import torch
6
 
 
 
 
7
  st.set_page_config(page_title="Multilingual Summarization Dashboard", layout="wide")
8
 
9
  # -------------------------------
 
13
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
14
 
15
  # -------------------------------
16
+ # Hugging Face API Token
17
+ # -------------------------------
18
+ st.sidebar.title("Hugging Face API Token")
19
+ api_token = st.sidebar.text_input(
20
+ "Enter your Hugging Face API token:",
21
+ type="password",
22
+ help="Get your token from https://huggingface.co/settings/tokens"
23
+ )
24
+
25
+ if not api_token:
26
+ st.warning("Please enter your Hugging Face API token to enable model inference.")
27
+
28
+ # -------------------------------
29
+ # Model Initialization
30
  # -------------------------------
31
  @st.cache_resource(show_spinner=True)
32
+ def load_models(token):
33
  models = {}
34
  models['urT5-base'] = pipeline(
35
  "summarization",
36
  model="mbshr/urt5-base-finetuned",
37
+ device=0 if torch.cuda.is_available() else -1,
38
+ use_auth_token=token
39
  )
40
  models['mT5-small'] = pipeline(
41
  "summarization",
42
  model="google/mt5-small",
43
+ device=0 if torch.cuda.is_available() else -1,
44
+ use_auth_token=token
45
  )
46
  models['mT5-base'] = pipeline(
47
  "summarization",
48
  model="google/mt5-base",
49
+ device=0 if torch.cuda.is_available() else -1,
50
+ use_auth_token=token
51
+ )
52
+ # Translation models
53
+ models['en→ur'] = pipeline(
54
+ "translation",
55
+ model="Helsinki-NLP/opus-mt-en-ur",
56
+ device=0 if torch.cuda.is_available() else -1,
57
+ use_auth_token=token
58
+ )
59
+ models['ur→en'] = pipeline(
60
+ "translation",
61
+ model="Helsinki-NLP/opus-mt-ur-en",
62
+ device=0 if torch.cuda.is_available() else -1,
63
+ use_auth_token=token
64
  )
65
  return models
66
 
67
+ models = load_models(api_token) if api_token else {}
 
 
68
 
69
  # -------------------------------
70
+ # Sidebar Settings
71
  # -------------------------------
72
  st.sidebar.title("Settings")
73
+ selected_model = st.sidebar.selectbox("Choose Summarization Model", ["urT5-base", "mT5-small", "mT5-base"])
74
  max_length = st.sidebar.slider("Max summary length", 50, 500, 150)
75
  min_length = st.sidebar.slider("Min summary length", 10, 300, 40)
76
+ target_lang = st.sidebar.selectbox("Translate summary to", ["None", "English", "Urdu"])
77
  show_comparison = st.sidebar.checkbox("Compare models")
78
  show_rouge = st.sidebar.checkbox("Show ROUGE Score (requires reference)")
79
 
80
  # -------------------------------
81
  # Main Interface
82
  # -------------------------------
83
+ st.title("🌐 Multilingual Summarization Dashboard (API Version)")
84
  st.write("Enter text to summarize, optionally translate, compare models, and evaluate with ROUGE.")
85
 
86
  text = st.text_area("Enter text to summarize:", height=200)
 
92
  # Generate Summary
93
  # -------------------------------
94
  if st.button("Generate Summary"):
95
+ if not api_token:
96
+ st.error("Please provide Hugging Face API token.")
97
+ elif not text.strip():
98
  st.error("Please enter some text!")
99
  else:
100
+ # Chunking
101
+ chunk_size = 500
102
  chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
103
  full_summary = ""
104
  for chunk in chunks:
 
110
 
111
  # Translation
112
  if target_lang != "None":
 
113
  try:
114
+ if target_lang == "English":
115
+ translated = models['ur→en'](full_summary)[0]['translation_text']
116
+ else: # Urdu
117
+ translated = models['en→ur'](full_summary)[0]['translation_text']
118
  st.subheader(f"Summary in {target_lang}:")
119
+ st.write(translated)
120
  except Exception as e:
121
  st.warning(f"Translation failed: {str(e)}")
122
 
123
  # Model comparison
124
  if show_comparison:
125
  st.subheader("Comparison with other models:")
126
+ for model_name in ["urT5-base", "mT5-small", "mT5-base"]:
127
  if model_name != selected_model:
128
  comp_summary = ""
129
  for chunk in chunks:
130
+ comp_summary += models[model_name](chunk, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text'] + " "
131
  st.markdown(f"**{model_name} Summary:** {comp_summary}")
132
 
133
  # ROUGE Evaluation
134
  if show_rouge and reference_text.strip():
135
+ scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True)
136
  scores = scorer.score(reference_text, full_summary)
137
  st.subheader("ROUGE Scores:")
138
  for k, v in scores.items():