Spaces:
Running
Running
Upload 2 files
Browse files- Text to img.py +53 -0
- requirements (1).txt +4 -0
Text to img.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 7 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
| 8 |
+
pipe.to("cuda") # or "cpu" if you don't have GPU
|
| 9 |
+
|
| 10 |
+
# Image generation function
|
| 11 |
+
def generate_image(prompt):
|
| 12 |
+
image = pipe(prompt).images[0]
|
| 13 |
+
return image
|
| 14 |
+
|
| 15 |
+
# Custom CSS for beautifying the interface
|
| 16 |
+
custom_css = """
|
| 17 |
+
footer {
|
| 18 |
+
text-align: center;
|
| 19 |
+
font-size: 16px;
|
| 20 |
+
font-weight: bold;
|
| 21 |
+
color: #4CAF50;
|
| 22 |
+
}
|
| 23 |
+
.gradio-container {
|
| 24 |
+
background-color: #f5f5f5;
|
| 25 |
+
border-radius: 15px;
|
| 26 |
+
padding: 20px;
|
| 27 |
+
}
|
| 28 |
+
input, textarea {
|
| 29 |
+
border: 2px solid #4CAF50;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
padding: 10px;
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# Interface definition with a footer
|
| 36 |
+
with gr.Blocks(css=custom_css) as interface:
|
| 37 |
+
gr.Markdown("# AI Image Generator")
|
| 38 |
+
gr.Markdown("**Enter a prompt to generate an image**")
|
| 39 |
+
|
| 40 |
+
# Input and Output
|
| 41 |
+
with gr.Row():
|
| 42 |
+
text_input = gr.Textbox(label="Prompt", placeholder="Enter text here")
|
| 43 |
+
image_output = gr.Image(label="Generated Image")
|
| 44 |
+
|
| 45 |
+
# Submit button
|
| 46 |
+
submit_button = gr.Button("Generate")
|
| 47 |
+
submit_button.click(generate_image, inputs=text_input, outputs=image_output)
|
| 48 |
+
|
| 49 |
+
# Footer section
|
| 50 |
+
gr.HTML('<footer>Powered by Ahsan TECH</footer>')
|
| 51 |
+
|
| 52 |
+
# Launch the interface
|
| 53 |
+
interface.launch(share=True)
|
requirements (1).txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
diffusers
|
| 3 |
+
transformers
|
| 4 |
+
torch
|