Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,129 +1,138 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import requests
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
""
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
53 |
time.sleep(1)
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
time.sleep(1)
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
def create_interface():
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
}
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
-
|
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()
|
|