Create app.py
Browse filesGenerate stunning images from text prompts using AI. Powered by Stable Diffusion, this app turns your imagination into beautiful artwork. Enter a prompt and get your image instantly.
इस ऐप की मदद से आप टेक्स्ट के माध्यम से अद्भुत चित्र बना सकते हैं। Stable Diffusion AI द्वारा संचालित, यह ऐप आपकी कल्पना को तुरंत सुंदर चित्रों में बदल देता है। बस एक प्रॉम्प्ट दर्ज करें और चित्र प्राप्त करें।
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py — Verified Hugging Face Space Code (CPU/GPU Safe)
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import traceback
|
| 8 |
+
|
| 9 |
+
# ------------------ CONFIG ------------------
|
| 10 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 11 |
+
|
| 12 |
+
# ------------------ SAFE MODEL LOAD ------------------
|
| 13 |
+
try:
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 16 |
+
|
| 17 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
MODEL_ID,
|
| 19 |
+
torch_dtype=dtype,
|
| 20 |
+
safety_checker=None
|
| 21 |
+
).to(device)
|
| 22 |
+
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print("❌ Model Loading Error:")
|
| 25 |
+
traceback.print_exc()
|
| 26 |
+
pipe = None
|
| 27 |
+
|
| 28 |
+
# ------------------ GENERATION FUNCTION ------------------
|
| 29 |
+
def generate_image(prompt):
|
| 30 |
+
if pipe is None:
|
| 31 |
+
return "⚠️ Model not loaded properly. Please check logs."
|
| 32 |
+
try:
|
| 33 |
+
result = pipe(prompt)
|
| 34 |
+
image = result.images[0]
|
| 35 |
+
return image
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print("❌ Generation Error:", e)
|
| 38 |
+
return "⚠️ Error generating image."
|
| 39 |
+
|
| 40 |
+
# ------------------ GRADIO UI ------------------
|
| 41 |
+
title = "🪷 Shrividya Text-to-Image AI"
|
| 42 |
+
description = "Generate stunning images from your text prompts using Stable Diffusion."
|
| 43 |
+
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=generate_image,
|
| 46 |
+
inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g. Divine temple on riverbank at sunset"),
|
| 47 |
+
outputs=gr.Image(label="Generated Image"),
|
| 48 |
+
title=title,
|
| 49 |
+
description=description,
|
| 50 |
+
allow_flagging="never"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
iface.launch()
|