Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 7 |
|
|
@@ -10,29 +14,44 @@ headers = {
|
|
| 10 |
}
|
| 11 |
|
| 12 |
def generate_image(prompt):
|
| 13 |
-
if not prompt.strip():
|
| 14 |
-
return None, "
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
return None, response.text
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
|
|
|
|
|
|
| 27 |
with gr.Blocks() as app:
|
| 28 |
-
gr.Markdown("# Text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
btn = gr.Button("Generate")
|
| 32 |
|
| 33 |
-
|
| 34 |
status = gr.Textbox()
|
| 35 |
|
| 36 |
-
btn.click(generate_image, prompt, [
|
| 37 |
|
| 38 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# π Your HF Space secret name is "images"
|
| 8 |
+
HF_TOKEN = os.getenv("images")
|
| 9 |
|
| 10 |
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 11 |
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def generate_image(prompt):
|
| 17 |
+
if not prompt or not prompt.strip():
|
| 18 |
+
return None, "β οΈ Please enter a prompt"
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
response = requests.post(
|
| 22 |
+
API_URL,
|
| 23 |
+
headers=headers,
|
| 24 |
+
json={"inputs": prompt}
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Handle API errors / loading
|
| 28 |
+
if response.status_code != 200:
|
| 29 |
+
return None, f"Error: {response.text}"
|
| 30 |
+
|
| 31 |
+
# Convert response bytes β image
|
| 32 |
+
image = Image.open(io.BytesIO(response.content))
|
| 33 |
|
| 34 |
+
return image, "β
Image generated successfully"
|
|
|
|
| 35 |
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return None, f"β Error: {str(e)}"
|
| 38 |
|
| 39 |
+
|
| 40 |
+
# π¨ Gradio UI
|
| 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 Image")
|
|
|
|
| 51 |
|
| 52 |
+
output_image = gr.Image(type="pil")
|
| 53 |
status = gr.Textbox()
|
| 54 |
|
| 55 |
+
btn.click(generate_image, prompt, [output_image, status])
|
| 56 |
|
| 57 |
app.launch()
|