MHamdan commited on
Commit
2af5feb
·
verified ·
1 Parent(s): 18d6761

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -44
app.py CHANGED
@@ -153,7 +153,6 @@ class ContentAnalyzer:
153
  print("[DEBUG]", error_msg)
154
  return {"error": error_msg}
155
 
156
-
157
  def create_interface():
158
  analyzer = ContentAnalyzer()
159
 
@@ -162,29 +161,11 @@ def create_interface():
162
  gr.Markdown("Analyze text content from various sources using AI.")
163
 
164
  with gr.Tabs():
165
- # Text Input Tab
166
- with gr.Tab("Text Input"):
167
- text_input = gr.Textbox(
168
- label="Enter Text",
169
- placeholder="Paste your text here...",
170
- lines=5
171
- )
172
-
173
- # URL Input Tab
174
- with gr.Tab("Web URL"):
175
- url_input = gr.Textbox(
176
- label="Enter URL",
177
- placeholder="https://example.com"
178
- )
179
 
180
- # File Upload Tab
181
- with gr.Tab("File Upload"):
182
- file_input = gr.File(
183
- label="Upload File",
184
- file_types=[".txt", ".pdf", ".docx"]
185
- )
186
-
187
- # Analysis Options
188
  analysis_types = gr.CheckboxGroup(
189
  choices=["summarize", "sentiment", "topics"],
190
  value=["summarize"],
@@ -193,7 +174,6 @@ def create_interface():
193
 
194
  analyze_btn = gr.Button("Analyze", variant="primary")
195
 
196
- # Output Sections
197
  with gr.Tabs():
198
  with gr.Tab("Original Text"):
199
  original_text = gr.Markdown()
@@ -205,15 +185,17 @@ def create_interface():
205
  topics_output = gr.Markdown()
206
 
207
  def process_analysis(text, url, file, types, progress=gr.Progress()):
208
- """
209
- This function is wrapped by gradio to handle user inputs.
210
- We use progress to show step-by-step updates.
211
- """
212
- steps_total = 4 # We have up to 4 possible steps
213
-
214
- def progress_callback(step, desc):
215
- progress((step, desc), total=steps_total)
216
-
 
 
217
  results = analyzer.analyze_content(
218
  text=text,
219
  url=url,
@@ -222,26 +204,21 @@ def create_interface():
222
  progress_callback=progress_callback
223
  )
224
 
225
- # If there's an error, show it in "Original Text" tab for clarity
226
  if "error" in results:
227
  return results["error"], "", "", ""
228
 
229
  # Format outputs
230
  original = results.get("original_text", "")
231
  summary = results.get("summary", "")
232
-
233
  sentiment = ""
234
  if "sentiment" in results:
235
- sent = results["sentiment"]
236
- sentiment = f"**Sentiment:** {sent['label']} (Confidence: {sent['score']})"
237
 
238
  topics = ""
239
  if "topics" in results:
240
- topics_list = "\n".join([
241
- f"- {t['label']}: {t['score']}"
242
- for t in results["topics"]
243
- ])
244
- topics = "**Detected Topics:**\n" + topics_list
245
 
246
  return original, summary, sentiment, topics
247
 
@@ -249,11 +226,11 @@ def create_interface():
249
  fn=process_analysis,
250
  inputs=[text_input, url_input, file_input, analysis_types],
251
  outputs=[original_text, summary_output, sentiment_output, topics_output],
252
- show_progress=True # Enable the progress bar in Gradio
253
  )
254
 
255
  return demo
256
 
257
  if __name__ == "__main__":
258
  demo = create_interface()
259
- demo.launch()
 
153
  print("[DEBUG]", error_msg)
154
  return {"error": error_msg}
155
 
 
156
  def create_interface():
157
  analyzer = ContentAnalyzer()
158
 
 
161
  gr.Markdown("Analyze text content from various sources using AI.")
162
 
163
  with gr.Tabs():
164
+ # Tabs for Text Input, Web URL, File Upload...
165
+ text_input = gr.Textbox(label="Enter Text", placeholder="Paste your text here...", lines=5)
166
+ url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com")
167
+ file_input = gr.File(label="Upload File", file_types=[".txt", ".pdf", ".docx"])
 
 
 
 
 
 
 
 
 
 
168
 
 
 
 
 
 
 
 
 
169
  analysis_types = gr.CheckboxGroup(
170
  choices=["summarize", "sentiment", "topics"],
171
  value=["summarize"],
 
174
 
175
  analyze_btn = gr.Button("Analyze", variant="primary")
176
 
 
177
  with gr.Tabs():
178
  with gr.Tab("Original Text"):
179
  original_text = gr.Markdown()
 
185
  topics_output = gr.Markdown()
186
 
187
  def process_analysis(text, url, file, types, progress=gr.Progress()):
188
+ steps_total = 4
189
+
190
+ def progress_callback(step: int, desc: str):
191
+ """
192
+ step: integer step index (1 to steps_total)
193
+ desc: a short description of the current step
194
+ """
195
+ # Pass the integer 'step' as iteration, and the string 'desc' as desc.
196
+ progress(step, total=steps_total, desc=desc)
197
+
198
+ # Call your analyzer
199
  results = analyzer.analyze_content(
200
  text=text,
201
  url=url,
 
204
  progress_callback=progress_callback
205
  )
206
 
 
207
  if "error" in results:
208
  return results["error"], "", "", ""
209
 
210
  # Format outputs
211
  original = results.get("original_text", "")
212
  summary = results.get("summary", "")
 
213
  sentiment = ""
214
  if "sentiment" in results:
215
+ s = results["sentiment"]
216
+ sentiment = f"**Sentiment:** {s['label']} (Confidence: {s['score']})"
217
 
218
  topics = ""
219
  if "topics" in results:
220
+ t_list = "\n".join([f"- {t['label']}: {t['score']}" for t in results["topics"]])
221
+ topics = "**Detected Topics:**\n" + t_list
 
 
 
222
 
223
  return original, summary, sentiment, topics
224
 
 
226
  fn=process_analysis,
227
  inputs=[text_input, url_input, file_input, analysis_types],
228
  outputs=[original_text, summary_output, sentiment_output, topics_output],
229
+ show_progress=True
230
  )
231
 
232
  return demo
233
 
234
  if __name__ == "__main__":
235
  demo = create_interface()
236
+ demo.launch()