File size: 1,412 Bytes
a9d15f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
import requests
import json
import os

# Hugging Face Inference API setup
API_URL = "https://api-inference.huggingface.co/models/Benevolent/PonyDiffusionV10"
HF_API_TOKEN = os.getenv("HF_API_TOKEN")  # Get token from environment variable
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return json.loads(response.content)

# Function to call the HF Inference API
def generate_image(prompt):
    payload = {
        "inputs": prompt,
    }
    response = query(payload)
    if 'error' in response:
        return f"Error: {response['error']}"
    # Check for the generated image
    if isinstance(response, list) and len(response) > 0 and "generated_image" in response[0]:
        return response[0]["generated_image"]  # Base64 encoded image
    return "No image returned from model"

# Gradio Blocks Web UI
with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown("# PonyDiffusion V10 Text-to-Image Generator")
    
    with gr.Row():
        prompt = gr.Textbox(label="Enter a prompt for the image")

    with gr.Row():
        generate_button = gr.Button("Generate Image")
        output_image = gr.Image()

    # When generate_button is clicked, call generate_image
    generate_button.click(fn=generate_image, inputs=prompt, outputs=output_image)

# Launch the app
demo.launch()