dlaima commited on
Commit
f9601e0
·
verified ·
1 Parent(s): 1e0699c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -58
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
- # ----- Tools -----
9
  @tool
10
  def my_custom_tool(arg1: str, arg2: int) -> str:
11
  """
12
- Creates a personalized greeting repeated multiple times.
13
-
14
  Args:
15
- arg1 (str): The name or phrase to greet.
16
- arg2 (int): The number of times to repeat the greeting.
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
- Fetches the current local time in a specified timezone.
28
-
29
  Args:
30
- timezone (str): A valid timezone string (e.g., 'America/New_York').
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
- # Load prompt templates
 
 
45
  with open("prompts.yaml", 'r') as stream:
46
  prompt_templates = yaml.safe_load(stream)
47
 
48
- # Load additional tools from Hugging Face Hub
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 Query Function -----
85
- def agent_response(api_key: str, user_query: str):
86
- """
87
- Responds to a user query using the CodeAgent initialized with the API key.
88
-
89
- Args:
90
- api_key (str): OpenAI API key provided by the user.
91
- user_query (str): The user's query for the agent.
92
-
93
- Returns:
94
- str: Agent's response.
95
- """
96
- agent = initialize_agent(api_key)
97
- return agent.run(user_query) # Run the agent on the user's query
98
-
99
- # ----- Gradio Interface -----
100
- iface = gr.Interface(
101
- fn=agent_response,
102
- inputs=[
103
- gr.Textbox(label="Enter your OpenAI API Key", type="password"),
104
- gr.Textbox(label="Your Query", placeholder="Type your question here...")
105
- ],
106
- outputs=gr.Textbox(label="Agent Response"),
107
- title="Custom SmolAgent App",
108
- description="Enter your OpenAI API key and ask a question to the agent."
109
- )
110
-
111
- iface.launch()
 
 
 
 
 
 
 
 
 
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()