AMfeta99/Object_Evolution_Generator
0
1from PIL import Image, ImageDraw, ImageFont2import tempfile3import gradio as gr4from smolagents import CodeAgent, InferenceClientModel5from smolagents import DuckDuckGoSearchTool, Tool6from diffusers import DiffusionPipeline7import torch8from smolagents import OpenAIServerModel9import os10from huggingface_hub import login11 12 13openai_key = os.environ.get("OPENAI_API_KEY")14hf_token = os.environ.get("HF_TOKEN")15 16if hf_token:17 login(token=hf_token)18else:19 print("Warning: HF_TOKEN not set.")20 21if openai_key:22 # Exemplo de como usar a OpenAI API key23 print("OpenAI API key is set")24else:25 print("Warning: OPENAI_API_KEY not set.")26 27print("HF_TOKEN set?", "Yes" if hf_token else "No")28print("OPENAI_API_KEY set?", "Yes" if openai_key else "No")29 30 31 32# =========================================================33# Utility functions34# =========================================================35 36def add_label_to_image(image, label):37 draw = ImageDraw.Draw(image)38 font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"39 font_size = 3040 try:41 font = ImageFont.truetype(font_path, font_size)42 except:43 font = ImageFont.load_default()44 45 text_bbox = draw.textbbox((0, 0), label, font=font)46 text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]47 position = (image.width - text_width - 20, image.height - text_height - 20)48 49 rect_margin = 1050 rect_position = [51 position[0] - rect_margin,52 position[1] - rect_margin,53 position[0] + text_width + rect_margin,54 position[1] + text_height + rect_margin,55 ]56 draw.rectangle(rect_position, fill=(0, 0, 0, 128))57 draw.text(position, label, fill="white", font=font)58 return image59 60 61def plot_and_save_agent_image(agent_image, label, save_path=None):62 #pil_image = agent_image.to_raw()63 pil_image = agent_image64 labeled_image = add_label_to_image(pil_image, label)65 #labeled_image.show()66 if save_path:67 labeled_image.save(save_path)68 print(f"Image saved to {save_path}")69 else:70 print("No save path provided. Image not saved.")71 72 73def generate_prompts_for_object(object_name):74 return {75 "past": f"Show an old version of a {object_name} from its early days.",76 "present": f"Show a {object_name} with current features/design/technology.",77 "future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."78 }79 80 81 82# =========================================================83# Tool and Agent Initialization84# =========================================================85 86image_generation_tool = Tool.from_space(87 #"KingNish/Realtime-FLUX",88 "black-forest-labs/FLUX.1-schnell",89 #"AMfeta99/FLUX.1-schnell",90 api_name="/infer",91 name="image_generator",92 description="Generate an image from a prompt"93)94 95search_tool = DuckDuckGoSearchTool()96#llm_engine = InferenceClientModel("Qwen/Qwen2.5-72B-Instruct")97 98llm_engine2 = InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")99 100# Inicialização do modelo OpenAI com smolagents101llm_engine = OpenAIServerModel(102 model_id="gpt-4o-mini", # Exemplo: ajuste para o modelo OpenAI que deseja usar103 api_base="https://api.openai.com/v1",104 api_key=openai_key105)106 107 108agent = CodeAgent(tools=[image_generation_tool, search_tool], model=llm_engine)109 110# =========================================================111# Main logic for image generation112# =========================================================113 114from PIL import Image115 116 117def generate_object_history(object_name):118 images = []119 prompts = generate_prompts_for_object(object_name)120 121 general_instruction = (122 "Search the necessary information and features for the following prompt, "123 "then generate an image of it."124 )125 126 image_paths = []127 for time_period, prompt in prompts.items():128 print(f"Generating {time_period} frame: {prompt}")129 130 try:131 result = agent.run(132 general_instruction,133 additional_args={"prompt": prompt,134 "width": 256, # specify width135 "height": 256, # specify height136 "seed": 0, # optional seed137 "randomize_seed": False, # optional138 "num_inference_steps": 4 # optional139 }140 )141 142 # result is tuple: (filepath, seed)143 if isinstance(result, (list, tuple)):144 image_filepath = result[0]145 else:146 image_filepath = result # fallback in case result is just a string147 148 # Open the image from filepath149 image = Image.open(image_filepath)150 151 # Save the image to your naming convention152 image_filename = f"{object_name}_{time_period}.png"153 image.save(image_filename)154 155 # Optional: call your plotting function (if needed)156 plot_and_save_agent_image(image, f"{object_name} - {time_period.title()}", save_path=image_filename)157 158 image_paths.append(image_filename)159 images.append(image)160 161 except Exception as e:162 print(f"Agent failed on {time_period}: {e}")163 continue164 165 # Create GIF from generated images if any166 gif_path = f"{object_name}_evolution.gif"167 if images:168 images[0].save(gif_path, save_all=True, append_images=images[1:], duration=1000, loop=0)169 170 return image_paths, gif_path171 172 173# =========================================================174# Gradio Interface175# =========================================================176 177 178def create_gradio_interface():179 with gr.Blocks() as demo:180 gr.Markdown("# TimeMetamorphy: An Object Evolution Generator")181 gr.Markdown("""182 Explore how everyday objects evolved over time. Enter an object name like "phone", "car", or "bicycle"183 and see its past, present, and future visualized with AI!184 """)185 #gr.Markdown("<span style='color: red;'>Note: If you experience issues connecting to the API while using the HF Space, try running the tool in this Colab Notebook instead — it may resolve the issue. <a href='https://colab.research.google.com/drive/1aKBJWkRBKhW8VFEu8p1zaxJr9VDzPaRz?usp=sharing' target='_blank'>Open Notebook</a>.</span>")186 gr.HTML("<p style='color: red; font-weight: bold;'>🚨 Note: If you experience issues connecting to the API (while using the HF Space), If that happens feel free to run the exact same app/code in this Colab Notebook (it solve the issue).<a href='https://colab.research.google.com/drive/1aKBJWkRBKhW8VFEu8p1zaxJr9VDzPaRz?usp=sharing' target='_blank' style='color: red; text-decoration: underline;'> Open Notebook</a>.</p>")187 188 189 default_images = [190 "car_past.png",191 "car_present.png",192 "car_future.png"193 ]194 default_gif_path = "car_evolution.gif"195 196 with gr.Row():197 with gr.Column():198 object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g. bicycle, car, phone")199 generate_button = gr.Button("Generate Evolution")200 image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, value=default_images, type="filepath")201 gif_output = gr.Image(label="Generated GIF", value=default_gif_path, type="filepath")202 #image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, type="filepath")203 #gif_output = gr.Image(label="Generated GIF", type="filepath")204 205 generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])206 207 return demo208 209 210# Launch the interface211demo = create_gradio_interface()212demo.launch()213 