YoussefSharawy91 commited on
Commit
ba07d49
·
verified ·
1 Parent(s): 51ce582

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -43
app.py CHANGED
@@ -1,14 +1,20 @@
 
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
2
  import datetime
3
  import pytz
4
  import yaml
5
  import requests
6
- import time
7
  import base64
8
  from io import BytesIO
9
  from PIL import Image
10
  from tools.final_answer import FinalAnswerTool
11
  from Gradio_UI import GradioUI
 
 
 
 
 
12
 
13
  # --- Define Tools ---
14
 
@@ -61,7 +67,7 @@ def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> s
61
  favorite_theme: Your favored narrative theme (e.g., adventure, mystery).
62
 
63
  Returns:
64
- A summary that shapes your personalized tale.
65
  """
66
  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."
67
 
@@ -71,62 +77,36 @@ def generate_studio_ghibli_image(prompt: str) -> str:
71
  Generates a Studio Ghibli style image based on the provided prompt.
72
 
73
  Args:
74
- prompt: The text prompt describing the desired image.
75
 
76
  Returns:
77
- A base64 encoded PNG image string representing the generated image.
78
  """
79
- repo = "artificialguybr/StudioGhibli.Redmond-V2"
80
- trigger_word = "Studio Ghibli, StdGBRedmAF"
81
- api_url = f"https://api-inference.huggingface.co/models/{repo}"
82
- # Optionally, set up your Hugging Face token in the environment and uncomment below:
83
- # token = os.getenv("API_TOKEN")
84
- headers = {
85
- # "Authorization": f"Bearer {token}"
86
- }
87
- full_prompt = f"{prompt} {trigger_word}"
88
- payload = {
89
- "inputs": full_prompt,
90
- "parameters": {
91
- "negative_prompt": "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (watermark, signature, text font, username, error, logo, words, letters, digits, autograph, trademark, name:1.2), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate, (airbrushed, cartoon, anime, semi-realistic, cgi, render, blender, digital art, manga, amateur:1.3), (3D ,3D Game, 3D Game Scene, 3D Character:1.1), (bad hands, bad anatomy, bad body, bad face, bad teeth, bad arms, bad legs, deformities:1.3)",
92
- "num_inference_steps": 30,
93
- "scheduler": "DPMSolverMultistepScheduler"
94
- },
95
- }
96
- error_count = 0
97
- while True:
98
- response = requests.post(api_url, headers=headers, json=payload)
99
- if response.status_code == 200:
100
- img = Image.open(BytesIO(response.content))
101
- buffered = BytesIO()
102
- img.save(buffered, format="PNG")
103
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
104
- return img_str
105
- elif response.status_code == 503:
106
- time.sleep(1)
107
- elif response.status_code == 500 and error_count < 5:
108
- time.sleep(1)
109
- error_count += 1
110
- else:
111
- raise Exception(f"API Error: {response.status_code}")
112
 
113
  @tool
114
  def offer_interactive_options(scene: str) -> str:
115
  """
116
- Provides interactive options for the next step in your adventure based on the scene.
117
 
118
  Args:
119
- scene: A detailed description of the current scene.
120
 
121
  Returns:
122
- A string listing numbered interactive options.
123
  """
124
  return (
125
  "Options:\n"
126
  "1. Follow the mysterious figure in the distance.\n"
127
  "2. Wander deeper into the enchanted landscape.\n"
128
  "3. Approach a peculiar, glowing object.\n"
129
- "4. Ask for more secrets of the scene.\n"
130
  "Please enter the number of your choice."
131
  )
132
 
@@ -176,7 +156,7 @@ agent = CodeAgent(
176
  name="Ghibili Interactive Tale",
177
  description=(
178
  "Ghibili Interactive Tale is your creative partner on a magical journey. "
179
- "We will co-author a story as enchanting as a classic tale, with every scene painted in Studio Ghibli style. "
180
  "I begin by reflecting on your current time and mood, ask for your location to capture the weather, "
181
  "and then weave a narrative filled with vivid imagery and interactive choices. Let's create magic together!"
182
  ),
@@ -193,7 +173,28 @@ initial_opener = (
193
  "What kind of tale are you yearning for today—a whimsical fairy tale, a mysterious quest, or a serene escape? "
194
  "Your story awaits, and I am excited to walk beside you every step of the way. 📖✨"
195
  )
196
- agent.conversation = [{"role": "Storyteller", "content": initial_opener}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  # --- Launch the Interactive UI ---
199
  GradioUI(agent).launch()
 
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
 
 
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
 
 
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:
95
  """
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
 
 
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
  ),
 
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()