File size: 1,819 Bytes
a1c995c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
import gradio as gr
from huggingface_hub import InferenceClient

# List of available models (custom models included)
model_options = {
    "Stable Diffusion 2": "stabilityai/stable-diffusion-2",
    "Stable Diffusion 1.5": "runwayml/stable-diffusion-v1-5",
    "DALL-E Mini": "dalle-mini/dalle-mini",
    "FLUX 1.0 (black-forest-labs)": "black-forest-labs/FLUX.1-dev",
    "Pony Diffusion": "AstraliteHeart/ponydiffusion"
}

# Initialize Hugging Face Inference Client
def get_client(model_name):
    return InferenceClient(model=model_name)

# Function to generate the image based on selected model and prompt
def generate_image(prompt, model_name):
    client = get_client(model_options[model_name])
    response = client.text_to_image(prompt, guidance_scale=7.5)
    return response

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Text to Image Generator using Hugging Face Inference Client")
    
    with gr.Row():
        with gr.Column():
            # Dropdown for model selection
            model_dropdown = gr.Dropdown(
                label="Select Model",
                choices=list(model_options.keys()),  # Display model names
                value="Stable Diffusion 2",  # Default model
            )
            
            # Input for text prompt
            prompt_input = gr.Textbox(label="Enter your prompt", placeholder="Describe the image you want...")
            
            # Button to generate image
            generate_button = gr.Button("Generate Image")
        
        with gr.Column():
            # Image output
            image_output = gr.Image(label="Generated Image")
    
    # Link the button click to the function
    generate_button.click(generate_image, inputs=[prompt_input, model_dropdown], outputs=image_output)

# Launch the Gradio app
demo.launch()