Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import io
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# π your secret
|
| 8 |
HF_TOKEN = os.getenv("images")
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
}
|
| 16 |
|
| 17 |
def generate_image(prompt):
|
| 18 |
if not prompt.strip():
|
| 19 |
return None, "β οΈ Enter a prompt"
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 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 (
|
| 47 |
|
| 48 |
-
prompt = gr.Textbox(label="Prompt")
|
| 49 |
|
| 50 |
-
btn = gr.Button("Generate")
|
| 51 |
|
| 52 |
-
|
| 53 |
status = gr.Textbox()
|
| 54 |
|
| 55 |
-
btn.click(generate_image, prompt, [
|
| 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()
|