Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| from smolagents import LiteLLMModel, ToolCallingAgent, Tool | |
| import wikipedia | |
| import gradio as gr | |
| import pandas as pd | |
| # Optional: Hugging Face token (for private models) | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # --- Tools --- | |
| class WebSearchTool(Tool): | |
| name = "web_search" | |
| description = "Search the web and return concise results. Input: search query string." | |
| inputs = { | |
| "query": { | |
| "type": "string", | |
| "description": "The search query to look up on the web" | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, query: str) -> str: | |
| from smolagents import DuckDuckGoSearchTool | |
| tool = DuckDuckGoSearchTool() | |
| return tool.forward(query) | |
| class LoadCsvTool(Tool): | |
| name = "load_csv" | |
| description = "Load and analyze CSV file. Returns summary statistics and first few rows. Input: file path." | |
| inputs = { | |
| "file_path": { | |
| "type": "string", | |
| "description": "Path to CSV file (e.g., 'data.csv' or '/app/data.csv')" | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, file_path: str) -> str: | |
| return pd.read_csv(file_path) | |
| class WikipediaTool(Tool): | |
| name = "wikipedia_search" | |
| description = "Fetch Wikipedia summary for a topic. Input: topic string." | |
| inputs = { | |
| "topic": { | |
| "type": "string", | |
| "description": "The topic to search for on Wikipedia" | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, topic: str) -> str: | |
| try: | |
| summary = wikipedia.summary(topic, sentences=3) | |
| return summary | |
| except Exception as e: | |
| return f"Wikipedia lookup failed: {e}" | |
| class WeatherTool(Tool): | |
| name = "weather" | |
| description = "Get current weather for a city. Input: city name." | |
| inputs = { | |
| "city": { | |
| "type": "string", | |
| "description": "The name of the city to get weather for" | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, city: str) -> str: | |
| try: | |
| # Geocoding to get coordinates | |
| geocode_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}" | |
| geo_resp = requests.get(geocode_url, timeout=10).json() | |
| results = geo_resp.get("results") | |
| if not results: | |
| return f"Could not find coordinates for {city}." | |
| lat = results[0]["latitude"] | |
| lon = results[0]["longitude"] | |
| # Get current weather | |
| weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true" | |
| weather_resp = requests.get(weather_url, timeout=10).json() | |
| weather = weather_resp.get("current_weather") | |
| if not weather: | |
| return f"Could not retrieve weather for {city}." | |
| return f"Weather in {city}: {weather['temperature']}°C, wind {weather['windspeed']} km/h, weather code {weather['weathercode']}." | |
| except Exception as e: | |
| return f"Weather lookup failed: {e}" | |
| # --- Initialize LLM Model --- | |
| model = LiteLLMModel( | |
| model_id="huggingface/google/gemma-2-2b-it", | |
| hf_token=HF_TOKEN | |
| ) | |
| # --- Initialize Tool-Calling Agent --- | |
| agent = ToolCallingAgent( | |
| tools=[WebSearchTool(), WikipediaTool(), WeatherTool(), LoadCsvTool()], | |
| model=model, | |
| max_steps=10, | |
| ) | |
| # --- Custom Gradio Interface --- | |
| def chat_with_agent(message, history): | |
| """Process user message and return agent response""" | |
| try: | |
| result = agent.run(message) | |
| return str(result) | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create Gradio ChatInterface | |
| demo = gr.ChatInterface( | |
| fn=chat_with_agent, | |
| title="🤖 Internet Agent", | |
| description="An AI agent with web search, Wikipedia, weather and Csv-Reader tools powered by Gemma-2-2b", | |
| examples=[ | |
| "What's the weather in Paris?", | |
| "Search for recent news about AI", | |
| "Tell me about Albert Einstein from Wikipedia", | |
| "What's the current temperature in Tokyo?" | |
| ] | |
| ) | |
| # --- Launch Gradio Web UI --- | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |