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|
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from duckduckgo_search import ddg
|
| 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 ----
|
| 12 |
+
def web_scrape_agent(url):
|
| 13 |
+
try:
|
| 14 |
+
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
|
| 15 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 16 |
+
text = soup.get_text(separator="\n")
|
| 17 |
+
return text.strip() or "No content found."
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"Error during scraping: {e}"
|
| 20 |
+
|
| 21 |
+
# ---- Research Agent using DuckDuckGo Search ----
|
| 22 |
+
def research_agent(query):
|
| 23 |
+
try:
|
| 24 |
+
results = ddg(query, max_results=5)
|
| 25 |
+
if results:
|
| 26 |
+
response = "\n".join([f"{result['title']}:\n{result['href']}" for result in results])
|
| 27 |
+
return response
|
| 28 |
+
else:
|
| 29 |
+
return "No results found."
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error during research: {e}"
|
| 32 |
+
|
| 33 |
+
# ---- Report Agent using Transformers Summarization ----
|
| 34 |
+
def report_agent(data):
|
| 35 |
+
try:
|
| 36 |
+
# If the input text is too short for a meaningful summary, notify the user.
|
| 37 |
+
if len(data.split()) < 30:
|
| 38 |
+
return "Input text is too short to generate a summary. Please provide more details."
|
| 39 |
+
summary = summarizer(data, max_length=130, min_length=30, do_sample=False)
|
| 40 |
+
return summary[0]['summary_text']
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error during report generation: {e}"
|
| 43 |
+
|
| 44 |
+
# ---- Planning Agent ----
|
| 45 |
+
def planning_agent(goal):
|
| 46 |
+
# A basic plan that breaks down any goal into five simple steps.
|
| 47 |
+
steps = [
|
| 48 |
+
f"Understand the requirements of '{goal}'.",
|
| 49 |
+
f"Break down the goal into smaller, manageable tasks.",
|
| 50 |
+
f"Prioritize the tasks and assign timelines.",
|
| 51 |
+
f"Execute the plan and monitor progress.",
|
| 52 |
+
f"Review and adjust the plan as necessary."
|
| 53 |
+
]
|
| 54 |
+
return "\n".join([f"Step {i+1}: {step}" for i, step in enumerate(steps)])
|
| 55 |
+
|
| 56 |
+
# ---- Weather Report Agent using Open-Meteo and Nominatim for Geocoding ----
|
| 57 |
+
def get_coordinates(city):
|
| 58 |
+
try:
|
| 59 |
+
url = f"https://nominatim.openstreetmap.org/search?city={city}&format=json"
|
| 60 |
+
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
|
| 61 |
+
data = response.json()
|
| 62 |
+
if data:
|
| 63 |
+
lat = data[0]["lat"]
|
| 64 |
+
lon = data[0]["lon"]
|
| 65 |
+
return lat, lon
|
| 66 |
+
else:
|
| 67 |
+
return None, None
|
| 68 |
+
except Exception:
|
| 69 |
+
return None, None
|
| 70 |
+
|
| 71 |
+
def weather_report_agent(city):
|
| 72 |
+
lat, lon = get_coordinates(city)
|
| 73 |
+
if not lat or not lon:
|
| 74 |
+
return "City not found. Please check the city name."
|
| 75 |
+
try:
|
| 76 |
+
url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 77 |
+
response = requests.get(url)
|
| 78 |
+
data = response.json()
|
| 79 |
+
if "current_weather" in data:
|
| 80 |
+
weather_code = data["current_weather"]["weathercode"]
|
| 81 |
+
temp = data["current_weather"]["temperature"]
|
| 82 |
+
# Mapping weather codes to human-friendly descriptions.
|
| 83 |
+
weather_mapping = {
|
| 84 |
+
0: "Clear sky",
|
| 85 |
+
1: "Mainly clear",
|
| 86 |
+
2: "Partly cloudy",
|
| 87 |
+
3: "Overcast",
|
| 88 |
+
45: "Fog",
|
| 89 |
+
48: "Depositing rime fog",
|
| 90 |
+
51: "Light drizzle",
|
| 91 |
+
53: "Moderate drizzle",
|
| 92 |
+
55: "Dense drizzle",
|
| 93 |
+
56: "Light freezing drizzle",
|
| 94 |
+
57: "Dense freezing drizzle",
|
| 95 |
+
61: "Slight rain",
|
| 96 |
+
63: "Moderate rain",
|
| 97 |
+
65: "Heavy rain",
|
| 98 |
+
66: "Light freezing rain",
|
| 99 |
+
67: "Heavy freezing rain",
|
| 100 |
+
71: "Slight snowfall",
|
| 101 |
+
73: "Moderate snowfall",
|
| 102 |
+
75: "Heavy snowfall",
|
| 103 |
+
77: "Snow grains",
|
| 104 |
+
80: "Slight rain showers",
|
| 105 |
+
81: "Moderate rain showers",
|
| 106 |
+
82: "Violent rain showers",
|
| 107 |
+
85: "Slight snow showers",
|
| 108 |
+
86: "Heavy snow showers",
|
| 109 |
+
95: "Thunderstorm",
|
| 110 |
+
96: "Thunderstorm with slight hail",
|
| 111 |
+
99: "Thunderstorm with heavy hail"
|
| 112 |
+
}
|
| 113 |
+
description = weather_mapping.get(weather_code, f"Code {weather_code}")
|
| 114 |
+
return f"Current weather in {city}:\nTemperature: {temp}°C\nCondition: {description}"
|
| 115 |
+
else:
|
| 116 |
+
return "Could not fetch weather data."
|
| 117 |
+
except Exception as e:
|
| 118 |
+
return f"Error fetching weather data: {e}"
|
| 119 |
+
|
| 120 |
+
# ---- Gradio Interface Definition ----
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("# Multi-Agent Showcase App")
|
| 123 |
+
gr.Markdown("This app demonstrates multiple agents: Web Scraping, Research, Report Generation, Planning, and Weather Reporting. It's like the Avengers of tools—each with its own superpower!")
|
| 124 |
+
|
| 125 |
+
with gr.Tabs():
|
| 126 |
+
# Web Scrape Tab
|
| 127 |
+
with gr.TabItem("Web Scrape"):
|
| 128 |
+
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com")
|
| 129 |
+
scrape_output = gr.Textbox(label="Scraped Content", lines=10)
|
| 130 |
+
gr.Button("Scrape").click(web_scrape_agent, inputs=url_input, outputs=scrape_output)
|
| 131 |
+
|
| 132 |
+
# Research Tab
|
| 133 |
+
with gr.TabItem("Research"):
|
| 134 |
+
research_query = gr.Textbox(label="Enter Research Query", placeholder="Latest trends in AI")
|
| 135 |
+
research_output = gr.Textbox(label="Research Summary", lines=10)
|
| 136 |
+
gr.Button("Research").click(research_agent, inputs=research_query, outputs=research_output)
|
| 137 |
+
|
| 138 |
+
# Report Tab
|
| 139 |
+
with gr.TabItem("Report"):
|
| 140 |
+
report_input = gr.Textbox(label="Enter Data for Report", placeholder="Paste your text here...", lines=10)
|
| 141 |
+
report_output = gr.Textbox(label="Generated Report", lines=10)
|
| 142 |
+
gr.Button("Generate Report").click(report_agent, inputs=report_input, outputs=report_output)
|
| 143 |
+
|
| 144 |
+
# Planning Tab
|
| 145 |
+
with gr.TabItem("Planning"):
|
| 146 |
+
planning_input = gr.Textbox(label="Enter Your Goal", placeholder="E.g., Plan a trip to Paris", lines=2)
|
| 147 |
+
planning_output = gr.Textbox(label="Action Plan", lines=6)
|
| 148 |
+
gr.Button("Plan").click(planning_agent, inputs=planning_input, outputs=planning_output)
|
| 149 |
+
|
| 150 |
+
# Weather Tab
|
| 151 |
+
with gr.TabItem("Weather"):
|
| 152 |
+
city_input = gr.Textbox(label="Enter City Name", placeholder="e.g., London", lines=1)
|
| 153 |
+
weather_output = gr.Textbox(label="Weather Report", lines=3)
|
| 154 |
+
gr.Button("Get Weather").click(weather_report_agent, inputs=city_input, outputs=weather_output)
|
| 155 |
+
|
| 156 |
+
# Launch the app
|
| 157 |
+
demo.launch()
|