Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,42 +2,51 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# π
|
| 6 |
-
HF_TOKEN = os.environ.get("
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
client = InferenceClient(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
|
| 16 |
def generate_image(prompt):
|
| 17 |
-
if not prompt.strip():
|
| 18 |
-
return None, "β οΈ
|
| 19 |
|
| 20 |
-
if
|
| 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 |
with gr.Blocks() as app:
|
| 32 |
-
gr.Markdown("# π¨ Text β Image Generator")
|
|
|
|
| 33 |
|
| 34 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
btn = gr.Button("Generate")
|
| 37 |
|
| 38 |
-
|
| 39 |
status = gr.Textbox()
|
| 40 |
|
| 41 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
app.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# π Load Hugging Face token from Space secret
|
| 6 |
+
HF_TOKEN = os.environ.get("image")
|
| 7 |
|
| 8 |
+
# π€ Create client only if token exists
|
| 9 |
+
client = None
|
| 10 |
+
if HF_TOKEN:
|
| 11 |
+
client = InferenceClient(
|
| 12 |
+
model="stabilityai/stable-diffusion-xl-base-1.0",
|
| 13 |
+
token=HF_TOKEN
|
| 14 |
+
)
|
| 15 |
|
| 16 |
def generate_image(prompt):
|
| 17 |
+
if not prompt or not prompt.strip():
|
| 18 |
+
return None, "β οΈ Please enter a prompt"
|
| 19 |
|
| 20 |
+
if client is None:
|
| 21 |
+
return None, "β Token not loaded. Check secret name = 'image' and restart Space."
|
| 22 |
|
| 23 |
try:
|
| 24 |
image = client.text_to_image(prompt)
|
| 25 |
+
return image, "β
Image generated successfully"
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
return None, f"β Error: {str(e)}"
|
| 29 |
|
| 30 |
|
| 31 |
+
# π¨ 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()
|