Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,259 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
#
|
4 |
-
|
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 |
-
clear_btn = gr.Button("Clear")
|
30 |
-
|
31 |
-
with gr.Column():
|
32 |
-
output = gr.JSON(label="Sentiment Analysis Results")
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
if not text:
|
37 |
-
return "{\"error\": \"Please enter some text to analyze\"}"
|
38 |
-
# The tool returns a JSON string now
|
39 |
-
json_str = sentiment_tool.forward(text, model_key)
|
40 |
-
# But we need to parse it for the Gradio JSON component
|
41 |
-
import json
|
42 |
-
try:
|
43 |
-
return json.loads(json_str)
|
44 |
-
except:
|
45 |
-
return {"error": "Failed to parse results"}
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
inputs=None,
|
56 |
-
outputs=[text_input, output]
|
57 |
-
)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import warnings
|
4 |
+
from smolagents import CodeAgent, InferenceClientModel
|
5 |
+
import json
|
6 |
|
7 |
+
# Suppress unnecessary warnings
|
8 |
+
warnings.filterwarnings("ignore")
|
9 |
|
10 |
+
# Import our Text Summarizer Tool
|
11 |
+
from summarizer_tool import TextSummarizerTool
|
12 |
+
|
13 |
+
# Initialize the Text Summarizer Tool
|
14 |
+
summarizer_tool = TextSummarizerTool()
|
15 |
+
|
16 |
+
# Load HuggingFace token from environment variable if available
|
17 |
+
hf_token = os.environ.get("HF_TOKEN")
|
18 |
+
|
19 |
+
# Sample texts for quick testing
|
20 |
+
sample_texts = {
|
21 |
+
"News Article": """
|
22 |
+
The European Union has approved a landmark artificial intelligence law, establishing comprehensive
|
23 |
+
regulations for AI systems according to their potential risks. The regulation categorizes AI into
|
24 |
+
four risk levels: unacceptable risk, high risk, limited risk, and minimal risk. Systems deemed to
|
25 |
+
pose unacceptable risks, such as those using subliminal manipulation or social scoring, will be
|
26 |
+
banned. High-risk systems, including those used in critical infrastructure, education, employment,
|
27 |
+
and law enforcement, will face strict requirements before market entry. These requirements include
|
28 |
+
risk assessments, high-quality datasets, detailed documentation, human oversight, and transparency.
|
29 |
+
The law aims to ensure AI systems are safe, transparent, traceable, non-discriminatory, and
|
30 |
+
environmentally friendly, while fostering innovation and establishing Europe as a leader in
|
31 |
+
responsible AI development.
|
32 |
+
""",
|
33 |
|
34 |
+
"Scientific Paper Abstract": """
|
35 |
+
Recent advancements in large language models (LLMs) have demonstrated remarkable capabilities
|
36 |
+
across various tasks. However, these models still face challenges with reasoning, factuality,
|
37 |
+
and potential biases. This paper introduces a novel framework for enhancing LLM performance
|
38 |
+
through a multi-stage processing pipeline that integrates retrieval-augmented generation,
|
39 |
+
self-reflection mechanisms, and external knowledge verification. Our approach, which we call
|
40 |
+
RACER (Retrieval-Augmented Chain-of-thought Enhanced Reasoning), demonstrates significant
|
41 |
+
improvements across benchmarks testing reasoning (GSM8K, +12.3%), factuality (FACTOR, +17.8%),
|
42 |
+
and reduced bias (BBQ, -24.5%) compared to base models. Additionally, we show that RACER is
|
43 |
+
complementary to existing techniques like chain-of-thought prompting and can be applied to
|
44 |
+
various model architectures with minimal computational overhead. Through extensive ablation
|
45 |
+
studies, we identify the contribution of each component and provide insights for efficient
|
46 |
+
implementation in real-world applications.
|
47 |
+
""",
|
48 |
+
|
49 |
+
"Business Report": """
|
50 |
+
Q1 Financial Performance Summary: The company achieved significant growth in the first quarter,
|
51 |
+
with revenue reaching $78.5 million, a 24% increase compared to the same period last year. This
|
52 |
+
growth was primarily driven by our expanded product portfolio and increased market penetration in
|
53 |
+
European and Asian markets. Our flagship product line saw sales increase by 32%, while our new
|
54 |
+
service offerings contributed $12.3 million in revenue. Gross margin improved to 62.8% from 58.4%
|
55 |
+
in the previous year, reflecting our successful cost optimization initiatives and economies of scale.
|
56 |
+
Operating expenses were $28.7 million, up 15% year-over-year, primarily due to increased R&D
|
57 |
+
investments and marketing campaigns for new product launches. Despite these investments, operating
|
58 |
+
profit grew by 42% to $20.5 million, representing a 26.1% operating margin. Our customer base expanded
|
59 |
+
by 15%, with particularly strong growth in the enterprise segment. Looking ahead, we maintain our
|
60 |
+
full-year guidance of $320-340 million in revenue and anticipate continued margin improvement as we
|
61 |
+
scale operations.
|
62 |
+
"""
|
63 |
+
}
|
64 |
+
|
65 |
+
# Function to directly use the summarizer tool
|
66 |
+
def summarize_text(text, model, max_length, min_length, style):
|
67 |
+
try:
|
68 |
+
# Convert max_length and min_length to integers
|
69 |
+
max_length = int(max_length) if max_length else None
|
70 |
+
min_length = int(min_length) if min_length else None
|
71 |
|
72 |
+
# Call the summarizer tool
|
73 |
+
result = summarizer_tool(
|
74 |
+
text=text,
|
75 |
+
model=model,
|
76 |
+
max_length=max_length,
|
77 |
+
min_length=min_length,
|
78 |
+
style=style
|
79 |
+
)
|
80 |
+
return result
|
81 |
+
except Exception as e:
|
82 |
+
return f"Error summarizing text: {str(e)}"
|
83 |
+
|
84 |
+
# Function to use the summarizer with an agent (if token available)
|
85 |
+
def agent_summarize(text, instruction, temperature=0.7):
|
86 |
+
if not hf_token:
|
87 |
+
return "Agent summarization requires a HuggingFace API token. Please set the HF_TOKEN environment variable."
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
if not text or not instruction:
|
90 |
+
return "Please provide both text and instructions."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
try:
|
93 |
+
# Initialize model for agent
|
94 |
+
model = InferenceClientModel(
|
95 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.2",
|
96 |
+
token=hf_token,
|
97 |
+
temperature=float(temperature)
|
98 |
)
|
99 |
|
100 |
+
# Create the agent with our summarizer tool
|
101 |
+
agent = CodeAgent(tools=[summarizer_tool], model=model)
|
|
|
|
|
|
|
102 |
|
103 |
+
# Format the prompt with the instruction and text
|
104 |
+
prompt = f"Instruction: {instruction}\n\nText to summarize: {text}"
|
105 |
+
|
106 |
+
# Run the agent
|
107 |
+
result = agent.run(prompt)
|
108 |
+
return result
|
109 |
+
except Exception as e:
|
110 |
+
return f"Error with agent summarization: {str(e)}"
|
111 |
+
|
112 |
+
# Create Gradio interface
|
113 |
+
with gr.Blocks(title="Advanced Text Summarizer") as demo:
|
114 |
+
gr.Markdown("# 📝 Advanced Text Summarizer")
|
115 |
+
gr.Markdown("Summarize text using different models and styles, with optional agent assistance.")
|
116 |
+
|
117 |
+
with gr.Tabs():
|
118 |
+
# Direct summarization tab
|
119 |
+
with gr.Tab("Direct Summarization"):
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Column():
|
122 |
+
# Input section
|
123 |
+
direct_text_input = gr.Textbox(
|
124 |
+
label="Text to Summarize",
|
125 |
+
placeholder="Enter text to summarize...",
|
126 |
+
lines=10
|
127 |
+
)
|
128 |
+
|
129 |
+
# Sample texts dropdown
|
130 |
+
sample_dropdown = gr.Dropdown(
|
131 |
+
choices=list(sample_texts.keys()),
|
132 |
+
label="Or Select a Sample Text"
|
133 |
+
)
|
134 |
+
|
135 |
+
# Configuration options
|
136 |
+
with gr.Row():
|
137 |
+
with gr.Column():
|
138 |
+
model_dropdown = gr.Dropdown(
|
139 |
+
choices=list(summarizer_tool.available_models.keys()),
|
140 |
+
value="facebook/bart-large-cnn",
|
141 |
+
label="Summarization Model"
|
142 |
+
)
|
143 |
+
|
144 |
+
style_dropdown = gr.Dropdown(
|
145 |
+
choices=["concise", "detailed", "bullet_points"],
|
146 |
+
value="concise",
|
147 |
+
label="Summary Style"
|
148 |
+
)
|
149 |
+
|
150 |
+
with gr.Column():
|
151 |
+
max_length_slider = gr.Slider(
|
152 |
+
minimum=50,
|
153 |
+
maximum=250,
|
154 |
+
value=130,
|
155 |
+
step=10,
|
156 |
+
label="Maximum Summary Length"
|
157 |
+
)
|
158 |
+
|
159 |
+
min_length_slider = gr.Slider(
|
160 |
+
minimum=10,
|
161 |
+
maximum=100,
|
162 |
+
value=30,
|
163 |
+
step=5,
|
164 |
+
label="Minimum Summary Length"
|
165 |
+
)
|
166 |
+
|
167 |
+
direct_summarize_button = gr.Button("Summarize Text")
|
168 |
+
|
169 |
+
with gr.Column():
|
170 |
+
# Output section
|
171 |
+
direct_output = gr.Textbox(label="Summary", lines=12)
|
172 |
+
|
173 |
+
# Agent-assisted summarization tab
|
174 |
+
with gr.Tab("Agent-Assisted Summarization"):
|
175 |
+
with gr.Row():
|
176 |
+
with gr.Column():
|
177 |
+
# Input section
|
178 |
+
agent_text_input = gr.Textbox(
|
179 |
+
label="Text to Summarize",
|
180 |
+
placeholder="Enter text to summarize...",
|
181 |
+
lines=10
|
182 |
+
)
|
183 |
+
|
184 |
+
# Agent instruction
|
185 |
+
instruction_input = gr.Textbox(
|
186 |
+
label="Instructions for the Agent",
|
187 |
+
placeholder="E.g., 'Summarize this text and highlight the three most important points'",
|
188 |
+
lines=2,
|
189 |
+
value="Summarize this text in a professional tone, highlighting key information."
|
190 |
+
)
|
191 |
+
|
192 |
+
# Sample instructions
|
193 |
+
instruction_examples = gr.Dropdown(
|
194 |
+
choices=[
|
195 |
+
"Summarize this text in a professional tone, highlighting key information.",
|
196 |
+
"Create a very concise summary focusing only on actionable items.",
|
197 |
+
"Summarize this for a high school student, explaining complex terms.",
|
198 |
+
"Extract the main argument and supporting evidence from this text.",
|
199 |
+
"Create a summary that focuses on financial implications mentioned in the text."
|
200 |
+
],
|
201 |
+
label="Or Select Example Instructions"
|
202 |
+
)
|
203 |
+
|
204 |
+
# Temperature setting
|
205 |
+
temperature_slider = gr.Slider(
|
206 |
+
minimum=0.1,
|
207 |
+
maximum=1.0,
|
208 |
+
value=0.7,
|
209 |
+
step=0.1,
|
210 |
+
label="Agent Temperature (creativity)"
|
211 |
+
)
|
212 |
+
|
213 |
+
agent_summarize_button = gr.Button("Use Agent to Summarize")
|
214 |
+
|
215 |
+
with gr.Column():
|
216 |
+
# Output section
|
217 |
+
agent_output = gr.Textbox(label="Agent Response", lines=15)
|
218 |
+
|
219 |
+
# Set up event handlers
|
220 |
+
def load_sample(sample_name):
|
221 |
+
return sample_texts.get(sample_name, "")
|
222 |
+
|
223 |
+
def load_instruction(instruction):
|
224 |
+
return instruction
|
225 |
|
226 |
+
sample_dropdown.change(
|
227 |
+
load_sample,
|
228 |
+
inputs=sample_dropdown,
|
229 |
+
outputs=direct_text_input
|
230 |
+
)
|
231 |
+
|
232 |
+
instruction_examples.change(
|
233 |
+
load_instruction,
|
234 |
+
inputs=instruction_examples,
|
235 |
+
outputs=instruction_input
|
236 |
+
)
|
237 |
+
|
238 |
+
direct_summarize_button.click(
|
239 |
+
summarize_text,
|
240 |
+
inputs=[direct_text_input, model_dropdown, max_length_slider, min_length_slider, style_dropdown],
|
241 |
+
outputs=direct_output
|
242 |
+
)
|
243 |
+
|
244 |
+
agent_summarize_button.click(
|
245 |
+
agent_summarize,
|
246 |
+
inputs=[agent_text_input, instruction_input, temperature_slider],
|
247 |
+
outputs=agent_output
|
248 |
+
)
|
249 |
+
|
250 |
+
# Also allow using sample text for agent tab
|
251 |
+
sample_dropdown.change(
|
252 |
+
load_sample,
|
253 |
+
inputs=sample_dropdown,
|
254 |
+
outputs=agent_text_input
|
255 |
+
)
|
256 |
+
|
257 |
+
# Launch the app
|
258 |
+
if __name__ == "__main__":
|
259 |
+
demo.launch()
|