Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from diffusers import
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
generate_button = gr.Button("Generate Icon")
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load a lightweight pipeline that works well on CPU
|
| 6 |
+
def load_image_generator():
|
| 7 |
+
try:
|
| 8 |
+
model = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 9 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
variant="fp16",
|
| 12 |
+
use_safetensors=True
|
| 13 |
+
)
|
| 14 |
+
# Ensure it runs on CPU
|
| 15 |
+
model = model.to("cpu")
|
| 16 |
+
return model
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Error loading model: {e}")
|
| 19 |
+
return None
|
| 20 |
|
| 21 |
+
# Generate chatbot icon
|
| 22 |
+
def generate_chatbot_icon(
|
| 23 |
+
prompt,
|
| 24 |
+
negative_prompt="low quality, bad composition, blurry",
|
| 25 |
+
num_inference_steps=20,
|
| 26 |
+
guidance_scale=7.5,
|
| 27 |
+
strength=0.75
|
| 28 |
+
):
|
| 29 |
+
# Load the model
|
| 30 |
+
model = load_image_generator()
|
| 31 |
+
if model is None:
|
| 32 |
+
return None
|
|
|
|
| 33 |
|
| 34 |
+
# Default icon if no initial image
|
| 35 |
+
default_init_image = torch.randn((1, 3, 512, 512))
|
| 36 |
|
| 37 |
+
try:
|
| 38 |
+
# Generate the image
|
| 39 |
+
image = model(
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
negative_prompt=negative_prompt,
|
| 42 |
+
num_inference_steps=num_inference_steps,
|
| 43 |
+
guidance_scale=guidance_scale,
|
| 44 |
+
strength=strength,
|
| 45 |
+
image=default_init_image
|
| 46 |
+
).images[0]
|
| 47 |
+
|
| 48 |
+
return image
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error generating image: {e}")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
# Create Gradio interface
|
| 54 |
+
def create_gradio_interface():
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# 🤖 Chatbot Icon Generator")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
# Prompt input
|
| 61 |
+
prompt = gr.Textbox(
|
| 62 |
+
label="Icon Description",
|
| 63 |
+
value="Cute minimalist chatbot avatar, clean design, friendly expression, cartoon style"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Generate button
|
| 67 |
+
generate_btn = gr.Button("Generate Icon")
|
| 68 |
+
|
| 69 |
+
with gr.Column():
|
| 70 |
+
# Output image
|
| 71 |
+
output_image = gr.Image(label="Generated Chatbot Icon")
|
| 72 |
+
|
| 73 |
+
# Connect generate button to function
|
| 74 |
+
generate_btn.click(
|
| 75 |
+
fn=generate_chatbot_icon,
|
| 76 |
+
inputs=[prompt],
|
| 77 |
+
outputs=[output_image]
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
return demo
|
| 81 |
|
| 82 |
+
# Launch the app
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
demo = create_gradio_interface()
|
| 85 |
+
demo.launch()
|