Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# π
|
| 6 |
HF_TOKEN = os.getenv("images")
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, "β οΈ
|
| 17 |
|
| 18 |
try:
|
| 19 |
image = client.text_to_image(prompt)
|
|
@@ -23,17 +28,25 @@ def generate_image(prompt):
|
|
| 23 |
return None, f"β Error: {str(e)}"
|
| 24 |
|
| 25 |
|
| 26 |
-
#
|
| 27 |
with gr.Blocks() as app:
|
| 28 |
-
gr.Markdown("# π¨ Text β Image Generator
|
|
|
|
| 29 |
|
| 30 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
btn = gr.Button("Generate Image")
|
| 33 |
|
| 34 |
-
|
| 35 |
status = gr.Textbox()
|
| 36 |
|
| 37 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# π Your Hugging Face Space secret name = "images"
|
| 6 |
HF_TOKEN = os.getenv("images")
|
| 7 |
|
| 8 |
+
# π¨ Safety check (very important)
|
| 9 |
+
if not HF_TOKEN:
|
| 10 |
+
raise ValueError("β Token not found. Make sure Space Secret name is 'images'")
|
| 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 or not prompt.strip():
|
| 21 |
+
return None, "β οΈ Please enter a prompt"
|
| 22 |
|
| 23 |
try:
|
| 24 |
image = client.text_to_image(prompt)
|
|
|
|
| 28 |
return None, f"β Error: {str(e)}"
|
| 29 |
|
| 30 |
|
| 31 |
+
# π₯οΈ Gradio UI
|
| 32 |
with gr.Blocks() as app:
|
| 33 |
+
gr.Markdown("# π¨ AI Text β Image Generator")
|
| 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 Image")
|
| 42 |
|
| 43 |
+
output_image = gr.Image(type="pil")
|
| 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()
|