Spaces:
Sleeping
Sleeping
Upload app0.py
Browse files
app0.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
def get_api_key():
|
| 9 |
+
return os.environ.get("PIXAZO_API_KEY")
|
| 10 |
+
|
| 11 |
+
def generate_image(prompt):
|
| 12 |
+
api_key = get_api_key()
|
| 13 |
+
if not api_key:
|
| 14 |
+
return None, "❌ API key not set"
|
| 15 |
+
|
| 16 |
+
url = "https://gateway.pixazo.ai/flux-1-schnell/v1/getData"
|
| 17 |
+
|
| 18 |
+
headers = {
|
| 19 |
+
'Content-Type': 'application/json',
|
| 20 |
+
'Cache-Control': 'no-cache',
|
| 21 |
+
'Ocp-Apim-Subscription-Key': api_key,
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
body = {
|
| 25 |
+
"prompt": prompt,
|
| 26 |
+
"num_steps": 4,
|
| 27 |
+
"height": 512,
|
| 28 |
+
"width": 512,
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(url, headers=headers, json=body, timeout=60)
|
| 33 |
+
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
result = response.json()
|
| 36 |
+
|
| 37 |
+
if "output" in result:
|
| 38 |
+
image_url = result["output"]
|
| 39 |
+
|
| 40 |
+
img_response = requests.get(image_url, timeout=30)
|
| 41 |
+
if img_response.status_code == 200:
|
| 42 |
+
image = Image.open(io.BytesIO(img_response.content))
|
| 43 |
+
if image.mode in ('RGBA', 'LA', 'P'):
|
| 44 |
+
image = image.convert('RGB')
|
| 45 |
+
return image, "✅ Image generated successfully!"
|
| 46 |
+
else:
|
| 47 |
+
return None, f"❌ Failed to download image: HTTP {img_response.status_code}"
|
| 48 |
+
else:
|
| 49 |
+
return None, "❌ No image URL in response"
|
| 50 |
+
else:
|
| 51 |
+
return None, f"❌ API error: {response.status_code}"
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return None, f"❌ Error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
# Simple interface
|
| 57 |
+
with gr.Blocks(title="Pixazo Image Generator") as demo:
|
| 58 |
+
gr.Markdown("# 🎨 Pixazo Image Generator")
|
| 59 |
+
|
| 60 |
+
api_key = get_api_key()
|
| 61 |
+
if api_key:
|
| 62 |
+
gr.Markdown("✅ API Key: Configured")
|
| 63 |
+
else:
|
| 64 |
+
gr.Markdown("❌ API Key: Not configured. Set PIXAZO_API_KEY in secrets.")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
prompt = gr.Textbox(label="Prompt", lines=3)
|
| 69 |
+
btn = gr.Button("Generate", variant="primary")
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
image = gr.Image(label="Generated Image")
|
| 73 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 74 |
+
|
| 75 |
+
btn.click(generate_image, inputs=prompt, outputs=[image, status])
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|