Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,11 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
|
@@ -22,6 +23,61 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 22 |
"""
|
| 23 |
return "What magic will you build ?"
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -54,6 +110,7 @@ custom_role_conversions=None,
|
|
| 54 |
# Import tool from Hub
|
| 55 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 56 |
internet_search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 57 |
|
| 58 |
with open("prompts.yaml", 'r') as stream:
|
| 59 |
prompt_templates = yaml.safe_load(stream)
|
|
|
|
| 4 |
import pytz
|
| 5 |
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 |
|
|
|
|
| 23 |
"""
|
| 24 |
return "What magic will you build ?"
|
| 25 |
|
| 26 |
+
@tool
|
| 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 |
+
image_bytes = base64.b64decode(base64_image.split(",")[-1])
|
| 40 |
+
original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 41 |
+
|
| 42 |
+
# Placeholder: Call an image-to-image or inpainting model to create variations
|
| 43 |
+
frames = []
|
| 44 |
+
for i in range(4): # 4-panel story
|
| 45 |
+
prompt = f"{story_prompt} Panel {i+1}"
|
| 46 |
+
# Hugging Face Image-to-Image endpoint (example)
|
| 47 |
+
response = requests.post(
|
| 48 |
+
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",
|
| 49 |
+
headers=headers,
|
| 50 |
+
files={"init_image": io.BytesIO(image_bytes)},
|
| 51 |
+
data={"prompt": prompt, "strength": 0.6}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
frame = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 56 |
+
else:
|
| 57 |
+
# fallback to original
|
| 58 |
+
frame = original_image
|
| 59 |
+
|
| 60 |
+
frames.append(frame)
|
| 61 |
+
|
| 62 |
+
# Combine into a storyboard (horizontal strip)
|
| 63 |
+
widths, heights = zip(*(img.size for img in frames))
|
| 64 |
+
total_width = sum(widths)
|
| 65 |
+
max_height = max(heights)
|
| 66 |
+
storyboard = Image.new('RGB', (total_width, max_height))
|
| 67 |
+
|
| 68 |
+
x_offset = 0
|
| 69 |
+
for img in frames:
|
| 70 |
+
storyboard.paste(img, (x_offset, 0))
|
| 71 |
+
x_offset += img.size[0]
|
| 72 |
+
|
| 73 |
+
# Encode storyboard to base64
|
| 74 |
+
buffered = io.BytesIO()
|
| 75 |
+
storyboard.save(buffered, format="PNG")
|
| 76 |
+
encoded_story = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 77 |
+
|
| 78 |
+
return f"data:image/png;base64,{encoded_story}"
|
| 79 |
+
|
| 80 |
+
|
| 81 |
@tool
|
| 82 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 83 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 110 |
# Import tool from Hub
|
| 111 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 112 |
internet_search_tool = DuckDuckGoSearchTool()
|
| 113 |
+
visit_webpage_tool = VisitWebpageTool()
|
| 114 |
|
| 115 |
with open("prompts.yaml", 'r') as stream:
|
| 116 |
prompt_templates = yaml.safe_load(stream)
|