Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,43 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# π
|
| 6 |
HF_TOKEN = os.getenv("images")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
if not HF_TOKEN:
|
| 10 |
-
|
| 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
|
| 21 |
-
return None, "β οΈ
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
image = client.text_to_image(prompt)
|
| 25 |
-
return image, "β
Image generated
|
| 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("# π¨
|
| 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
|
| 42 |
|
| 43 |
-
|
| 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()
|