File size: 1,498 Bytes
a9d15f3
 
050a39a
 
a9d15f3
 
 
 
050a39a
 
a9d15f3
050a39a
 
a9d15f3
050a39a
 
 
 
 
 
 
 
 
 
 
 
 
a9d15f3
050a39a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d15f3
050a39a
 
 
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
46
47
48
import gradio as gr
import requests
from PIL import Image
from io import BytesIO
import os



# Load API Token from environment variable
API_TOKEN = os.getenv("HF_API_TOKEN")  # Ensure you've set this environment variable

# Hugging Face Inference API URL
API_URL = "https://api-inference.huggingface.co/models/Benevolent/PonyDiffusionV10"

# Function to call Hugging Face API and get the generated image
def generate_image(prompt):
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    data = {"inputs": prompt}
    
    response = requests.post(API_URL, headers=headers, json=data)
    
    if response.status_code == 200:
        image_bytes = BytesIO(response.content)
        image = Image.open(image_bytes)
        return image
    else:
        return f"Error: {response.status_code}, {response.text}"

# Create Gradio interface
def create_ui():
    with gr.Blocks() as ui:
        gr.Markdown("## PonyDiffusionV10 - Text to Image Generator")
        
        with gr.Row():
            prompt_input = gr.Textbox(label="Enter a Prompt", placeholder="Describe the image you want to generate", lines=3)
            generate_button = gr.Button("Generate Image")
        
        with gr.Row():
            output_image = gr.Image(label="Generated Image")
        
        # Link the button to the function
        generate_button.click(fn=generate_image, inputs=prompt_input, outputs=output_image)
    
    return ui

# Run the interface
if __name__ == "__main__":
    create_ui().launch()