Spaces:
Runtime error
Runtime error
File size: 9,539 Bytes
6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e 7aacd35 6ec448e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | 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("""
<div style="text-align: center; margin-bottom: 25px; border-bottom: 1px solid #e5e7eb; padding-bottom: 20px;">
<h1 style="margin: 0; font-size: 2.5rem; background: linear-gradient(90deg, #4285F4, #34A853, #FBBC05, #EA4335); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
GPT-ChatBot x Gemini 2.0 Flash
</h1>
<p style="color: #6b7280; margin-top: 10px; font-size: 1.1rem;">
Unlimited Online Free Demo β’ AI Image Generation & Editing
</p>
<div style="margin-top: 15px;">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank"
style="display: inline-block; background-color: #f3f4f6; color: #374151;
padding: 8px 16px; border-radius: 20px; text-decoration: none;
font-weight: 600; font-size: 0.9rem; border: 1px solid #d1d5db;">
Built with anycoder
</a>
</div>
</div>
""")
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"}
]
) |