SNA-AI commited on
Commit
7b18d6f
Β·
verified Β·
1 Parent(s): 46d4fd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -33
app.py CHANGED
@@ -1,57 +1,39 @@
1
  import gradio as gr
2
- import requests
3
- from PIL import Image
4
- import io
5
  import os
6
 
7
- # πŸ”‘ your secret token name
8
  HF_TOKEN = os.getenv("images")
9
 
10
- # βœ… CORRECT inference API endpoint
11
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
12
-
13
- headers = {
14
- "Authorization": f"Bearer {HF_TOKEN}"
15
- }
16
 
17
  def generate_image(prompt):
18
  if not prompt.strip():
19
  return None, "⚠️ Enter a prompt"
20
 
21
  try:
22
- response = requests.post(
23
- API_URL,
24
- headers=headers,
25
- json={"inputs": prompt}
26
- )
27
-
28
- # πŸ”΄ IMPORTANT: handle loading state
29
- if response.status_code == 503:
30
- return None, "⏳ Model is loading... try again in 20 seconds"
31
-
32
- if response.status_code != 200:
33
- return None, f"Error: {response.text}"
34
-
35
- # convert bytes β†’ image
36
- image = Image.open(io.BytesIO(response.content))
37
-
38
- return image, "βœ… Image generated"
39
 
40
  except Exception as e:
41
- return None, str(e)
42
 
43
 
44
  # 🎨 UI
45
  with gr.Blocks() as app:
46
- gr.Markdown("# 🎨 Text β†’ Image Generator (Fixed HF API)")
47
 
48
- prompt = gr.Textbox(label="Prompt")
49
 
50
- btn = gr.Button("Generate")
51
 
52
- img = gr.Image(type="pil")
53
  status = gr.Textbox()
54
 
55
- btn.click(generate_image, prompt, [img, status])
56
 
57
  app.launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
 
3
  import os
4
 
5
+ # πŸ”‘ your HF secret name = images
6
  HF_TOKEN = os.getenv("images")
7
 
8
+ # βœ… Use official client (THIS FIXES YOUR ERROR)
9
+ client = InferenceClient(
10
+ model="stabilityai/stable-diffusion-xl-base-1.0",
11
+ token=HF_TOKEN
12
+ )
 
13
 
14
  def generate_image(prompt):
15
  if not prompt.strip():
16
  return None, "⚠️ Enter a prompt"
17
 
18
  try:
19
+ image = client.text_to_image(prompt)
20
+ return image, "βœ… Image generated successfully"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  except Exception as e:
23
+ return None, f"❌ Error: {str(e)}"
24
 
25
 
26
  # 🎨 UI
27
  with gr.Blocks() as app:
28
+ gr.Markdown("# 🎨 Text β†’ Image Generator (FIXED VERSION)")
29
 
30
+ prompt = gr.Textbox(label="Enter Prompt")
31
 
32
+ btn = gr.Button("Generate Image")
33
 
34
+ output = gr.Image(type="pil")
35
  status = gr.Textbox()
36
 
37
+ btn.click(generate_image, prompt, [output, status])
38
 
39
  app.launch()