Spaces:
Runtime error
Runtime error
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() | |