Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Hugging Face Inference API setup
|
7 |
+
API_URL = "https://api-inference.huggingface.co/models/Benevolent/PonyDiffusionV10"
|
8 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Get token from environment variable
|
9 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
10 |
+
|
11 |
+
def query(payload):
|
12 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
13 |
+
return json.loads(response.content)
|
14 |
+
|
15 |
+
# Function to call the HF Inference API
|
16 |
+
def generate_image(prompt):
|
17 |
+
payload = {
|
18 |
+
"inputs": prompt,
|
19 |
+
}
|
20 |
+
response = query(payload)
|
21 |
+
if 'error' in response:
|
22 |
+
return f"Error: {response['error']}"
|
23 |
+
# Check for the generated image
|
24 |
+
if isinstance(response, list) and len(response) > 0 and "generated_image" in response[0]:
|
25 |
+
return response[0]["generated_image"] # Base64 encoded image
|
26 |
+
return "No image returned from model"
|
27 |
+
|
28 |
+
# Gradio Blocks Web UI
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
with gr.Row():
|
31 |
+
gr.Markdown("# PonyDiffusion V10 Text-to-Image Generator")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
prompt = gr.Textbox(label="Enter a prompt for the image")
|
35 |
+
|
36 |
+
with gr.Row():
|
37 |
+
generate_button = gr.Button("Generate Image")
|
38 |
+
output_image = gr.Image()
|
39 |
+
|
40 |
+
# When generate_button is clicked, call generate_image
|
41 |
+
generate_button.click(fn=generate_image, inputs=prompt, outputs=output_image)
|
42 |
+
|
43 |
+
# Launch the app
|
44 |
+
demo.launch()
|