Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,31 @@
|
|
| 1 |
-
from smolagents import CodeAgent, OpenAIServerModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
#
|
| 9 |
@tool
|
| 10 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
Args:
|
| 15 |
-
arg1
|
| 16 |
-
arg2
|
| 17 |
-
|
| 18 |
-
Returns:
|
| 19 |
-
str: The repeated greeting.
|
| 20 |
"""
|
| 21 |
greeting = f"Hello, {arg1}!"
|
| 22 |
return " ".join([greeting] * arg2)
|
| 23 |
|
|
|
|
| 24 |
@tool
|
| 25 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
Args:
|
| 30 |
-
timezone
|
| 31 |
-
|
| 32 |
-
Returns:
|
| 33 |
-
str: The current time in that timezone or an error message.
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
tz = pytz.timezone(timezone)
|
|
@@ -41,24 +36,14 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 41 |
|
| 42 |
final_answer = FinalAnswerTool()
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
| 45 |
with open("prompts.yaml", 'r') as stream:
|
| 46 |
prompt_templates = yaml.safe_load(stream)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 50 |
-
|
| 51 |
-
# ----- Agent Initialization -----
|
| 52 |
def initialize_agent(api_key: str):
|
| 53 |
-
"""
|
| 54 |
-
Initializes the CodeAgent with the user-provided OpenAI API key.
|
| 55 |
-
|
| 56 |
-
Args:
|
| 57 |
-
api_key (str): OpenAI API key provided by the user.
|
| 58 |
-
|
| 59 |
-
Returns:
|
| 60 |
-
CodeAgent: The initialized agent ready to use.
|
| 61 |
-
"""
|
| 62 |
model = OpenAIServerModel(
|
| 63 |
max_tokens=2096,
|
| 64 |
temperature=0.5,
|
|
@@ -78,34 +63,41 @@ def initialize_agent(api_key: str):
|
|
| 78 |
description=None,
|
| 79 |
prompt_templates=prompt_templates
|
| 80 |
)
|
| 81 |
-
|
| 82 |
return agent
|
| 83 |
|
| 84 |
-
# ----- Gradio
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
# Tool to create a personalized greeting repeated a specific number of times.
|
| 9 |
@tool
|
| 10 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
"""
|
| 12 |
+
A tool that creates a personalized greeting repeated multiple times.
|
| 13 |
+
|
| 14 |
Args:
|
| 15 |
+
arg1: the name or phrase to greet
|
| 16 |
+
arg2: the number of times to repeat the greeting
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
greeting = f"Hello, {arg1}!"
|
| 19 |
return " ".join([greeting] * arg2)
|
| 20 |
|
| 21 |
+
# Tool to get the current local time in your chosen timezone.
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
"""
|
| 25 |
+
A tool that fetches the current local time in a specified timezone.
|
| 26 |
+
|
| 27 |
Args:
|
| 28 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
| 29 |
"""
|
| 30 |
try:
|
| 31 |
tz = pytz.timezone(timezone)
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
+
# Import tool from Hub
|
| 40 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 41 |
+
|
| 42 |
with open("prompts.yaml", 'r') as stream:
|
| 43 |
prompt_templates = yaml.safe_load(stream)
|
| 44 |
|
| 45 |
+
# ----- Initialize agent with API key -----
|
|
|
|
|
|
|
|
|
|
| 46 |
def initialize_agent(api_key: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
model = OpenAIServerModel(
|
| 48 |
max_tokens=2096,
|
| 49 |
temperature=0.5,
|
|
|
|
| 63 |
description=None,
|
| 64 |
prompt_templates=prompt_templates
|
| 65 |
)
|
|
|
|
| 66 |
return agent
|
| 67 |
|
| 68 |
+
# ----- Gradio functions -----
|
| 69 |
+
# Store agent globally after API key input
|
| 70 |
+
AGENT = None
|
| 71 |
+
|
| 72 |
+
def set_api_key(api_key: str):
|
| 73 |
+
global AGENT
|
| 74 |
+
AGENT = initialize_agent(api_key)
|
| 75 |
+
return "Agent initialized! You can now enter your queries below."
|
| 76 |
+
|
| 77 |
+
def ask_agent(user_query: str):
|
| 78 |
+
global AGENT
|
| 79 |
+
if AGENT is None:
|
| 80 |
+
return "Please enter your OpenAI API key first."
|
| 81 |
+
response = AGENT.run(user_query)
|
| 82 |
+
return response
|
| 83 |
+
|
| 84 |
+
# ----- Gradio interface -----
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
gr.Markdown("## Enter your OpenAI API key and start querying the agent")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", placeholder="sk-...", type="password")
|
| 90 |
+
api_key_button = gr.Button("Initialize Agent")
|
| 91 |
+
|
| 92 |
+
init_output = gr.Textbox(label="Status", interactive=False)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
user_query_input = gr.Textbox(label="Enter your query here", placeholder="Ask something...")
|
| 96 |
+
submit_query_button = gr.Button("Submit Query")
|
| 97 |
+
|
| 98 |
+
query_output = gr.Textbox(label="Agent Response", interactive=False)
|
| 99 |
+
|
| 100 |
+
api_key_button.click(set_api_key, inputs=api_key_input, outputs=init_output)
|
| 101 |
+
submit_query_button.click(ask_agent, inputs=user_query_input, outputs=query_output)
|
| 102 |
+
|
| 103 |
+
demo.launch()
|