LucMarcelPellinger commited on
Commit
bc90b3d
·
verified ·
1 Parent(s): 8c5c24b

added cartoon generation tool to app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -1
app.py CHANGED
@@ -5,6 +5,10 @@ import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
 
 
 
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
@@ -33,6 +37,62 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -49,13 +109,14 @@ custom_role_conversions=None,
49
 
50
  # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
52
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
8
+ import base64
9
+ from PIL import Image
10
+ import io
11
+
12
  from Gradio_UI import GradioUI
13
 
14
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
40
+ @tool
41
+ def cartoon_story_tool(base64_image: str, story_prompt: str = "Create a 4-panel cartoon story.") -> str:
42
+ """
43
+ Generates a cartoon-style story from an input image and optional prompt.
44
+
45
+ Arguments:
46
+ base64_image: A base64-encoded image string (input frame).
47
+ story_prompt: Optional text guiding the storyline.
48
+
49
+ Returns:
50
+ A base64-encoded storyboard image or HTML with the sequence.
51
+ """
52
+ HF_TOKEN = "your_huggingface_token"
53
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
54
+
55
+ # Decode input image
56
+ image_bytes = base64.b64decode(base64_image.split(",")[-1])
57
+ original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
58
+
59
+ # Placeholder: Call an image-to-image or inpainting model to create variations
60
+ frames = []
61
+ for i in range(4): # 4-panel story
62
+ prompt = f"{story_prompt} Panel {i+1}"
63
+ # Hugging Face Image-to-Image endpoint (example)
64
+ response = requests.post(
65
+ "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",
66
+ headers=headers,
67
+ files={"init_image": io.BytesIO(image_bytes)},
68
+ data={"prompt": prompt, "strength": 0.6}
69
+ )
70
+
71
+ if response.status_code == 200:
72
+ frame = Image.open(io.BytesIO(response.content)).convert("RGB")
73
+ else:
74
+ # fallback to original
75
+ frame = original_image
76
+
77
+ frames.append(frame)
78
+
79
+ # Combine into a storyboard (horizontal strip)
80
+ widths, heights = zip(*(img.size for img in frames))
81
+ total_width = sum(widths)
82
+ max_height = max(heights)
83
+ storyboard = Image.new('RGB', (total_width, max_height))
84
+
85
+ x_offset = 0
86
+ for img in frames:
87
+ storyboard.paste(img, (x_offset, 0))
88
+ x_offset += img.size[0]
89
+
90
+ # Encode storyboard to base64
91
+ buffered = io.BytesIO()
92
+ storyboard.save(buffered, format="PNG")
93
+ encoded_story = base64.b64encode(buffered.getvalue()).decode("utf-8")
94
+
95
+ return f"data:image/png;base64,{encoded_story}"
96
 
97
  final_answer = FinalAnswerTool()
98
 
 
109
 
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
 
114
  with open("prompts.yaml", 'r') as stream:
115
  prompt_templates = yaml.safe_load(stream)
116
 
117
  agent = CodeAgent(
118
  model=model,
119
+ tools=[final_answer, get_current_time_in_timezone, internet_search_tool, image_generation_tool, cartoon_story_tool], ## add your tools here (don't remove final answer)
120
  max_steps=6,
121
  verbosity_level=1,
122
  grammar=None,