Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
# Load your Stable Diffusion model (you can change the model if you want)
|
| 8 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 10 |
+
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Text generation for "Rewrite" button
|
| 13 |
+
text_generator = pipeline("text-generation", model="gpt2")
|
| 14 |
+
|
| 15 |
+
# Function to generate image
|
| 16 |
+
def generate_image(prompt):
|
| 17 |
+
image = pipe(prompt).images[0]
|
| 18 |
+
return image
|
| 19 |
+
|
| 20 |
+
# Rewrite prompt
|
| 21 |
+
def rewrite_prompt(prompt):
|
| 22 |
+
outputs = text_generator(f"Rephrase this: {prompt}", max_length=50, num_return_sequences=1)
|
| 23 |
+
return outputs[0]['generated_text'].replace("Rephrase this: ", "")
|
| 24 |
+
|
| 25 |
+
# Enhance prompt
|
| 26 |
+
def enhance_prompt(prompt):
|
| 27 |
+
return prompt + ", ultra detailed, 4K, high quality, cinematic lighting"
|
| 28 |
+
|
| 29 |
+
# Gradio Interface
|
| 30 |
+
with gr.Blocks(css="footer {display: none !important}") as demo:
|
| 31 |
+
gr.Markdown("## 🎨 Text to Image Generator")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
with gr.Column(scale=3):
|
| 34 |
+
prompt = gr.Textbox(label="Enter your prompt here")
|
| 35 |
+
with gr.Row():
|
| 36 |
+
btn_generate = gr.Button("🎨 Generate")
|
| 37 |
+
btn_rewrite = gr.Button("✍️ Rewrite")
|
| 38 |
+
btn_enhance = gr.Button("✨ Enhance")
|
| 39 |
+
output_image = gr.Image(label="Generated Image")
|
| 40 |
+
|
| 41 |
+
with gr.Column(scale=1):
|
| 42 |
+
gr.Markdown("### Prompt Editing")
|
| 43 |
+
prompt_display = gr.Textbox(label="Modified Prompt", interactive=False)
|
| 44 |
+
|
| 45 |
+
btn_generate.click(fn=generate_image, inputs=prompt, outputs=output_image)
|
| 46 |
+
btn_rewrite.click(fn=rewrite_prompt, inputs=prompt, outputs=prompt_display)
|
| 47 |
+
btn_enhance.click(fn=enhance_prompt, inputs=prompt, outputs=prompt_display)
|
| 48 |
+
|
| 49 |
+
demo.launch()
|