Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from duckduckgo_search import DDGS # Updated import for the new API
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
# ---- Initialize Summarization Pipeline ----
|
| 8 |
-
# This uses the 'sshleifer/distilbart-cnn-12-6' model to summarize input text.
|
| 9 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 10 |
|
| 11 |
# ---- Web Scrape Agent ----
|
|
@@ -21,7 +20,6 @@ def web_scrape_agent(url):
|
|
| 21 |
# ---- Research Agent using DuckDuckGo Search with new API ----
|
| 22 |
def research_agent(query):
|
| 23 |
try:
|
| 24 |
-
# Use the DDGS class as a context manager to perform a text search.
|
| 25 |
with DDGS() as ddgs:
|
| 26 |
results = ddgs.text(query, max_results=5)
|
| 27 |
if results:
|
|
@@ -37,7 +35,6 @@ def research_agent(query):
|
|
| 37 |
# ---- Report Agent using Transformers Summarization ----
|
| 38 |
def report_agent(data):
|
| 39 |
try:
|
| 40 |
-
# If the input text is too short for a meaningful summary, notify the user.
|
| 41 |
if len(data.split()) < 30:
|
| 42 |
return "Input text is too short to generate a summary. Please provide more details."
|
| 43 |
summary = summarizer(data, max_length=130, min_length=30, do_sample=False)
|
|
@@ -47,7 +44,6 @@ def report_agent(data):
|
|
| 47 |
|
| 48 |
# ---- Planning Agent ----
|
| 49 |
def planning_agent(goal):
|
| 50 |
-
# A basic plan that breaks down any goal into five simple steps.
|
| 51 |
steps = [
|
| 52 |
f"Understand the requirements of '{goal}'.",
|
| 53 |
f"Break down the goal into smaller, manageable tasks.",
|
|
@@ -83,7 +79,6 @@ def weather_report_agent(city):
|
|
| 83 |
if "current_weather" in data:
|
| 84 |
weather_code = data["current_weather"]["weathercode"]
|
| 85 |
temp = data["current_weather"]["temperature"]
|
| 86 |
-
# Mapping weather codes to human-friendly descriptions.
|
| 87 |
weather_mapping = {
|
| 88 |
0: "Clear sky",
|
| 89 |
1: "Mainly clear",
|
|
@@ -121,41 +116,111 @@ def weather_report_agent(city):
|
|
| 121 |
except Exception as e:
|
| 122 |
return f"Error fetching weather data: {e}"
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# ---- Gradio Interface Definition ----
|
| 125 |
-
with gr.Blocks() as demo:
|
| 126 |
gr.Markdown("# Multi-Agent Showcase App")
|
| 127 |
-
gr.Markdown(
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
with gr.Tabs():
|
| 130 |
-
# Web Scrape Tab
|
| 131 |
with gr.TabItem("Web Scrape"):
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
| 133 |
scrape_output = gr.Textbox(label="Scraped Content", lines=10)
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
with gr.TabItem("Research"):
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
research_output = gr.Textbox(label="Research Summary", lines=10)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
with gr.TabItem("Report"):
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
| 145 |
report_output = gr.Textbox(label="Generated Report", lines=10)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
| 149 |
with gr.TabItem("Planning"):
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
planning_output = gr.Textbox(label="Action Plan", lines=6)
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
| 155 |
with gr.TabItem("Weather"):
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
weather_output = gr.Textbox(label="Weather Report", lines=3)
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
# Launch the app
|
| 161 |
demo.launch()
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
# ---- Initialize Summarization Pipeline ----
|
|
|
|
| 8 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 9 |
|
| 10 |
# ---- Web Scrape Agent ----
|
|
|
|
| 20 |
# ---- Research Agent using DuckDuckGo Search with new API ----
|
| 21 |
def research_agent(query):
|
| 22 |
try:
|
|
|
|
| 23 |
with DDGS() as ddgs:
|
| 24 |
results = ddgs.text(query, max_results=5)
|
| 25 |
if results:
|
|
|
|
| 35 |
# ---- Report Agent using Transformers Summarization ----
|
| 36 |
def report_agent(data):
|
| 37 |
try:
|
|
|
|
| 38 |
if len(data.split()) < 30:
|
| 39 |
return "Input text is too short to generate a summary. Please provide more details."
|
| 40 |
summary = summarizer(data, max_length=130, min_length=30, do_sample=False)
|
|
|
|
| 44 |
|
| 45 |
# ---- Planning Agent ----
|
| 46 |
def planning_agent(goal):
|
|
|
|
| 47 |
steps = [
|
| 48 |
f"Understand the requirements of '{goal}'.",
|
| 49 |
f"Break down the goal into smaller, manageable tasks.",
|
|
|
|
| 79 |
if "current_weather" in data:
|
| 80 |
weather_code = data["current_weather"]["weathercode"]
|
| 81 |
temp = data["current_weather"]["temperature"]
|
|
|
|
| 82 |
weather_mapping = {
|
| 83 |
0: "Clear sky",
|
| 84 |
1: "Mainly clear",
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
return f"Error fetching weather data: {e}"
|
| 118 |
|
| 119 |
+
# ---- Showcase All Agents Together ----
|
| 120 |
+
def showcase_all(url, research_query, report_data, planning_goal, city):
|
| 121 |
+
scraped = web_scrape_agent(url)
|
| 122 |
+
research = research_agent(research_query)
|
| 123 |
+
report = report_agent(report_data)
|
| 124 |
+
planning = planning_agent(planning_goal)
|
| 125 |
+
weather = weather_report_agent(city)
|
| 126 |
+
return scraped, research, report, planning, weather
|
| 127 |
+
|
| 128 |
+
# ---- Clear function for the Showcase Magic tab (clears both inputs and outputs) ----
|
| 129 |
+
def clear_all():
|
| 130 |
+
# Returns 10 empty strings (5 inputs + 5 outputs)
|
| 131 |
+
return "", "", "", "", "", "", "", "", "", ""
|
| 132 |
+
|
| 133 |
# ---- Gradio Interface Definition ----
|
| 134 |
+
with gr.Blocks(title="Multi-Agent Showcase App") as demo:
|
| 135 |
gr.Markdown("# Multi-Agent Showcase App")
|
| 136 |
+
gr.Markdown(
|
| 137 |
+
"Experience the magic of multiple agents at work! "
|
| 138 |
+
"Explore each tool individually or try the **Showcase Magic** tab to see all in action."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
with gr.Tabs():
|
| 142 |
+
# --------- Web Scrape Tab ---------
|
| 143 |
with gr.TabItem("Web Scrape"):
|
| 144 |
+
with gr.Row():
|
| 145 |
+
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com", lines=1)
|
| 146 |
+
scrape_button = gr.Button("Scrape")
|
| 147 |
+
clear_scrape = gr.Button("Clear")
|
| 148 |
scrape_output = gr.Textbox(label="Scraped Content", lines=10)
|
| 149 |
+
scrape_button.click(web_scrape_agent, inputs=url_input, outputs=scrape_output)
|
| 150 |
+
clear_scrape.click(lambda: "", None, scrape_output)
|
| 151 |
+
|
| 152 |
+
# --------- Research Tab ---------
|
| 153 |
with gr.TabItem("Research"):
|
| 154 |
+
with gr.Row():
|
| 155 |
+
research_query = gr.Textbox(label="Enter Research Query", placeholder="Latest trends in AI", lines=1)
|
| 156 |
+
research_button = gr.Button("Search")
|
| 157 |
+
clear_research = gr.Button("Clear")
|
| 158 |
research_output = gr.Textbox(label="Research Summary", lines=10)
|
| 159 |
+
research_button.click(research_agent, inputs=research_query, outputs=research_output)
|
| 160 |
+
clear_research.click(lambda: "", None, research_output)
|
| 161 |
+
|
| 162 |
+
# --------- Report Tab ---------
|
| 163 |
with gr.TabItem("Report"):
|
| 164 |
+
with gr.Row():
|
| 165 |
+
report_input = gr.Textbox(label="Enter Data for Report", placeholder="Paste your text here...", lines=10)
|
| 166 |
+
report_button = gr.Button("Generate Report")
|
| 167 |
+
clear_report = gr.Button("Clear")
|
| 168 |
report_output = gr.Textbox(label="Generated Report", lines=10)
|
| 169 |
+
report_button.click(report_agent, inputs=report_input, outputs=report_output)
|
| 170 |
+
clear_report.click(lambda: "", None, report_output)
|
| 171 |
+
|
| 172 |
+
# --------- Planning Tab ---------
|
| 173 |
with gr.TabItem("Planning"):
|
| 174 |
+
with gr.Row():
|
| 175 |
+
planning_input = gr.Textbox(label="Enter Your Goal", placeholder="E.g., Plan a trip to Paris", lines=2)
|
| 176 |
+
planning_button = gr.Button("Plan")
|
| 177 |
+
clear_planning = gr.Button("Clear")
|
| 178 |
planning_output = gr.Textbox(label="Action Plan", lines=6)
|
| 179 |
+
planning_button.click(planning_agent, inputs=planning_input, outputs=planning_output)
|
| 180 |
+
clear_planning.click(lambda: "", None, planning_output)
|
| 181 |
+
|
| 182 |
+
# --------- Weather Tab ---------
|
| 183 |
with gr.TabItem("Weather"):
|
| 184 |
+
with gr.Row():
|
| 185 |
+
city_input = gr.Textbox(label="Enter City Name", placeholder="e.g., London", lines=1)
|
| 186 |
+
weather_button = gr.Button("Get Weather")
|
| 187 |
+
clear_weather = gr.Button("Clear")
|
| 188 |
weather_output = gr.Textbox(label="Weather Report", lines=3)
|
| 189 |
+
weather_button.click(weather_report_agent, inputs=city_input, outputs=weather_output)
|
| 190 |
+
clear_weather.click(lambda: "", None, weather_output)
|
| 191 |
+
|
| 192 |
+
# --------- Showcase Magic Tab ---------
|
| 193 |
+
with gr.TabItem("Showcase Magic"):
|
| 194 |
+
gr.Markdown("Provide inputs for each tool and click **Showcase Magic** to see all agents in action!")
|
| 195 |
+
with gr.Row():
|
| 196 |
+
with gr.Column():
|
| 197 |
+
demo_url = gr.Textbox(label="URL for Web Scrape", placeholder="https://example.com", lines=1)
|
| 198 |
+
demo_research = gr.Textbox(label="Research Query", placeholder="Latest trends in AI", lines=1)
|
| 199 |
+
demo_report = gr.Textbox(label="Data for Report", placeholder="Paste your text here...", lines=3)
|
| 200 |
+
with gr.Column():
|
| 201 |
+
demo_planning = gr.Textbox(label="Your Goal", placeholder="Plan a trip to Paris", lines=1)
|
| 202 |
+
demo_city = gr.Textbox(label="City for Weather", placeholder="e.g., London", lines=1)
|
| 203 |
+
with gr.Row():
|
| 204 |
+
showcase_button = gr.Button("Showcase Magic")
|
| 205 |
+
clear_showcase = gr.Button("Clear All")
|
| 206 |
+
with gr.Row():
|
| 207 |
+
out_scrape = gr.Textbox(label="Scraped Content", lines=10)
|
| 208 |
+
out_research = gr.Textbox(label="Research Summary", lines=10)
|
| 209 |
+
with gr.Row():
|
| 210 |
+
out_report = gr.Textbox(label="Generated Report", lines=10)
|
| 211 |
+
out_planning = gr.Textbox(label="Action Plan", lines=6)
|
| 212 |
+
with gr.Row():
|
| 213 |
+
out_weather = gr.Textbox(label="Weather Report", lines=3)
|
| 214 |
+
showcase_button.click(
|
| 215 |
+
showcase_all,
|
| 216 |
+
inputs=[demo_url, demo_research, demo_report, demo_planning, demo_city],
|
| 217 |
+
outputs=[out_scrape, out_research, out_report, out_planning, out_weather]
|
| 218 |
+
)
|
| 219 |
+
# The clear button resets both input and output fields for this tab.
|
| 220 |
+
clear_showcase.click(
|
| 221 |
+
clear_all,
|
| 222 |
+
outputs=[demo_url, demo_research, demo_report, demo_planning, demo_city, out_scrape, out_research, out_report, out_planning, out_weather]
|
| 223 |
+
)
|
| 224 |
|
| 225 |
# Launch the app
|
| 226 |
demo.launch()
|