Update app.py
Browse files8 unmodified lines9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919237 unmodified lines1301311321331341351361371381398 unmodified linesfrom Gradio_UI import GradioUI# Below is an example of a tool that does nothing. Amaze us with your creativity !@tooldef my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type#Keep this format for the description / args / args description but feel free to modify the tool"""A tool that does nothing yetArgs:arg1: the first argumentarg2: the second argument"""return "What magic will you build ?"@tooldef cartoon_story_tool(base64_image: str, story_prompt: str = "Create a 4-panel cartoon story.") -> str:"""Generates a cartoon-style story from an input image and optional prompt.Args:base64_image: A base64-encoded image string (input frame).story_prompt: Optional text guiding the storyline.Returns:A base64 PNG image strip of 4 cartoon panels."""import osfrom PIL import Imageimport ioimport base64HF_TOKEN = os.getenv("HF_TOKEN")headers = {"Authorization": f"Bearer {HF_TOKEN}"}# Decode the input imageimage_data = base64.b64decode(base64_image.split(",")[-1])original_image = Image.open(io.BytesIO(image_data)).convert("RGB")# Use the same image but apply prompt-based changes in a loopframes = []for i in range(4): # 4 cartoon panelsprompt = f"{story_prompt}, panel {i+1}"payload = {"inputs": prompt,"parameters": {"negative_prompt": "","guidance_scale": 7.5,"num_inference_steps": 25}}# Call a text-to-image model instead of image-to-imageresponse = requests.post("https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",headers=headers,json=payload)if response.status_code == 200:try:frame = Image.open(io.BytesIO(response.content)).convert("RGB")frames.append(frame)except Exception:frames.append(original_image)else:frames.append(original_image)# Combine into a single horizontal stripwidths, heights = zip(*(img.size for img in frames))total_width = sum(widths)max_height = max(heights)storyboard = Image.new('RGB', (total_width, max_height))x_offset = 0for img in frames:storyboard.paste(img, (x_offset, 0))x_offset += img.size[0]# Convert to base64buffer = io.BytesIO()storyboard.save(buffer, format="PNG")encoded_story = base64.b64encode(buffer.getvalue()).decode("utf-8")return f"data:image/png;base64,{encoded_story}"@tooldef get_current_time_in_timezone(timezone: str) -> str:37 unmodified linestools=[final_answer,get_current_time_in_timezone,internet_search_tool,image_generation_tool,visit_webpage_tool,cartoon_story_tool # Include this!], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,8 unmodified lines91011121314151617181920212223242526272829303132333435363737 unmodified lines757677787980818 unmodified linesfrom Gradio_UI import GradioUI# Below is an example of a tool that does nothing. Amaze us with your creativity !#@tool#def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type# #Keep this format for the description / args / args description but feel free to modify the tool# """A tool that does nothing yet# Args:# arg1: the first argument# arg2: the second argument# """# return "What magic will you build ?"@tooldef todays_useless_fact() -> str:"""Fetches today's useless fact from an external public API."""try:response = requests.get("https://uselessfacts.jsph.pl/api/v2/facts/today")if response.status_code == 200:data = response.json()return data.get("text", "No fact found.")else:return f"Failed to fetch fact: {response.status_code}"except Exception as e:return f"Error occurred: {str(e)}"@tooldef get_current_time_in_timezone(timezone: str) -> str:37 unmodified linestools=[final_answer,get_current_time_in_timezone,todays_useless_fact # Include this!], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,
app.py+19-77