Spaces:
Build error
Build error
| 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() | |