Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,23 +3,30 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
import wikipedia
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Initialize Hugging Face Inference Client
|
| 12 |
client = InferenceClient()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
@tool
|
| 16 |
def generate_image(description: str) -> str:
|
| 17 |
"""
|
| 18 |
Generates an image based on the given text description.
|
| 19 |
-
|
| 20 |
Args:
|
| 21 |
description (str): The textual description of the image to generate.
|
| 22 |
-
|
| 23 |
Returns:
|
| 24 |
str: A URL to the generated image or an error message.
|
| 25 |
"""
|
|
@@ -39,15 +46,15 @@ def generate_image(description: str) -> str:
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error generating image: {str(e)}"
|
| 41 |
|
| 42 |
-
#
|
| 43 |
@tool
|
| 44 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 45 |
"""
|
| 46 |
Fetches the current local time in a specified timezone.
|
| 47 |
-
|
| 48 |
Args:
|
| 49 |
timezone (str): A valid timezone (e.g., 'America/New_York').
|
| 50 |
-
|
| 51 |
Returns:
|
| 52 |
str: The current time in the specified timezone.
|
| 53 |
"""
|
|
@@ -58,11 +65,11 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 60 |
|
| 61 |
-
#
|
| 62 |
@tool
|
| 63 |
def get_weather(city: str, api_key: str) -> str:
|
| 64 |
"""
|
| 65 |
-
|
| 66 |
|
| 67 |
Args:
|
| 68 |
city (str): The name of the city to fetch weather for.
|
|
@@ -83,4 +90,60 @@ def get_weather(city: str, api_key: str) -> str:
|
|
| 83 |
else:
|
| 84 |
return f"Error fetching weather: {data.get('message', 'Unknown error')}"
|
| 85 |
except Exception as e:
|
| 86 |
-
return f"Error fetching weather: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
|
| 10 |
+
# Attempt to import Wikipedia module; install if missing
|
| 11 |
+
try:
|
| 12 |
+
import wikipedia
|
| 13 |
+
except ImportError:
|
| 14 |
+
import os
|
| 15 |
+
os.system("pip install wikipedia")
|
| 16 |
+
import wikipedia
|
| 17 |
+
|
| 18 |
# Initialize Hugging Face Inference Client
|
| 19 |
client = InferenceClient()
|
| 20 |
|
| 21 |
+
# Tool 1: Image Generation
|
| 22 |
@tool
|
| 23 |
def generate_image(description: str) -> str:
|
| 24 |
"""
|
| 25 |
Generates an image based on the given text description.
|
| 26 |
+
|
| 27 |
Args:
|
| 28 |
description (str): The textual description of the image to generate.
|
| 29 |
+
|
| 30 |
Returns:
|
| 31 |
str: A URL to the generated image or an error message.
|
| 32 |
"""
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error generating image: {str(e)}"
|
| 48 |
|
| 49 |
+
# Tool 2: Get Current Time in a Given Timezone
|
| 50 |
@tool
|
| 51 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 52 |
"""
|
| 53 |
Fetches the current local time in a specified timezone.
|
| 54 |
+
|
| 55 |
Args:
|
| 56 |
timezone (str): A valid timezone (e.g., 'America/New_York').
|
| 57 |
+
|
| 58 |
Returns:
|
| 59 |
str: The current time in the specified timezone.
|
| 60 |
"""
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 67 |
|
| 68 |
+
# Tool 3: Fetch Weather Information
|
| 69 |
@tool
|
| 70 |
def get_weather(city: str, api_key: str) -> str:
|
| 71 |
"""
|
| 72 |
+
Fetches the current weather for a given city.
|
| 73 |
|
| 74 |
Args:
|
| 75 |
city (str): The name of the city to fetch weather for.
|
|
|
|
| 90 |
else:
|
| 91 |
return f"Error fetching weather: {data.get('message', 'Unknown error')}"
|
| 92 |
except Exception as e:
|
| 93 |
+
return f"Error fetching weather: {str(e)}"
|
| 94 |
+
|
| 95 |
+
# Tool 4: Wikipedia Search
|
| 96 |
+
@tool
|
| 97 |
+
def wikipedia_summary(query: str) -> str:
|
| 98 |
+
"""
|
| 99 |
+
Fetches a summary from Wikipedia based on a search query.
|
| 100 |
+
|
| 101 |
+
Args:
|
| 102 |
+
query (str): The topic to search for on Wikipedia.
|
| 103 |
+
|
| 104 |
+
Returns:
|
| 105 |
+
str: A short summary of the topic.
|
| 106 |
+
"""
|
| 107 |
+
try:
|
| 108 |
+
summary = wikipedia.summary(query, sentences=2)
|
| 109 |
+
return summary
|
| 110 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 111 |
+
return f"Multiple results found, be more specific: {e.options}"
|
| 112 |
+
except wikipedia.exceptions.PageError:
|
| 113 |
+
return "No Wikipedia page found for the given query."
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return f"Error fetching Wikipedia summary: {str(e)}"
|
| 116 |
+
|
| 117 |
+
# Load Final Answer Tool
|
| 118 |
+
final_answer = FinalAnswerTool()
|
| 119 |
+
|
| 120 |
+
# Define the LLM model
|
| 121 |
+
model = HfApiModel(
|
| 122 |
+
max_tokens=2096,
|
| 123 |
+
temperature=0.5,
|
| 124 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 125 |
+
custom_role_conversions=None,
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Import tool from Hugging Face Hub
|
| 129 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 130 |
+
|
| 131 |
+
# Load Prompt Templates
|
| 132 |
+
with open("prompts.yaml", 'r') as stream:
|
| 133 |
+
prompt_templates = yaml.safe_load(stream)
|
| 134 |
+
|
| 135 |
+
# Create the CodeAgent
|
| 136 |
+
agent = CodeAgent(
|
| 137 |
+
model=model,
|
| 138 |
+
tools=[final_answer, generate_image, get_current_time_in_timezone, get_weather, wikipedia_summary, DuckDuckGoSearchTool()], # Added all tools
|
| 139 |
+
max_steps=6,
|
| 140 |
+
verbosity_level=1,
|
| 141 |
+
grammar=None,
|
| 142 |
+
planning_interval=None,
|
| 143 |
+
name="My Custom Agent",
|
| 144 |
+
description="An AI assistant with various tools for image generation, weather, time, and Wikipedia search.",
|
| 145 |
+
prompt_templates=prompt_templates
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Launch UI
|
| 149 |
+
GradioUI(agent).launch()
|