Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
# Only needed if using Inference API
|
| 8 |
+
HF_API_KEY = os.getenv("HF_API_KEY") # Set in Space secrets if using API
|
| 9 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 10 |
+
|
| 11 |
+
def generate_image_api(prompt, negative_prompt, width, height, num_inference_steps, guidance_scale, seed, num_images):
|
| 12 |
+
"""Generate using Hugging Face Inference API (requires API key for heavy use)"""
|
| 13 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"} if HF_API_KEY else {}
|
| 14 |
+
|
| 15 |
+
images = []
|
| 16 |
+
for i in range(num_images):
|
| 17 |
+
payload = {
|
| 18 |
+
"inputs": prompt,
|
| 19 |
+
"parameters": {
|
| 20 |
+
"negative_prompt": negative_prompt,
|
| 21 |
+
"width": width,
|
| 22 |
+
"height": height,
|
| 23 |
+
"num_inference_steps": num_inference_steps,
|
| 24 |
+
"guidance_scale": guidance_scale,
|
| 25 |
+
"seed": seed if seed != -1 else None
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
image = Image.open(io.BytesIO(response.content))
|
| 32 |
+
images.append(image)
|
| 33 |
+
else:
|
| 34 |
+
raise gr.Error(f"API Error: {response.text}")
|
| 35 |
+
|
| 36 |
+
return images
|
| 37 |
+
|
| 38 |
+
# Rest of the Gradio interface code remains the same...
|