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("""
Unlimited Online Free Demo • AI Image Generation & Editing