Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import requests
|
|
| 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,13 +21,14 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 20 |
"""
|
| 21 |
return "What magic will you build?"
|
| 22 |
|
|
|
|
| 23 |
@tool
|
| 24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
"""
|
| 26 |
Fetches the current local time in a specified timezone.
|
| 27 |
|
| 28 |
Args:
|
| 29 |
-
timezone: A
|
| 30 |
|
| 31 |
Returns:
|
| 32 |
A string with the current local time.
|
|
@@ -38,28 +40,30 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 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 |
"""
|
| 44 |
-
Captures
|
| 45 |
|
| 46 |
Args:
|
| 47 |
-
likes: Elements
|
| 48 |
-
dislikes: Elements
|
| 49 |
-
favorite_theme:
|
| 50 |
|
| 51 |
Returns:
|
| 52 |
-
A summary string to
|
| 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 |
"""
|
| 59 |
Generates a Studio Ghibli style image based on the provided text prompt.
|
| 60 |
|
| 61 |
Args:
|
| 62 |
-
prompt: The text prompt
|
| 63 |
|
| 64 |
Returns:
|
| 65 |
A URL or identifier for the generated image.
|
|
@@ -74,18 +78,43 @@ def generate_studio_ghibli_image(prompt: str) -> str:
|
|
| 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,7 +123,8 @@ agent = CodeAgent(
|
|
| 94 |
get_current_time_in_timezone,
|
| 95 |
probe_story_preferences,
|
| 96 |
generate_studio_ghibli_image,
|
| 97 |
-
|
|
|
|
| 98 |
],
|
| 99 |
max_steps=6,
|
| 100 |
verbosity_level=1,
|
|
@@ -102,11 +132,12 @@ agent = CodeAgent(
|
|
| 102 |
planning_interval=None,
|
| 103 |
name="Ghibili Interactive Tale",
|
| 104 |
description=(
|
| 105 |
-
"A highly interactive and
|
| 106 |
-
"It
|
| 107 |
-
"Enjoy a seamless blend of
|
| 108 |
),
|
| 109 |
prompt_templates=prompt_templates
|
| 110 |
)
|
| 111 |
|
|
|
|
| 112 |
GradioUI(agent).launch()
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# 1. A placeholder tool for future creativity.
|
| 10 |
@tool
|
| 11 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 12 |
"""
|
|
|
|
| 21 |
"""
|
| 22 |
return "What magic will you build?"
|
| 23 |
|
| 24 |
+
# 2. A tool to get the current local time for a given timezone.
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
"""
|
| 28 |
Fetches the current local time in a specified timezone.
|
| 29 |
|
| 30 |
Args:
|
| 31 |
+
timezone: A valid timezone string (e.g., 'America/New_York').
|
| 32 |
|
| 33 |
Returns:
|
| 34 |
A string with the current local time.
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 42 |
|
| 43 |
+
# 3. A tool to capture your story preferences.
|
| 44 |
@tool
|
| 45 |
def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> str:
|
| 46 |
"""
|
| 47 |
+
Captures your story preferences and returns a personalized summary.
|
| 48 |
|
| 49 |
Args:
|
| 50 |
+
likes: Elements you love in stories.
|
| 51 |
+
dislikes: Elements you dislike.
|
| 52 |
+
favorite_theme: Your preferred story theme (e.g., adventure, mystery).
|
| 53 |
|
| 54 |
Returns:
|
| 55 |
+
A summary string to shape your personalized narrative.
|
| 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 |
+
# 4. A tool that generates a Studio Ghibli–style image.
|
| 60 |
@tool
|
| 61 |
def generate_studio_ghibli_image(prompt: str) -> str:
|
| 62 |
"""
|
| 63 |
Generates a Studio Ghibli style image based on the provided text prompt.
|
| 64 |
|
| 65 |
Args:
|
| 66 |
+
prompt: The text prompt describing the image.
|
| 67 |
|
| 68 |
Returns:
|
| 69 |
A URL or identifier for the generated image.
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error generating image: {str(e)}"
|
| 80 |
|
| 81 |
+
# 5. A tool to offer interactive options based on the current scene.
|
| 82 |
+
@tool
|
| 83 |
+
def offer_interactive_options(scene: str) -> str:
|
| 84 |
+
"""
|
| 85 |
+
Provides interactive options for the next step in your adventure based on the scene description.
|
| 86 |
+
|
| 87 |
+
Args:
|
| 88 |
+
scene: The description of the current scene.
|
| 89 |
+
|
| 90 |
+
Returns:
|
| 91 |
+
A string listing numbered options for you to choose from.
|
| 92 |
+
"""
|
| 93 |
+
return (
|
| 94 |
+
"Options:\n"
|
| 95 |
+
"1. Follow the mysterious character that appears in the scene.\n"
|
| 96 |
+
"2. Explore the magical surroundings further.\n"
|
| 97 |
+
"3. Interact with a unique object you notice.\n"
|
| 98 |
+
"4. Ask for more details about the scene.\n"
|
| 99 |
+
"Please enter the number of your choice."
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# 6. The final answer tool that consolidates the agent’s response.
|
| 103 |
final_answer = FinalAnswerTool()
|
| 104 |
|
| 105 |
+
# 7. Set up the language model.
|
| 106 |
model = HfApiModel(
|
| 107 |
max_tokens=2096,
|
| 108 |
temperature=0.5,
|
| 109 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # Use an alternative endpoint if necessary.
|
| 110 |
custom_role_conversions=None,
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# 8. Load prompt templates from a YAML file.
|
| 114 |
with open("prompts.yaml", "r") as stream:
|
| 115 |
prompt_templates = yaml.safe_load(stream)
|
| 116 |
|
| 117 |
+
# 9. Create the interactive agent.
|
| 118 |
agent = CodeAgent(
|
| 119 |
model=model,
|
| 120 |
tools=[
|
|
|
|
| 123 |
get_current_time_in_timezone,
|
| 124 |
probe_story_preferences,
|
| 125 |
generate_studio_ghibli_image,
|
| 126 |
+
offer_interactive_options,
|
| 127 |
+
DuckDuckGoSearchTool(), # Optional: for additional web-based info.
|
| 128 |
],
|
| 129 |
max_steps=6,
|
| 130 |
verbosity_level=1,
|
|
|
|
| 132 |
planning_interval=None,
|
| 133 |
name="Ghibili Interactive Tale",
|
| 134 |
description=(
|
| 135 |
+
"A highly interactive and dynamic agent that co-creates a personalized Studio Ghibli-inspired adventure. "
|
| 136 |
+
"It engages you by asking probing questions about your preferences, crafts vivid narrative scenes complete with enchanting imagery, "
|
| 137 |
+
"and offers interactive choices at every step to shape the story. Enjoy a seamless blend of storytelling, art, and creativity!"
|
| 138 |
),
|
| 139 |
prompt_templates=prompt_templates
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# 10. Launch the Gradio web interface to interact with the agent.
|
| 143 |
GradioUI(agent).launch()
|