Spaces:
Sleeping
Sleeping
Update app.py
Browse filesMade adjustments to run the Pizza Agent using Gradio.
app.py
CHANGED
|
@@ -4,26 +4,31 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
-
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def pizza_finder(pizza:str, find:int)-> str:
|
| 13 |
-
|
| 14 |
-
"""A tool that finds the best pizza in a city.
|
| 15 |
Args:
|
| 16 |
-
pizza: A string representing the
|
| 17 |
-
find:
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
"""A tool that fetches the current local time in a specified timezone.
|
| 25 |
Args:
|
| 26 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
# Create timezone object
|
|
@@ -34,17 +39,25 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 41 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 42 |
|
| 43 |
model = HfApiModel(
|
| 44 |
-
max_tokens=2096,
|
| 45 |
-
temperature=0.5,
|
| 46 |
-
model_id='
|
| 47 |
-
custom_role_conversions=None,
|
| 48 |
)
|
| 49 |
|
| 50 |
|
|
@@ -56,7 +69,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 56 |
|
| 57 |
agent = CodeAgent(
|
| 58 |
model=model,
|
| 59 |
-
tools=[
|
| 60 |
max_steps=6,
|
| 61 |
verbosity_level=1,
|
| 62 |
grammar=None,
|
|
@@ -66,5 +79,39 @@ agent = CodeAgent(
|
|
| 66 |
prompt_templates=prompt_templates
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import gradio as gr
|
|
|
|
| 8 |
|
| 9 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 10 |
@tool
|
| 11 |
+
def pizza_finder(pizza: str, find: int) -> str:
|
| 12 |
+
"""A tool that finds the best pizza in a city.
|
|
|
|
| 13 |
Args:
|
| 14 |
+
pizza: A string representing the pizza type to find (e.g., 'Margherita').
|
| 15 |
+
find: An integer representing the number of pizza places to find.
|
| 16 |
+
Returns:
|
| 17 |
+
str: A string describing the pizza places found.
|
| 18 |
"""
|
| 19 |
+
try:
|
| 20 |
+
# This is a dummy implementation - replace with actual pizza finding logic
|
| 21 |
+
return f"Found {find} places serving {pizza} pizza"
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Error finding pizza: {str(e)}"
|
| 24 |
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
"""A tool that fetches the current local time in a specified timezone.
|
| 28 |
Args:
|
| 29 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 30 |
+
Returns:
|
| 31 |
+
str: A string containing the current time in the specified timezone.
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
# Create timezone object
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 41 |
|
| 42 |
+
@tool
|
| 43 |
+
def final_answer_wrapper(answer: str) -> str:
|
| 44 |
+
"""Tool to provide the final answer
|
| 45 |
+
Args:
|
| 46 |
+
answer: The final answer to return
|
| 47 |
+
Returns:
|
| 48 |
+
str: The final answer
|
| 49 |
+
"""
|
| 50 |
+
final_answer_tool = FinalAnswerTool()
|
| 51 |
+
return final_answer_tool(answer)
|
| 52 |
|
| 53 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 54 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 55 |
|
| 56 |
model = HfApiModel(
|
| 57 |
+
max_tokens=2096,
|
| 58 |
+
temperature=0.5,
|
| 59 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', # Changed to the alternative endpoint
|
| 60 |
+
custom_role_conversions=None,
|
| 61 |
)
|
| 62 |
|
| 63 |
|
|
|
|
| 69 |
|
| 70 |
agent = CodeAgent(
|
| 71 |
model=model,
|
| 72 |
+
tools=[final_answer_wrapper, pizza_finder, get_current_time_in_timezone, image_generation_tool],
|
| 73 |
max_steps=6,
|
| 74 |
verbosity_level=1,
|
| 75 |
grammar=None,
|
|
|
|
| 79 |
prompt_templates=prompt_templates
|
| 80 |
)
|
| 81 |
|
| 82 |
+
def create_interface():
|
| 83 |
+
# Create a Gradio Blocks interface
|
| 84 |
+
with gr.Blocks() as interface:
|
| 85 |
+
# Add a title
|
| 86 |
+
gr.Markdown("# AI Agent Chat")
|
| 87 |
+
# Add a chatbot component to show conversation
|
| 88 |
+
chatbot = gr.Chatbot()
|
| 89 |
+
# Add a textbox for user input
|
| 90 |
+
msg = gr.Textbox()
|
| 91 |
+
# Add a clear button
|
| 92 |
+
clear = gr.Button("Clear")
|
| 93 |
+
|
| 94 |
+
# Function to handle user input
|
| 95 |
+
def user(user_message, history):
|
| 96 |
+
return "", history + [[user_message, None]]
|
| 97 |
+
|
| 98 |
+
# Function to get agent's response
|
| 99 |
+
def bot(history):
|
| 100 |
+
user_message = history[-1][0]
|
| 101 |
+
response = agent.run(user_message)
|
| 102 |
+
history[-1][1] = response
|
| 103 |
+
return history
|
| 104 |
+
|
| 105 |
+
# Set up the interaction flow
|
| 106 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 107 |
+
bot, chatbot, chatbot
|
| 108 |
+
)
|
| 109 |
+
# Set up clear button functionality
|
| 110 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 111 |
+
|
| 112 |
+
return interface
|
| 113 |
|
| 114 |
+
# Launch the interface
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
interface = create_interface()
|
| 117 |
+
interface.launch()
|