Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import requests
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
-
# A simple custom tool (feel free to modify for extra magic!)
|
| 10 |
@tool
|
| 11 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 12 |
"""
|
|
@@ -21,7 +20,6 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 21 |
"""
|
| 22 |
return "What magic will you build?"
|
| 23 |
|
| 24 |
-
# Tool to get the current local time in a given timezone.
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
"""
|
|
@@ -40,7 +38,6 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 42 |
|
| 43 |
-
# Tool to probe the user's story preferences.
|
| 44 |
@tool
|
| 45 |
def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> str:
|
| 46 |
"""
|
|
@@ -56,7 +53,6 @@ def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> s
|
|
| 56 |
"""
|
| 57 |
return f"Noted! You enjoy {likes}, prefer to avoid {dislikes}, and love {favorite_theme} themes. Let's weave these into your tale!"
|
| 58 |
|
| 59 |
-
# New tool to generate a Studio Ghibli style image by calling the /predict endpoint.
|
| 60 |
@tool
|
| 61 |
def generate_studio_ghibli_image(prompt: str) -> str:
|
| 62 |
"""
|
|
@@ -74,28 +70,22 @@ def generate_studio_ghibli_image(prompt: str) -> str:
|
|
| 74 |
response = requests.post(url, json=payload)
|
| 75 |
response.raise_for_status()
|
| 76 |
result = response.json()
|
| 77 |
-
# The API is expected to return a list in its JSON response.
|
| 78 |
-
# Adjust the indexing based on the actual output structure.
|
| 79 |
return result[0] if result and isinstance(result, list) else "No image output received."
|
| 80 |
except Exception as e:
|
| 81 |
return f"Error generating image: {str(e)}"
|
| 82 |
|
| 83 |
-
# The final answer tool is required to deliver the conclusive response.
|
| 84 |
final_answer = FinalAnswerTool()
|
| 85 |
|
| 86 |
-
# Set up the LLM model (using Qwen 2.5 Coder 32B as an example).
|
| 87 |
model = HfApiModel(
|
| 88 |
max_tokens=2096,
|
| 89 |
temperature=0.5,
|
| 90 |
-
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 91 |
custom_role_conversions=None,
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Load prompt templates from a YAML file.
|
| 95 |
with open("prompts.yaml", "r") as stream:
|
| 96 |
prompt_templates = yaml.safe_load(stream)
|
| 97 |
|
| 98 |
-
# Create the CodeAgent with an interactive narrative focus.
|
| 99 |
agent = CodeAgent(
|
| 100 |
model=model,
|
| 101 |
tools=[
|
|
@@ -104,7 +94,7 @@ agent = CodeAgent(
|
|
| 104 |
get_current_time_in_timezone,
|
| 105 |
probe_story_preferences,
|
| 106 |
generate_studio_ghibli_image,
|
| 107 |
-
DuckDuckGoSearchTool(),
|
| 108 |
],
|
| 109 |
max_steps=6,
|
| 110 |
verbosity_level=1,
|
|
@@ -119,9 +109,4 @@ agent = CodeAgent(
|
|
| 119 |
prompt_templates=prompt_templates
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
GradioUI(
|
| 124 |
-
agent,
|
| 125 |
-
title="Ghibili Interactive Tale",
|
| 126 |
-
description="Embark on a magical journey where your story comes to life with enchanting visuals and a narrative tailored just for you!"
|
| 127 |
-
).launch()
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
| 9 |
@tool
|
| 10 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
"""
|
|
|
|
| 20 |
"""
|
| 21 |
return "What magic will you build?"
|
| 22 |
|
|
|
|
| 23 |
@tool
|
| 24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
"""
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 40 |
|
|
|
|
| 41 |
@tool
|
| 42 |
def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> str:
|
| 43 |
"""
|
|
|
|
| 53 |
"""
|
| 54 |
return f"Noted! You enjoy {likes}, prefer to avoid {dislikes}, and love {favorite_theme} themes. Let's weave these into your tale!"
|
| 55 |
|
|
|
|
| 56 |
@tool
|
| 57 |
def generate_studio_ghibli_image(prompt: str) -> str:
|
| 58 |
"""
|
|
|
|
| 70 |
response = requests.post(url, json=payload)
|
| 71 |
response.raise_for_status()
|
| 72 |
result = response.json()
|
|
|
|
|
|
|
| 73 |
return result[0] if result and isinstance(result, list) else "No image output received."
|
| 74 |
except Exception as e:
|
| 75 |
return f"Error generating image: {str(e)}"
|
| 76 |
|
|
|
|
| 77 |
final_answer = FinalAnswerTool()
|
| 78 |
|
|
|
|
| 79 |
model = HfApiModel(
|
| 80 |
max_tokens=2096,
|
| 81 |
temperature=0.5,
|
| 82 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 83 |
custom_role_conversions=None,
|
| 84 |
)
|
| 85 |
|
|
|
|
| 86 |
with open("prompts.yaml", "r") as stream:
|
| 87 |
prompt_templates = yaml.safe_load(stream)
|
| 88 |
|
|
|
|
| 89 |
agent = CodeAgent(
|
| 90 |
model=model,
|
| 91 |
tools=[
|
|
|
|
| 94 |
get_current_time_in_timezone,
|
| 95 |
probe_story_preferences,
|
| 96 |
generate_studio_ghibli_image,
|
| 97 |
+
DuckDuckGoSearchTool(),
|
| 98 |
],
|
| 99 |
max_steps=6,
|
| 100 |
verbosity_level=1,
|
|
|
|
| 109 |
prompt_templates=prompt_templates
|
| 110 |
)
|
| 111 |
|
| 112 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|