Spaces:
Running
Running
| import gradio as gr | |
| from transformers import pipeline | |
| from diffusers import StableDiffusionPipeline | |
| import torch | |
| import random | |
| # Load your Stable Diffusion model (you can change the model if you want) | |
| model_id = "runwayml/stable-diffusion-v1-5" | |
| pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32) | |
| pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu") | |
| # Text generation for "Rewrite" button | |
| text_generator = pipeline("text-generation", model="gpt2") | |
| # Function to generate image | |
| def generate_image(prompt): | |
| image = pipe(prompt).images[0] | |
| return image | |
| # Rewrite prompt | |
| def rewrite_prompt(prompt): | |
| outputs = text_generator(f"Rephrase this: {prompt}", max_length=50, num_return_sequences=1) | |
| return outputs[0]['generated_text'].replace("Rephrase this: ", "") | |
| # Enhance prompt | |
| def enhance_prompt(prompt): | |
| return prompt + ", ultra detailed, 4K, high quality, cinematic lighting" | |
| # Gradio Interface | |
| with gr.Blocks(css="footer {display: none !important}") as demo: | |
| gr.Markdown("## 🎨 Text to Image Generator") | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| prompt = gr.Textbox(label="Enter your prompt here") | |
| with gr.Row(): | |
| btn_generate = gr.Button("🎨 Generate") | |
| btn_rewrite = gr.Button("✍️ Rewrite") | |
| btn_enhance = gr.Button("✨ Enhance") | |
| output_image = gr.Image(label="Generated Image") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### Prompt Editing") | |
| prompt_display = gr.Textbox(label="Modified Prompt", interactive=False) | |
| btn_generate.click(fn=generate_image, inputs=prompt, outputs=output_image) | |
| btn_rewrite.click(fn=rewrite_prompt, inputs=prompt, outputs=prompt_display) | |
| btn_enhance.click(fn=enhance_prompt, inputs=prompt, outputs=prompt_display) | |
| demo.launch() | |