Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,6 @@ import yaml
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
|
| 9 |
-
from PIL import Image
|
| 10 |
-
import io
|
| 11 |
-
import base64
|
| 12 |
-
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
|
| 15 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
@@ -27,39 +23,54 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 27 |
def cartoon_story_tool(base64_image: str, story_prompt: str = "Create a 4-panel cartoon story.") -> str:
|
| 28 |
"""
|
| 29 |
Generates a cartoon-style story from an input image and optional prompt.
|
| 30 |
-
|
| 31 |
Args:
|
| 32 |
base64_image: A base64-encoded image string (input frame).
|
| 33 |
story_prompt: Optional text guiding the storyline.
|
|
|
|
|
|
|
| 34 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 36 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 37 |
|
| 38 |
-
# Decode input image
|
| 39 |
-
|
| 40 |
-
original_image = Image.open(io.BytesIO(
|
| 41 |
|
| 42 |
-
#
|
| 43 |
frames = []
|
| 44 |
-
for i in range(4): # 4
|
| 45 |
-
prompt = f"{story_prompt}
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
response = requests.post(
|
| 48 |
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",
|
| 49 |
headers=headers,
|
| 50 |
-
|
| 51 |
-
data={"prompt": prompt, "strength": 0.6}
|
| 52 |
)
|
| 53 |
|
| 54 |
if response.status_code == 200:
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
-
frame = original_image
|
| 59 |
-
|
| 60 |
-
frames.append(frame)
|
| 61 |
|
| 62 |
-
# Combine into a
|
| 63 |
widths, heights = zip(*(img.size for img in frames))
|
| 64 |
total_width = sum(widths)
|
| 65 |
max_height = max(heights)
|
|
@@ -70,11 +81,10 @@ def cartoon_story_tool(base64_image: str, story_prompt: str = "Create a 4-panel
|
|
| 70 |
storyboard.paste(img, (x_offset, 0))
|
| 71 |
x_offset += img.size[0]
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
storyboard.save(
|
| 76 |
-
encoded_story = base64.b64encode(
|
| 77 |
-
|
| 78 |
return f"data:image/png;base64,{encoded_story}"
|
| 79 |
|
| 80 |
|
|
@@ -117,7 +127,14 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 117 |
|
| 118 |
agent = CodeAgent(
|
| 119 |
model=model,
|
| 120 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
max_steps=6,
|
| 122 |
verbosity_level=1,
|
| 123 |
grammar=None,
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
| 23 |
def cartoon_story_tool(base64_image: str, story_prompt: str = "Create a 4-panel cartoon story.") -> str:
|
| 24 |
"""
|
| 25 |
Generates a cartoon-style story from an input image and optional prompt.
|
| 26 |
+
|
| 27 |
Args:
|
| 28 |
base64_image: A base64-encoded image string (input frame).
|
| 29 |
story_prompt: Optional text guiding the storyline.
|
| 30 |
+
Returns:
|
| 31 |
+
A base64 PNG image strip of 4 cartoon panels.
|
| 32 |
"""
|
| 33 |
+
import os
|
| 34 |
+
from PIL import Image
|
| 35 |
+
import io
|
| 36 |
+
import base64
|
| 37 |
+
|
| 38 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 39 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 40 |
|
| 41 |
+
# Decode the input image
|
| 42 |
+
image_data = base64.b64decode(base64_image.split(",")[-1])
|
| 43 |
+
original_image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 44 |
|
| 45 |
+
# Use the same image but apply prompt-based changes in a loop
|
| 46 |
frames = []
|
| 47 |
+
for i in range(4): # 4 cartoon panels
|
| 48 |
+
prompt = f"{story_prompt}, panel {i+1}"
|
| 49 |
+
payload = {
|
| 50 |
+
"inputs": prompt,
|
| 51 |
+
"parameters": {
|
| 52 |
+
"negative_prompt": "",
|
| 53 |
+
"guidance_scale": 7.5,
|
| 54 |
+
"num_inference_steps": 25
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
# Call a text-to-image model instead of image-to-image
|
| 58 |
response = requests.post(
|
| 59 |
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",
|
| 60 |
headers=headers,
|
| 61 |
+
json=payload
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
if response.status_code == 200:
|
| 65 |
+
try:
|
| 66 |
+
frame = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 67 |
+
frames.append(frame)
|
| 68 |
+
except Exception:
|
| 69 |
+
frames.append(original_image)
|
| 70 |
else:
|
| 71 |
+
frames.append(original_image)
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# Combine into a single horizontal strip
|
| 74 |
widths, heights = zip(*(img.size for img in frames))
|
| 75 |
total_width = sum(widths)
|
| 76 |
max_height = max(heights)
|
|
|
|
| 81 |
storyboard.paste(img, (x_offset, 0))
|
| 82 |
x_offset += img.size[0]
|
| 83 |
|
| 84 |
+
# Convert to base64
|
| 85 |
+
buffer = io.BytesIO()
|
| 86 |
+
storyboard.save(buffer, format="PNG")
|
| 87 |
+
encoded_story = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
|
|
|
| 88 |
return f"data:image/png;base64,{encoded_story}"
|
| 89 |
|
| 90 |
|
|
|
|
| 127 |
|
| 128 |
agent = CodeAgent(
|
| 129 |
model=model,
|
| 130 |
+
tools=[
|
| 131 |
+
final_answer,
|
| 132 |
+
get_current_time_in_timezone,
|
| 133 |
+
internet_search_tool,
|
| 134 |
+
image_generation_tool,
|
| 135 |
+
visit_webpage_tool,
|
| 136 |
+
cartoon_story_tool # Include this!
|
| 137 |
+
], ## add your tools here (don't remove final answer)
|
| 138 |
max_steps=6,
|
| 139 |
verbosity_level=1,
|
| 140 |
grammar=None,
|