Spaces:
Build error
Build error
File size: 10,349 Bytes
d3cabca ceb828a d3cabca ceb828a d3cabca ceb828a d3cabca ceb828a d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca 6e203bf d3cabca |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
import gradio as gr
import requests
from bs4 import BeautifulSoup
from duckduckgo_search import DDGS # Updated import for the new API
from transformers import pipeline
# ---- Initialize Summarization Pipeline ----
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
# ---- Web Scrape Agent ----
def web_scrape_agent(url):
try:
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(response.text, "html.parser")
text = soup.get_text(separator="\n")
return text.strip() or "No content found."
except Exception as e:
return f"Error during scraping: {e}"
# ---- Research Agent using DuckDuckGo Search with new API ----
def research_agent(query):
try:
with DDGS() as ddgs:
results = ddgs.text(query, max_results=5)
if results:
response = "\n\n".join(
[f"{result.get('title', 'No Title')}:\n{result.get('href', 'No URL')}" for result in results]
)
return response
else:
return "No results found."
except Exception as e:
return f"Error during research: {e}"
# ---- Report Agent using Transformers Summarization ----
def report_agent(data):
try:
if len(data.split()) < 30:
return "Input text is too short to generate a summary. Please provide more details."
summary = summarizer(data, max_length=130, min_length=30, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"Error during report generation: {e}"
# ---- Planning Agent ----
def planning_agent(goal):
steps = [
f"Understand the requirements of '{goal}'.",
f"Break down the goal into smaller, manageable tasks.",
f"Prioritize the tasks and assign timelines.",
f"Execute the plan and monitor progress.",
f"Review and adjust the plan as necessary."
]
return "\n".join([f"Step {i+1}: {step}" for i, step in enumerate(steps)])
# ---- Weather Report Agent using Open-Meteo and Nominatim for Geocoding ----
def get_coordinates(city):
try:
url = f"https://nominatim.openstreetmap.org/search?city={city}&format=json"
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
data = response.json()
if data:
lat = data[0]["lat"]
lon = data[0]["lon"]
return lat, lon
else:
return None, None
except Exception:
return None, None
def weather_report_agent(city):
lat, lon = get_coordinates(city)
if not lat or not lon:
return "City not found. Please check the city name."
try:
url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
response = requests.get(url)
data = response.json()
if "current_weather" in data:
weather_code = data["current_weather"]["weathercode"]
temp = data["current_weather"]["temperature"]
weather_mapping = {
0: "Clear sky",
1: "Mainly clear",
2: "Partly cloudy",
3: "Overcast",
45: "Fog",
48: "Depositing rime fog",
51: "Light drizzle",
53: "Moderate drizzle",
55: "Dense drizzle",
56: "Light freezing drizzle",
57: "Dense freezing drizzle",
61: "Slight rain",
63: "Moderate rain",
65: "Heavy rain",
66: "Light freezing rain",
67: "Heavy freezing rain",
71: "Slight snowfall",
73: "Moderate snowfall",
75: "Heavy snowfall",
77: "Snow grains",
80: "Slight rain showers",
81: "Moderate rain showers",
82: "Violent rain showers",
85: "Slight snow showers",
86: "Heavy snow showers",
95: "Thunderstorm",
96: "Thunderstorm with slight hail",
99: "Thunderstorm with heavy hail"
}
description = weather_mapping.get(weather_code, f"Code {weather_code}")
return f"Current weather in {city}:\nTemperature: {temp}°C\nCondition: {description}"
else:
return "Could not fetch weather data."
except Exception as e:
return f"Error fetching weather data: {e}"
# ---- Showcase All Agents Together ----
def showcase_all(url, research_query, report_data, planning_goal, city):
scraped = web_scrape_agent(url)
research = research_agent(research_query)
report = report_agent(report_data)
planning = planning_agent(planning_goal)
weather = weather_report_agent(city)
return scraped, research, report, planning, weather
# ---- Clear function for the Showcase Magic tab (clears both inputs and outputs) ----
def clear_all():
# Returns 10 empty strings (5 inputs + 5 outputs)
return "", "", "", "", "", "", "", "", "", ""
# ---- Gradio Interface Definition ----
with gr.Blocks(title="Multi-Agent Showcase App") as demo:
gr.Markdown("# Multi-Agent Showcase App")
gr.Markdown(
"Experience the magic of multiple agents at work! "
"Explore each tool individually or try the **Showcase Magic** tab to see all in action."
)
with gr.Tabs():
# --------- Web Scrape Tab ---------
with gr.TabItem("Web Scrape"):
with gr.Row():
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com", lines=1)
scrape_button = gr.Button("Scrape")
clear_scrape = gr.Button("Clear")
scrape_output = gr.Textbox(label="Scraped Content", lines=10)
scrape_button.click(web_scrape_agent, inputs=url_input, outputs=scrape_output)
clear_scrape.click(lambda: "", None, scrape_output)
# --------- Research Tab ---------
with gr.TabItem("Research"):
with gr.Row():
research_query = gr.Textbox(label="Enter Research Query", placeholder="Latest trends in AI", lines=1)
research_button = gr.Button("Search")
clear_research = gr.Button("Clear")
research_output = gr.Textbox(label="Research Summary", lines=10)
research_button.click(research_agent, inputs=research_query, outputs=research_output)
clear_research.click(lambda: "", None, research_output)
# --------- Report Tab ---------
with gr.TabItem("Report"):
with gr.Row():
report_input = gr.Textbox(label="Enter Data for Report", placeholder="Paste your text here...", lines=10)
report_button = gr.Button("Generate Report")
clear_report = gr.Button("Clear")
report_output = gr.Textbox(label="Generated Report", lines=10)
report_button.click(report_agent, inputs=report_input, outputs=report_output)
clear_report.click(lambda: "", None, report_output)
# --------- Planning Tab ---------
with gr.TabItem("Planning"):
with gr.Row():
planning_input = gr.Textbox(label="Enter Your Goal", placeholder="E.g., Plan a trip to Paris", lines=2)
planning_button = gr.Button("Plan")
clear_planning = gr.Button("Clear")
planning_output = gr.Textbox(label="Action Plan", lines=6)
planning_button.click(planning_agent, inputs=planning_input, outputs=planning_output)
clear_planning.click(lambda: "", None, planning_output)
# --------- Weather Tab ---------
with gr.TabItem("Weather"):
with gr.Row():
city_input = gr.Textbox(label="Enter City Name", placeholder="e.g., London", lines=1)
weather_button = gr.Button("Get Weather")
clear_weather = gr.Button("Clear")
weather_output = gr.Textbox(label="Weather Report", lines=3)
weather_button.click(weather_report_agent, inputs=city_input, outputs=weather_output)
clear_weather.click(lambda: "", None, weather_output)
# --------- Showcase Magic Tab ---------
with gr.TabItem("Showcase Magic"):
gr.Markdown("Provide inputs for each tool and click **Showcase Magic** to see all agents in action!")
with gr.Row():
with gr.Column():
demo_url = gr.Textbox(label="URL for Web Scrape", placeholder="https://example.com", lines=1)
demo_research = gr.Textbox(label="Research Query", placeholder="Latest trends in AI", lines=1)
demo_report = gr.Textbox(label="Data for Report", placeholder="Paste your text here...", lines=3)
with gr.Column():
demo_planning = gr.Textbox(label="Your Goal", placeholder="Plan a trip to Paris", lines=1)
demo_city = gr.Textbox(label="City for Weather", placeholder="e.g., London", lines=1)
with gr.Row():
showcase_button = gr.Button("Showcase Magic")
clear_showcase = gr.Button("Clear All")
with gr.Row():
out_scrape = gr.Textbox(label="Scraped Content", lines=10)
out_research = gr.Textbox(label="Research Summary", lines=10)
with gr.Row():
out_report = gr.Textbox(label="Generated Report", lines=10)
out_planning = gr.Textbox(label="Action Plan", lines=6)
with gr.Row():
out_weather = gr.Textbox(label="Weather Report", lines=3)
showcase_button.click(
showcase_all,
inputs=[demo_url, demo_research, demo_report, demo_planning, demo_city],
outputs=[out_scrape, out_research, out_report, out_planning, out_weather]
)
# The clear button resets both input and output fields for this tab.
clear_showcase.click(
clear_all,
outputs=[demo_url, demo_research, demo_report, demo_planning, demo_city, out_scrape, out_research, out_report, out_planning, out_weather]
)
# Launch the app
demo.launch()
|