YoussefSharawy91 commited on
Commit
0afe4a9
·
verified ·
1 Parent(s): ba07d49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -74
app.py CHANGED
@@ -1,20 +1,11 @@
1
- import threading
2
- import time
3
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
4
  import datetime
5
  import pytz
6
  import yaml
7
  import requests
8
- import base64
9
- from io import BytesIO
10
- from PIL import Image
11
  from tools.final_answer import FinalAnswerTool
12
  from Gradio_UI import GradioUI
13
- from huggingface_hub import InferenceClient
14
-
15
- # --- Set up the Inference Client for Image Generation ---
16
- # This client calls the Studio Ghibli Image Generator V2 model.
17
- image_client = InferenceClient(model_id="artificialguybr/StudioGhibli.Redmond-V2")
18
 
19
  # --- Define Tools ---
20
 
@@ -51,44 +42,40 @@ def get_current_time_in_timezone(timezone: str) -> str:
51
  if hour < 6 or hour >= 22:
52
  comment = "🌙 It's late, and the night whispers secrets. What dreams keep you awake?"
53
  else:
54
- comment = "☀️ The day unfolds with gentle light. Where are you basking in this beautiful moment?"
55
  return f"The current local time in {timezone} is: {local_time_str}. {comment}"
56
  except Exception as e:
57
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
58
 
59
  @tool
60
- def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> str:
61
  """
62
- Captures your story preferences and returns a poetic summary.
63
 
64
  Args:
65
- likes: What you cherish in stories.
66
- dislikes: What you prefer to leave behind.
67
- favorite_theme: Your favored narrative theme (e.g., adventure, mystery).
68
 
69
  Returns:
70
- A summary that helps weave your personalized tale.
71
  """
72
- return f"Splendid! You delight in {likes} 🌸, wish to steer clear of {dislikes} 🚫, and are enchanted by {favorite_theme} themes. Let us craft your story accordingly."
 
 
73
 
74
  @tool
75
- def generate_studio_ghibli_image(prompt: str) -> str:
76
  """
77
- Generates a Studio Ghibli style image based on the provided prompt.
78
 
79
  Args:
80
- prompt: The descriptive prompt for the desired image.
 
 
81
 
82
  Returns:
83
- A base64-encoded PNG string representing the generated image.
84
  """
85
- try:
86
- # Call the image-generation API with our prompt and desired parameters.
87
- result = image_client.image_generation(prompt, parameters={"width": 1024, "height": 1024, "prompt": prompt})
88
- # Assume result is a base64 string or a URL; here we return it directly.
89
- return result
90
- except Exception as e:
91
- return f"Error generating image: {str(e)}"
92
 
93
  @tool
94
  def offer_interactive_options(scene: str) -> str:
@@ -96,33 +83,20 @@ def offer_interactive_options(scene: str) -> str:
96
  Provides interactive options based on the current scene description.
97
 
98
  Args:
99
- scene: The detailed description of the current scene.
100
 
101
  Returns:
102
- A string listing interactive options.
103
  """
104
  return (
105
  "Options:\n"
106
  "1. Follow the mysterious figure in the distance.\n"
107
  "2. Wander deeper into the enchanted landscape.\n"
108
- "3. Approach a peculiar, glowing object.\n"
109
- "4. Inquire for more secrets of the scene.\n"
110
  "Please enter the number of your choice."
111
  )
112
 
113
- @tool
114
- def get_weather(location: str) -> str:
115
- """
116
- Simulates fetching poetic weather information for a given location.
117
-
118
- Args:
119
- location: The name of your location.
120
-
121
- Returns:
122
- A poetic description of the current weather.
123
- """
124
- return f"In {location}, the weather is radiant and gentle, with soft breezes and a sky that tells tales of wonder."
125
-
126
  final_answer = FinalAnswerTool()
127
 
128
  # --- Set up the Language Model ---
@@ -144,9 +118,8 @@ agent = CodeAgent(
144
  my_custom_tool,
145
  get_current_time_in_timezone,
146
  probe_story_preferences,
147
- generate_studio_ghibli_image,
148
- offer_interactive_options,
149
  get_weather,
 
150
  DuckDuckGoSearchTool(),
151
  ],
152
  max_steps=6,
@@ -156,9 +129,9 @@ agent = CodeAgent(
156
  name="Ghibili Interactive Tale",
157
  description=(
158
  "Ghibili Interactive Tale is your creative partner on a magical journey. "
159
- "Together, we'll co-author a story as enchanting as a classic tale, with every scene painted in Studio Ghibli style. "
160
- "I begin by reflecting on your current time and mood, ask for your location to capture the weather, "
161
- "and then weave a narrative filled with vivid imagery and interactive choices. Let's create magic together!"
162
  ),
163
  prompt_templates=prompt_templates
164
  )
@@ -166,35 +139,14 @@ agent = CodeAgent(
166
  # --- Set an Initial Opener with a Storybook-Like Tone and Emojis ---
167
  initial_opener = (
168
  "🌟 Welcome, dear traveler, to the enchanting realm of Ghibili Interactive Tale! 🌟\n\n"
169
- "Imagine stepping into the pages of a timeless storybook, where each page is filled with wonder and magic. "
170
  "I am here to be your guide and creative partner on this journey, crafting a narrative that reflects your dreams and desires.\n\n"
171
- "To begin, please share your timezone (for example, 'America/New_York') so I may whisper a gentle comment on the hour. "
172
  "Then, tell me your location so we can capture the weather and set the perfect scene for our adventure.\n\n"
173
  "What kind of tale are you yearning for today—a whimsical fairy tale, a mysterious quest, or a serene escape? "
174
  "Your story awaits, and I am excited to walk beside you every step of the way. 📖✨"
175
  )
176
  agent.conversation = [{"role": "assistant", "content": initial_opener}]
177
 
178
- # --- Proactive Story Loop ---
179
- # This loop proactively generates narrative segments if the user is idle.
180
- def proactive_story_loop(agent, interval=15, steps=5):
181
- for i in range(steps):
182
- # Simulate generation of the next narrative segment.
183
- # Here, we assume that `agent.generate_next_message()` generates a new story segment based on the current conversation.
184
- try:
185
- new_segment = agent.generate_next_message()
186
- except Exception as e:
187
- new_segment = f"(Error generating next message: {e})"
188
- if new_segment:
189
- agent.conversation.append({"role": "assistant", "content": new_segment})
190
- time.sleep(interval)
191
- # After the narrative loop, generate a final image representing the culmination of the story.
192
- image_prompt = "A breathtaking final scene that encapsulates the magical journey we have created together, in vivid Studio Ghibli style."
193
- final_image = generate_studio_ghibli_image(image_prompt)
194
- agent.conversation.append({"role": "assistant", "content": f"Here is the final image of our adventure: {final_image}"})
195
-
196
- # Start the proactive story loop in a background thread.
197
- threading.Thread(target=proactive_story_loop, args=(agent, 15, 5), daemon=True).start()
198
-
199
  # --- Launch the Interactive UI ---
200
  GradioUI(agent).launch()
 
 
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
2
  import datetime
3
  import pytz
4
  import yaml
5
  import requests
6
+ import time
 
 
7
  from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
 
 
 
 
 
9
 
10
  # --- Define Tools ---
11
 
 
42
  if hour < 6 or hour >= 22:
43
  comment = "🌙 It's late, and the night whispers secrets. What dreams keep you awake?"
44
  else:
45
+ comment = "☀️ The day unfolds with gentle light. Where are you enjoying this beautiful moment?"
46
  return f"The current local time in {timezone} is: {local_time_str}. {comment}"
47
  except Exception as e:
48
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
49
 
50
  @tool
51
+ def get_weather(location: str) -> str:
52
  """
53
+ Simulates fetching poetic weather information for a given location.
54
 
55
  Args:
56
+ location: The name of your location.
 
 
57
 
58
  Returns:
59
+ A poetic description of the current weather.
60
  """
61
+ # In a real implementation, this tool would call a weather API.
62
+ # For now, we'll return a simulated weather description.
63
+ return f"In {location}, the weather is radiant and gentle, with soft breezes and a sky that whispers tales of wonder."
64
 
65
  @tool
66
+ def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> str:
67
  """
68
+ Captures your story preferences and returns a poetic summary.
69
 
70
  Args:
71
+ likes: What you cherish in stories.
72
+ dislikes: What you prefer to leave behind.
73
+ favorite_theme: Your favored narrative theme (e.g., adventure, mystery).
74
 
75
  Returns:
76
+ A summary that helps shape your personalized narrative.
77
  """
78
+ return f"Splendid! You delight in {likes} 🌸, wish to steer clear of {dislikes} 🚫, and are enchanted by {favorite_theme} themes. Let us craft your story accordingly."
 
 
 
 
 
 
79
 
80
  @tool
81
  def offer_interactive_options(scene: str) -> str:
 
83
  Provides interactive options based on the current scene description.
84
 
85
  Args:
86
+ scene: A detailed description of the current scene.
87
 
88
  Returns:
89
+ A string listing interactive options for the next step.
90
  """
91
  return (
92
  "Options:\n"
93
  "1. Follow the mysterious figure in the distance.\n"
94
  "2. Wander deeper into the enchanted landscape.\n"
95
+ "3. Approach a peculiar object you notice.\n"
96
+ "4. Ask for more details about the scene.\n"
97
  "Please enter the number of your choice."
98
  )
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  final_answer = FinalAnswerTool()
101
 
102
  # --- Set up the Language Model ---
 
118
  my_custom_tool,
119
  get_current_time_in_timezone,
120
  probe_story_preferences,
 
 
121
  get_weather,
122
+ offer_interactive_options,
123
  DuckDuckGoSearchTool(),
124
  ],
125
  max_steps=6,
 
129
  name="Ghibili Interactive Tale",
130
  description=(
131
  "Ghibili Interactive Tale is your creative partner on a magical journey. "
132
+ "I will guide you through a personalized adventure by reflecting on your current time and mood, "
133
+ "asking about your location to capture the weather, and using your preferences to craft a captivating narrative. "
134
+ "Let's begin this interactive story together!"
135
  ),
136
  prompt_templates=prompt_templates
137
  )
 
139
  # --- Set an Initial Opener with a Storybook-Like Tone and Emojis ---
140
  initial_opener = (
141
  "🌟 Welcome, dear traveler, to the enchanting realm of Ghibili Interactive Tale! 🌟\n\n"
142
+ "Imagine stepping into the pages of a timeless storybook, where every moment is filled with wonder and magic. "
143
  "I am here to be your guide and creative partner on this journey, crafting a narrative that reflects your dreams and desires.\n\n"
144
+ "To begin, please share your timezone (for example, 'America/New_York') so I may gently comment on the hour. "
145
  "Then, tell me your location so we can capture the weather and set the perfect scene for our adventure.\n\n"
146
  "What kind of tale are you yearning for today—a whimsical fairy tale, a mysterious quest, or a serene escape? "
147
  "Your story awaits, and I am excited to walk beside you every step of the way. 📖✨"
148
  )
149
  agent.conversation = [{"role": "assistant", "content": initial_opener}]
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  # --- Launch the Interactive UI ---
152
  GradioUI(agent).launch()