Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,9 @@ from tools.visit_webpage import VisitWebpageTool
|
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
-
|
| 15 |
-
# 1
|
| 16 |
-
|
| 17 |
@tool
|
| 18 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 19 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -34,26 +34,43 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
@tool
|
| 39 |
def generate_image_from_text(prompt: str) -> str:
|
| 40 |
-
"""A tool that generates an image based on a text description.
|
| 41 |
Args:
|
| 42 |
prompt: A detailed text description of the image you want to generate.
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# Return
|
| 50 |
-
return
|
| 51 |
except Exception as e:
|
| 52 |
# Handle any errors that occur during image generation
|
| 53 |
return f"Error generating image: {str(e)}"
|
| 54 |
|
| 55 |
-
|
| 56 |
-
# 3
|
|
|
|
| 57 |
# Initialize the DuckDuckGo search tool from the tools directory
|
| 58 |
web_search_tool = DuckDuckGoSearchTool()
|
| 59 |
|
|
@@ -73,17 +90,21 @@ def search_web(query: str) -> str:
|
|
| 73 |
# Handle any errors that occur during the search
|
| 74 |
return f"Error searching the web: {str(e)}"
|
| 75 |
|
|
|
|
| 76 |
# FINAL ANSWER TOOL (REQUIRED)
|
|
|
|
| 77 |
# Initialize the final answer tool - required for the agent to provide final answers
|
| 78 |
final_answer = FinalAnswerTool()
|
| 79 |
|
| 80 |
-
|
| 81 |
# VISIT WEBPAGE TOOL - From tools directory
|
|
|
|
| 82 |
# Initialize the visit webpage tool
|
| 83 |
visit_webpage = VisitWebpageTool()
|
| 84 |
|
| 85 |
-
|
| 86 |
# TOOL INITIALIZATION AND VERIFICATION
|
|
|
|
| 87 |
# Ensure all tools are properly loaded and print status
|
| 88 |
print("Initializing tools...")
|
| 89 |
try:
|
|
@@ -102,21 +123,22 @@ print("✓ Web search tool initialized")
|
|
| 102 |
print("✓ Visit webpage tool initialized")
|
| 103 |
print("✓ Final answer tool initialized")
|
| 104 |
|
| 105 |
-
|
| 106 |
# MODEL CONFIGURATION
|
|
|
|
| 107 |
print("Configuring model...")
|
| 108 |
-
#
|
| 109 |
-
# Alternative endpoint: 'https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 110 |
model = HfApiModel(
|
| 111 |
max_tokens=2096, # Maximum number of tokens in the response
|
| 112 |
temperature=0.5, # Controls randomness: lower = more deterministic
|
| 113 |
-
model_id='
|
| 114 |
custom_role_conversions=None,
|
| 115 |
)
|
| 116 |
print("✓ Model configured successfully")
|
| 117 |
|
| 118 |
-
|
| 119 |
# LOAD PROMPT TEMPLATES
|
|
|
|
| 120 |
print("Loading prompt templates...")
|
| 121 |
# Create an upload directory if it doesn't exist
|
| 122 |
os.makedirs("uploads", exist_ok=True)
|
|
@@ -131,8 +153,9 @@ except Exception as e:
|
|
| 131 |
# Provide default empty templates if loading fails
|
| 132 |
prompt_templates = {}
|
| 133 |
|
| 134 |
-
|
| 135 |
# AGENT CONFIGURATION
|
|
|
|
| 136 |
print("Configuring agent...")
|
| 137 |
agent = CodeAgent(
|
| 138 |
model=model,
|
|
@@ -153,7 +176,9 @@ agent = CodeAgent(
|
|
| 153 |
)
|
| 154 |
print("✓ Agent configured successfully")
|
| 155 |
|
|
|
|
| 156 |
# LAUNCH THE GRADIO UI
|
|
|
|
| 157 |
print("Launching Gradio UI...")
|
| 158 |
# Start the Gradio interface with our configured agent and file upload directory
|
| 159 |
# Remove the debug parameter as it's already set in the GradioUI.launch method
|
|
|
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
+
#############################################################
|
| 15 |
+
# TOOL 1: TIME ZONE TOOL
|
| 16 |
+
#############################################################
|
| 17 |
@tool
|
| 18 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 19 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
+
#############################################################
|
| 38 |
+
# TOOL 2: IMAGE GENERATION TOOL
|
| 39 |
+
#############################################################
|
| 40 |
@tool
|
| 41 |
def generate_image_from_text(prompt: str) -> str:
|
| 42 |
+
"""A tool that generates an image based on a text description and saves it to a file.
|
| 43 |
Args:
|
| 44 |
prompt: A detailed text description of the image you want to generate.
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
+
import os
|
| 48 |
+
import uuid
|
| 49 |
+
from datetime import datetime
|
| 50 |
+
|
| 51 |
+
# Create images directory if it doesn't exist
|
| 52 |
+
os.makedirs("uploads/images", exist_ok=True)
|
| 53 |
+
|
| 54 |
+
# Generate a unique filename
|
| 55 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 56 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 57 |
+
filename = f"uploads/images/{timestamp}_{unique_id}.jpg"
|
| 58 |
+
|
| 59 |
+
# Call the image generation tool to get the PIL image
|
| 60 |
+
pil_image = image_generation_tool(prompt)
|
| 61 |
+
|
| 62 |
+
# Save the PIL image to file
|
| 63 |
+
pil_image.save(filename)
|
| 64 |
|
| 65 |
+
# Return the file path
|
| 66 |
+
return filename
|
| 67 |
except Exception as e:
|
| 68 |
# Handle any errors that occur during image generation
|
| 69 |
return f"Error generating image: {str(e)}"
|
| 70 |
|
| 71 |
+
#############################################################
|
| 72 |
+
# TOOL 3: WEB SEARCH TOOL - Using the existing DuckDuckGoSearchTool class
|
| 73 |
+
#############################################################
|
| 74 |
# Initialize the DuckDuckGo search tool from the tools directory
|
| 75 |
web_search_tool = DuckDuckGoSearchTool()
|
| 76 |
|
|
|
|
| 90 |
# Handle any errors that occur during the search
|
| 91 |
return f"Error searching the web: {str(e)}"
|
| 92 |
|
| 93 |
+
#############################################################
|
| 94 |
# FINAL ANSWER TOOL (REQUIRED)
|
| 95 |
+
#############################################################
|
| 96 |
# Initialize the final answer tool - required for the agent to provide final answers
|
| 97 |
final_answer = FinalAnswerTool()
|
| 98 |
|
| 99 |
+
#############################################################
|
| 100 |
# VISIT WEBPAGE TOOL - From tools directory
|
| 101 |
+
#############################################################
|
| 102 |
# Initialize the visit webpage tool
|
| 103 |
visit_webpage = VisitWebpageTool()
|
| 104 |
|
| 105 |
+
#############################################################
|
| 106 |
# TOOL INITIALIZATION AND VERIFICATION
|
| 107 |
+
#############################################################
|
| 108 |
# Ensure all tools are properly loaded and print status
|
| 109 |
print("Initializing tools...")
|
| 110 |
try:
|
|
|
|
| 123 |
print("✓ Visit webpage tool initialized")
|
| 124 |
print("✓ Final answer tool initialized")
|
| 125 |
|
| 126 |
+
#############################################################
|
| 127 |
# MODEL CONFIGURATION
|
| 128 |
+
#############################################################
|
| 129 |
print("Configuring model...")
|
| 130 |
+
# Use the alternative endpoint since the primary one seems to have authentication issues
|
|
|
|
| 131 |
model = HfApiModel(
|
| 132 |
max_tokens=2096, # Maximum number of tokens in the response
|
| 133 |
temperature=0.5, # Controls randomness: lower = more deterministic
|
| 134 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', # Alternative endpoint
|
| 135 |
custom_role_conversions=None,
|
| 136 |
)
|
| 137 |
print("✓ Model configured successfully")
|
| 138 |
|
| 139 |
+
#############################################################
|
| 140 |
# LOAD PROMPT TEMPLATES
|
| 141 |
+
#############################################################
|
| 142 |
print("Loading prompt templates...")
|
| 143 |
# Create an upload directory if it doesn't exist
|
| 144 |
os.makedirs("uploads", exist_ok=True)
|
|
|
|
| 153 |
# Provide default empty templates if loading fails
|
| 154 |
prompt_templates = {}
|
| 155 |
|
| 156 |
+
#############################################################
|
| 157 |
# AGENT CONFIGURATION
|
| 158 |
+
#############################################################
|
| 159 |
print("Configuring agent...")
|
| 160 |
agent = CodeAgent(
|
| 161 |
model=model,
|
|
|
|
| 176 |
)
|
| 177 |
print("✓ Agent configured successfully")
|
| 178 |
|
| 179 |
+
#############################################################
|
| 180 |
# LAUNCH THE GRADIO UI
|
| 181 |
+
#############################################################
|
| 182 |
print("Launching Gradio UI...")
|
| 183 |
# Start the Gradio interface with our configured agent and file upload directory
|
| 184 |
# Remove the debug parameter as it's already set in the GradioUI.launch method
|