Files changed (1) hide show
  1. app.py +54 -0
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()