MHamdan commited on
Commit
16eaebe
·
verified ·
1 Parent(s): 8e8a46c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -121
app.py CHANGED
@@ -1,129 +1,138 @@
1
  import gradio as gr
2
- import time
3
  import requests
4
- import os
5
-
6
- def read_file(file_obj):
7
- """Reads text from a .txt file only (no PDF/docx)."""
8
- if file_obj is None:
9
- return ""
10
- file_ext = os.path.splitext(file_obj.name)[1].lower()
11
- if file_ext != ".txt":
12
- return f"Unsupported file type: {file_ext}"
13
- try:
14
- return file_obj.read().decode("utf-8")
15
- except Exception as e:
16
- return f"Error reading file: {str(e)}"
17
-
18
- def fetch_url(url: str):
19
- """Fetch text from URL."""
20
- try:
21
- resp = requests.get(url, timeout=10)
22
- resp.raise_for_status()
23
- return resp.text[:1000] # just show first 1000 chars
24
- except Exception as e:
25
- return f"Error fetching URL: {str(e)}"
26
-
27
- def process_input(choice, text_val, url_val, file_val):
28
- """
29
- Minimal process function that:
30
- 1. Shows a progress bar for 4 steps (with time.sleep to visualize).
31
- 2. Reads content from the chosen input type.
32
- 3. Returns that content to the output.
33
- """
34
- with gr.Progress() as p:
35
- # STEP 1: "Reading input" placeholder
36
- p(0, total=4, desc="Reading input")
37
- time.sleep(1)
38
-
39
- # Actually read the content now
40
- if choice == "Text":
41
- content = text_val or "No text provided"
42
- elif choice == "URL":
43
- content = fetch_url(url_val or "")
44
- else: # "File"
45
- content = read_file(file_val)
46
-
47
- # STEP 2: Some dummy step
48
- p(1, total=4, desc="Doing something else")
49
- time.sleep(1)
50
-
51
- # STEP 3: Another dummy step
52
- p(2, total=4, desc="Almost done...")
 
 
 
 
 
53
  time.sleep(1)
54
-
55
- # STEP 4: Final step
56
- p(3, total=4, desc="Finalizing")
 
 
57
  time.sleep(1)
58
-
59
- # Return the content to show in the output
60
- return content
 
61
 
62
  def create_interface():
63
- with gr.Blocks(title="Minimal Progress Bar Demo") as demo:
64
- gr.Markdown("# Minimal Progress Bar Demo")
65
- gr.Markdown(
66
- "Select an input type, provide some data, then click **Analyze**. "
67
- "A progress bar will appear with four steps."
68
- )
69
-
70
- # 1) Dropdown to select input
71
- input_choice = gr.Dropdown(
72
- choices=["Text", "URL", "File"],
73
- value="Text",
74
- label="Select Input Type"
75
- )
76
-
77
- # 2) Containers for each input
78
- with gr.Column(visible=True) as text_col:
79
- text_input = gr.Textbox(
80
- label="Enter Text",
81
- placeholder="Paste text here...",
82
- lines=3
83
- )
84
- with gr.Column(visible=False) as url_col:
85
- url_input = gr.Textbox(
86
- label="Enter URL",
87
- placeholder="https://example.com"
88
- )
89
- with gr.Column(visible=False) as file_col:
90
- file_input = gr.File(
91
- label="Upload a .txt File Only",
92
- file_types=[".txt"]
93
- )
94
-
95
- # Toggle visibility function
96
- def show_inputs(choice):
97
- return {
98
- text_col: choice == "Text",
99
- url_col: choice == "URL",
100
- file_col: choice == "File"
101
- }
102
-
103
- input_choice.change(
104
- fn=show_inputs,
105
- inputs=[input_choice],
106
- outputs=[text_col, url_col, file_col]
107
- )
108
-
109
- analyze_btn = gr.Button("Analyze", variant="primary")
110
-
111
- # 3) Output
112
- output_box = gr.Textbox(
113
- label="Output",
114
- lines=6
115
- )
116
-
117
- # Link the button to the process function
118
- analyze_btn.click(
119
- fn=process_input,
120
- inputs=[input_choice, text_input, url_input, file_input],
121
- outputs=[output_box],
122
- show_progress=True
123
- )
124
-
125
- return demo
 
 
126
 
127
  if __name__ == "__main__":
128
- demo = create_interface()
129
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  import requests
4
+ from bs4 import BeautifulSoup
5
+ import PyPDF2
6
+ import docx
7
+ import time
8
+ from smolagents.agents import HuggingFaceAgent
9
+
10
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
11
+ sentiment_analyzer = pipeline("sentiment-analysis")
12
+ topic_classifier = pipeline("zero-shot-classification")
13
+
14
+ def fetch_text_from_url(url):
15
+ response = requests.get(url)
16
+ soup = BeautifulSoup(response.text, "html.parser")
17
+ return " ".join(p.get_text() for p in soup.find_all("p"))
18
+
19
+ def extract_text_from_pdf(file):
20
+ pdf_reader = PyPDF2.PdfReader(file)
21
+ text = ""
22
+ for page in pdf_reader.pages:
23
+ text += page.extract_text()
24
+ return text
25
+
26
+ def extract_text_from_docx(file):
27
+ doc = docx.Document(file)
28
+ text = ""
29
+ for para in doc.paragraphs:
30
+ text += para.text + "\n"
31
+ return text
32
+
33
+ def analyze_text(input_text, input_type, tasks, progress=gr.Progress()):
34
+ if input_type == "URL":
35
+ progress(0, desc="Fetching text from URL")
36
+ input_text = fetch_text_from_url(input_text)
37
+ elif input_type == "File":
38
+ progress(0, desc="Extracting text from file")
39
+ if input_text.name.lower().endswith(".pdf"):
40
+ input_text = extract_text_from_pdf(input_text)
41
+ elif input_text.name.lower().endswith(".docx"):
42
+ input_text = extract_text_from_docx(input_text)
43
+ else:
44
+ input_text = input_text.read().decode("utf-8")
45
+
46
+ original_text = input_text[:1000] + ("..." if len(input_text) > 1000 else "")
47
+
48
+ summary, sentiment, topics = "", "", ""
49
+
50
+ if "Summarization" in tasks:
51
+ progress(0.3, desc="Generating summary")
52
+ summary = summarizer(input_text, max_length=100, min_length=30, do_sample=False)[0]["summary_text"]
53
+ time.sleep(1) # Add a minimal delay for demonstration purposes
54
+
55
+ if "Sentiment Analysis" in tasks:
56
+ progress(0.6, desc="Analyzing sentiment")
57
+ sentiment = sentiment_analyzer(input_text[:512])[0]["label"] # Truncate input for sentiment analysis
58
  time.sleep(1)
59
+
60
+ if "Topic Detection" in tasks:
61
+ progress(0.9, desc="Detecting topics")
62
+ topic_labels = ["technology", "politics", "sports", "entertainment", "business"]
63
+ topics = topic_classifier(input_text[:512], topic_labels, multi_label=True)["labels"] # Truncate input for topic detection
64
  time.sleep(1)
65
+
66
+ progress(1, desc="Analysis completed")
67
+
68
+ return original_text, summary, sentiment, ", ".join(topics)
69
 
70
  def create_interface():
71
+ input_type = gr.inputs.Dropdown(["Text", "URL", "File"], label="Input Type")
72
+ text_input = gr.Textbox(visible=False)
73
+ url_input = gr.Textbox(visible=False)
74
+ file_input = gr.File(visible=False)
75
+
76
+ tasks_checkboxes = gr.CheckboxGroup(["Summarization", "Sentiment Analysis", "Topic Detection"], label="Analysis Tasks")
77
+
78
+ submit_button = gr.Button("Analyze")
79
+ progress_bar = gr.Progress()
80
+
81
+ model_endpoint = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
82
+ agent = HuggingFaceAgent(model_endpoint=model_endpoint)
83
+
84
+ def update_input_visibility(input_type):
85
+ return {
86
+ text_input: gr.update(visible=input_type == "Text"),
87
+ url_input: gr.update(visible=input_type == "URL"),
88
+ file_input: gr.update(visible=input_type == "File"),
89
+ }
90
+
91
+ input_type.change(update_input_visibility, [input_type], [text_input, url_input, file_input])
92
+
93
+ original_text_output = gr.Textbox(label="Original Text")
94
+ summary_output = gr.Textbox(label="Summary")
95
+ sentiment_output = gr.Textbox(label="Sentiment")
96
+ topics_output = gr.Textbox(label="Topics")
97
+
98
+ def process_input(input_type, text, url, file, tasks):
99
+ if input_type == "Text":
100
+ input_value = text
101
+ elif input_type == "URL":
102
+ input_value = url
103
+ else:
104
+ input_value = file
105
+
106
+ try:
107
+ original_text, summary, sentiment, topics = analyze_text(input_value, input_type, tasks, progress_bar)
108
+ enhanced_summary = agent.run(f"Given the following text: '{original_text}', please suggest improvements to this summary: '{summary}'")
109
+ enhanced_sentiment = agent.run(f"Given the following text: '{original_text}', does this sentiment seem accurate: '{sentiment}'? Please elaborate and suggest any corrections.")
110
+ except Exception as e:
111
+ original_text = f"Error: {str(e)}"
112
+ summary, sentiment, topics = "", "", ""
113
+ enhanced_summary = ""
114
+ enhanced_sentiment = ""
115
+
116
+ return original_text, summary, enhanced_summary, sentiment, enhanced_sentiment, topics
117
+
118
+ submit_button.click(
119
+ fn=process_input,
120
+ inputs=[input_type, text_input, url_input, file_input, tasks_checkboxes],
121
+ outputs=[original_text_output, summary_output, summary_output, sentiment_output, sentiment_output, topics_output]
122
+ )
123
+
124
+ interface = gr.TabbedInterface([
125
+ gr.Tab(original_text_output, label="Original Text"),
126
+ gr.Tab(summary_output, label="Summary"),
127
+ gr.Tab(sentiment_output, label="Sentiment"),
128
+ gr.Tab(topics_output, label="Topics")
129
+ ])
130
+
131
+ return gr.Blocks(
132
+ title="Text Analysis App",
133
+ inputs=[input_type, text_input, url_input, file_input, tasks_checkboxes, submit_button],
134
+ outputs=[interface, progress_bar]
135
+ )
136
 
137
  if __name__ == "__main__":
138
+ create_interface().launch()