import gradio as gr import time import numpy as np from PIL import Image, ImageEnhance, ImageFilter import random def gemini_chatbot_processor(image_input, chat_history, prompt, mode, strength): """ Simulates a GPT-ChatBot experience powered by 'Gemini 2.0 Flash'. Handles both Image Generation (Text-to-Image) and Image Editing (Image-to-Image). """ # 1. Update Chat History with User Message if not prompt: gr.Warning("Please enter a prompt.") return chat_history, image_input chat_history = chat_history + [[prompt, None]] # 2. Simulate AI "Thinking" (Streaming effect simulation) time.sleep(0.5) chat_history[-1][1] = "🤔 Thinking..." yield chat_history, None time.sleep(0.8) # 3. Determine Logic: Generation vs Editing result_image = None response_text = "" # Check if user uploaded an image or wants to generate new has_image = image_input is not None if mode == "Generate New Image" or not has_image: # --- TEXT-TO-IMAGE GENERATION SIMULATION --- chat_history[-1][1] = "✨ Generating new image based on your prompt..." # Simulate processing time time.sleep(1.0) # Create a procedural "generated" image based on prompt hash (deterministic random) # This simulates the AI creating something unique based on text width, height = 512, 512 # Create a gradient/pattern based on prompt string hash_val = hash(prompt) random.seed(hash_val) # Create a base color r = random.randint(50, 200) g = random.randint(50, 200) b = random.randint(50, 200) img = Image.new("RGB", (width, height), (r, g, b)) pixels = img.load() # Add some "AI" noise/pattern for i in range(width): for j in range(height): noise = random.randint(-20, 20) pixels[i, j] = ( max(0, min(255, r + noise)), max(0, min(255, g + noise)), max(0, min(255, b + noise)) ) # Add a geometric pattern to look like a "generated" abstract art img = img.filter(ImageFilter.SMOOTH) result_image = np.array(img) response_text = f"Here is a new image generated for: '{prompt}'" else: # --- IMAGE EDITING SIMULATION --- chat_history[-1][1] = "🎨 Analyzing image and applying edits..." time.sleep(1.2) # Extract image if isinstance(image_input, dict) and "composite" in image_input: pil_img = Image.fromarray(image_input["composite"]) elif isinstance(image_input, np.ndarray): pil_img = Image.fromarray(image_input) else: pil_img = image_input # Apply simulated effects if mode == "Enhance / Refine": factor = 1.0 + (strength * 0.5) pil_img = ImageEnhance.Contrast(pil_img).enhance(factor) pil_img = ImageEnhance.Sharpness(pil_img).enhance(1.2) response_text = "I've enhanced the sharpness and contrast of your image." elif mode == "Change Style": # Simulate style transfer with a tint r, g, b = pil_img.split() r = r.point(lambda i: i * 1.1) pil_img = Image.merge("RGB", (r, g, b)) response_text = "I've applied a stylistic filter to the image." elif mode == "Magic Fill / Inpaint": # Simulate inpainting by slightly brightening masked areas (simulated logic) pil_img = ImageEnhance.Brightness(pil_img).enhance(1.1) response_text = "I've filled in the areas and adjusted the lighting." result_image = np.array(pil_img) # 4. Final Chat Update chat_history[-1][1] = response_text gr.Success("✨ Operation Complete!") return chat_history, result_image # --- Gradio 6 Application Setup --- with gr.Blocks() as demo: # Header with Branding gr.HTML("""

GPT-ChatBot x Gemini 2.0 Flash

Unlimited Online Free Demo • AI Image Generation & Editing

Built with anycoder
""") with gr.Row(): # Left Column: Editor & Canvas with gr.Column(scale=3): gr.Markdown("### 🖼️ Canvas") editor = gr.ImageEditor( label="Upload or Sketch", type="numpy", layers=True, sources=["upload", "webcam", "clipboard"], transforms=["crop", "rotate"], brush=gr.Brush( colors=["#FFFFFF", "#000000", "#FF0000", "#00FF00"], default_size="15", color_mode="fixed" ), eraser=gr.Eraser(default_size="20"), height=550 ) # Output Image Display output_image = gr.Image( label="Result", interactive=False, show_download_button=True, height=400 ) # Right Column: Chatbot & Controls with gr.Column(scale=2): gr.Markdown("### 🤖 AI Chat Interface") # Chatbot History chatbot = gr.Chatbot( label="Conversation History", height=300, show_copy_button=True, avatar_images=(None, "https://www.gstatic.com/lamda/images/gemini_sparkle_v002_d47a511478e8.svg") # Generic AI avatar ) # Controls with gr.Accordion("⚙️ Generation Settings", open=True): prompt_input = gr.Textbox( label="Your Instruction", placeholder="Describe the image you want to generate or how to edit the current one...", lines=3, autofocus=True ) mode = gr.Radio( choices=["Generate New Image", "Enhance / Refine", "Change Style", "Magic Fill / Inpaint"], value="Enhance / Refine", label="Operation Mode", info="Choose to create new or edit existing" ) strength = gr.Slider( minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Effect Intensity" ) generate_btn = gr.Button( "🚀 Send to Gemini Flash", variant="primary", size="lg" ) clear_btn = gr.ClearButton([prompt_input, chatbot, output_image], value="🗑️ Clear History") # Examples gr.Examples( examples=[ [None, "A futuristic cyberpunk city at night with neon lights", "Generate New Image", 0.8], [None, "A cute oil painting of a cat in a garden", "Generate New Image", 0.7], ["https://images.unsplash.com/photo-1506744038136-46273834b3fb?w=600", "Make this look like a vintage photo", "Change Style", 0.9], ], inputs=[editor, prompt_input, mode, strength], cache_examples=False, label="Quick Start Examples" ) # Event Listeners generate_btn.click( fn=gemini_chatbot_processor, inputs=[editor, chatbot, prompt_input, mode, strength], outputs=[chatbot, output_image], api_visibility="public" ) # Launch Configuration (Gradio 6 Syntax) demo.launch( theme=gr.themes.Soft( primary_hue="blue", # Google Blue secondary_hue="green", # Google Green neutral_hue="slate", font=gr.themes.GoogleFont("Google Sans", "Inter"), text_size="lg", spacing_size="lg", radius_size="md" ).set( button_primary_background_fill="*primary_600", button_primary_background_fill_hover="*primary_700", button_primary_text_color="white", # Custom Chatbot styling chatbot_message_background_fill_user="*primary_100", chatbot_message_background_fill_bot="*background_fill_secondary", ), footer_links=[ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}, {"label": "Gradio", "url": "https://gradio.app"}, {"label": "Google DeepMind", "url": "https://deepmind.google"} ] )