Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,30 @@ import gradio as gr
|
|
| 8 |
# ----- Tools -----
|
| 9 |
@tool
|
| 10 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
greeting = f"Hello, {arg1}!"
|
| 13 |
return " ".join([greeting] * arg2)
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 17 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
tz = pytz.timezone(timezone)
|
| 20 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -31,14 +48,23 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 31 |
# Load additional tools from Hugging Face Hub
|
| 32 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 33 |
|
| 34 |
-
# -----
|
| 35 |
def initialize_agent(api_key: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
model = OpenAIServerModel(
|
| 37 |
max_tokens=2096,
|
| 38 |
temperature=0.5,
|
| 39 |
model_id="gpt-4o",
|
| 40 |
custom_role_conversions=None,
|
| 41 |
-
openai_api_key=api_key
|
| 42 |
)
|
| 43 |
|
| 44 |
agent = CodeAgent(
|
|
@@ -55,12 +81,21 @@ def initialize_agent(api_key: str):
|
|
| 55 |
|
| 56 |
return agent
|
| 57 |
|
| 58 |
-
# ----- Gradio
|
| 59 |
def launch_agent(api_key: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
agent = initialize_agent(api_key)
|
| 61 |
return GradioUI(agent).launch(share=True)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
iface = gr.Interface(
|
| 65 |
fn=launch_agent,
|
| 66 |
inputs=gr.Textbox(label="Enter your OpenAI API Key", type="password"),
|
|
|
|
| 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)
|
| 37 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 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,
|
| 65 |
model_id="gpt-4o",
|
| 66 |
custom_role_conversions=None,
|
| 67 |
+
openai_api_key=api_key
|
| 68 |
)
|
| 69 |
|
| 70 |
agent = CodeAgent(
|
|
|
|
| 81 |
|
| 82 |
return agent
|
| 83 |
|
| 84 |
+
# ----- Gradio Interface -----
|
| 85 |
def launch_agent(api_key: str):
|
| 86 |
+
"""
|
| 87 |
+
Launches the Gradio interface for the CodeAgent after receiving the API key.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
api_key (str): User's OpenAI API key.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
None
|
| 94 |
+
"""
|
| 95 |
agent = initialize_agent(api_key)
|
| 96 |
return GradioUI(agent).launch(share=True)
|
| 97 |
|
| 98 |
+
# Gradio input for API key
|
| 99 |
iface = gr.Interface(
|
| 100 |
fn=launch_agent,
|
| 101 |
inputs=gr.Textbox(label="Enter your OpenAI API Key", type="password"),
|