Spaces:
Running
Running
File size: 2,442 Bytes
06ae90e 057604e 06ae90e 057604e 01e9899 057604e 01e9899 057604e 06ae90e 057604e 06ae90e 057604e 06ae90e 057604e 06ae90e 057604e 06ae90e 057604e |
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 |
import gradio as gr
from diffusers import StableDiffusionXLImg2ImgPipeline
import torch
# Load a lightweight pipeline that works well on CPU
def load_image_generator():
try:
model = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True
)
# Ensure it runs on CPU
#model = model.to("cpu")
return model
except Exception as e:
print(f"Error loading model: {e}")
return None
# Generate chatbot icon
def generate_chatbot_icon(
prompt,
negative_prompt="low quality, bad composition, blurry",
num_inference_steps=20,
guidance_scale=7.5,
strength=0.75
):
# Load the model
model = load_image_generator()
if model is None:
return None
# Default icon if no initial image
default_init_image = torch.randn((1, 3, 512, 512))
try:
# Generate the image
image = model(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
strength=strength,
image=default_init_image
).images[0]
return image
except Exception as e:
print(f"Error generating image: {e}")
return None
# Create Gradio interface
def create_gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("# 🤖 Chatbot Icon Generator")
with gr.Row():
with gr.Column():
# Prompt input
prompt = gr.Textbox(
label="Icon Description",
value="Cute minimalist chatbot avatar, clean design, friendly expression, cartoon style"
)
# Generate button
generate_btn = gr.Button("Generate Icon")
with gr.Column():
# Output image
output_image = gr.Image(label="Generated Chatbot Icon")
# Connect generate button to function
generate_btn.click(
fn=generate_chatbot_icon,
inputs=[prompt],
outputs=[output_image]
)
return demo
# Launch the app
if __name__ == "__main__":
demo = create_gradio_interface()
demo.launch() |