Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,10 @@ from PIL import Image
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# π
|
| 8 |
HF_TOKEN = os.getenv("images")
|
| 9 |
|
|
|
|
| 10 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 11 |
|
| 12 |
headers = {
|
|
@@ -14,8 +15,8 @@ headers = {
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def generate_image(prompt):
|
| 17 |
-
if not prompt
|
| 18 |
-
return None, "β οΈ
|
| 19 |
|
| 20 |
try:
|
| 21 |
response = requests.post(
|
|
@@ -24,34 +25,33 @@ def generate_image(prompt):
|
|
| 24 |
json={"inputs": prompt}
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
| 28 |
if response.status_code != 200:
|
| 29 |
return None, f"Error: {response.text}"
|
| 30 |
|
| 31 |
-
#
|
| 32 |
image = Image.open(io.BytesIO(response.content))
|
| 33 |
|
| 34 |
-
return image, "β
Image generated
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
return None,
|
| 38 |
|
| 39 |
|
| 40 |
-
# π¨
|
| 41 |
with gr.Blocks() as app:
|
| 42 |
-
gr.Markdown("# π¨ Text β Image Generator (HF API)")
|
| 43 |
-
gr.Markdown("Enter a prompt and generate AI images using Hugging Face API")
|
| 44 |
|
| 45 |
-
prompt = gr.Textbox(
|
| 46 |
-
label="Enter Prompt",
|
| 47 |
-
placeholder="A futuristic cyberpunk city at night..."
|
| 48 |
-
)
|
| 49 |
|
| 50 |
-
btn = gr.Button("Generate
|
| 51 |
|
| 52 |
-
|
| 53 |
status = gr.Textbox()
|
| 54 |
|
| 55 |
-
btn.click(generate_image, prompt, [
|
| 56 |
|
| 57 |
app.launch()
|
|
|
|
| 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 = {
|
|
|
|
| 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(
|
|
|
|
| 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()
|