SNA-AI commited on
Commit
e00a860
Β·
verified Β·
1 Parent(s): 21c3440

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -22
app.py CHANGED
@@ -2,51 +2,43 @@ import gradio as gr
2
  import os
3
  from huggingface_hub import InferenceClient
4
 
5
- # πŸ”‘ Your Hugging Face Space secret name = "images"
6
  HF_TOKEN = os.getenv("images")
7
 
8
- # 🚨 Safety check (very important)
9
  if not HF_TOKEN:
10
- raise ValueError("❌ Token not found. Make sure Space Secret name is 'images'")
11
 
12
- # πŸ€– Stable Diffusion model (fast + reliable)
13
  client = InferenceClient(
14
  model="stabilityai/stable-diffusion-xl-base-1.0",
15
  token=HF_TOKEN
16
  )
17
 
18
- # 🎨 Image generator function
19
  def generate_image(prompt):
20
- if not prompt or not prompt.strip():
21
- return None, "⚠️ Please enter a prompt"
 
 
 
22
 
23
  try:
24
  image = client.text_to_image(prompt)
25
- return image, "βœ… Image generated successfully"
26
 
27
  except Exception as e:
28
  return None, f"❌ Error: {str(e)}"
29
 
30
 
31
- # πŸ–₯️ Gradio UI
32
  with gr.Blocks() as app:
33
- gr.Markdown("# 🎨 AI Text β†’ Image Generator")
34
- gr.Markdown("Generate images using Stable Diffusion (Hugging Face API)")
35
 
36
- prompt = gr.Textbox(
37
- label="Enter Prompt",
38
- placeholder="A futuristic city at night, ultra detailed..."
39
- )
40
 
41
- btn = gr.Button("Generate Image")
42
 
43
- output_image = gr.Image(type="pil")
44
  status = gr.Textbox()
45
 
46
- btn.click(
47
- fn=generate_image,
48
- inputs=prompt,
49
- outputs=[output_image, status]
50
- )
51
 
52
  app.launch()
 
2
  import os
3
  from huggingface_hub import InferenceClient
4
 
5
+ # πŸ”‘ Load token from Space secret
6
  HF_TOKEN = os.getenv("images")
7
 
8
+ # ❌ DO NOT crash app
9
  if not HF_TOKEN:
10
+ HF_TOKEN = ""
11
 
 
12
  client = InferenceClient(
13
  model="stabilityai/stable-diffusion-xl-base-1.0",
14
  token=HF_TOKEN
15
  )
16
 
 
17
  def generate_image(prompt):
18
+ if not prompt.strip():
19
+ return None, "⚠️ Enter a prompt"
20
+
21
+ if not HF_TOKEN:
22
+ return None, "❌ Token not loaded. Restart Space or check secret name."
23
 
24
  try:
25
  image = client.text_to_image(prompt)
26
+ return image, "βœ… Image generated"
27
 
28
  except Exception as e:
29
  return None, f"❌ Error: {str(e)}"
30
 
31
 
 
32
  with gr.Blocks() as app:
33
+ gr.Markdown("# 🎨 Text β†’ Image Generator")
 
34
 
35
+ prompt = gr.Textbox(label="Prompt")
 
 
 
36
 
37
+ btn = gr.Button("Generate")
38
 
39
+ img = gr.Image(type="pil")
40
  status = gr.Textbox()
41
 
42
+ btn.click(generate_image, prompt, [img, status])
 
 
 
 
43
 
44
  app.launch()