File size: 3,294 Bytes
114c45d 1307279 114c45d 1307279 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d f12470b 114c45d 2f5cdae 114c45d f12470b 114c45d |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import os
import gradio as gr
from PIL import Image
import io
from utils import query_hf_api
def generate_image(prompt: str) -> Image.Image:
"""
Generate an image from a text prompt.
Args:
prompt (str): Text description for image generation
Returns:
Image.Image: Generated PIL Image
"""
try:
# Generate image bytes
image_bytes = query_hf_api(prompt)
# Convert to PIL Image
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
return image
except Exception as e:
print(f"Image generation error: {e}")
return None
def create_gradio_interface():
"""
Create and configure the Gradio interface.
Returns:
gr.Blocks: Configured Gradio interface
"""
with gr.Blocks(
theme=gr.themes.Soft(),
title="π¨ AI Image Generator"
) as demo:
# Title and Description
gr.Markdown("# π¨ AI Image Generator")
gr.Markdown("Generate stunning images from your text prompts using AI!")
# Input and Output Components
with gr.Row():
with gr.Column(scale=3):
# Prompt Input
text_input = gr.Textbox(
label="Enter your image prompt",
placeholder="e.g., 'Astronaut riding a bike on Mars at sunset'",
lines=3
)
# Advanced Options
with gr.Accordion("Advanced Options", open=False):
steps_slider = gr.Slider(
minimum=10,
maximum=100,
value=50,
step=1,
label="Inference Steps"
)
guidance_slider = gr.Slider(
minimum=1,
maximum=20,
value=7.5,
step=0.5,
label="Guidance Scale"
)
# Generate Button
generate_button = gr.Button("β¨ Generate Image", variant="primary")
# Output Image Display
with gr.Column(scale=4):
output_image = gr.Image(
label="Generated Image",
type="pil",
interactive=False
)
# Error Handling Output
error_output = gr.Textbox(label="Status", visible=False)
# Event Handlers
generate_button.click(
fn=generate_image,
inputs=[text_input],
outputs=[output_image, error_output],
api_name="generate"
)
return demo
def main():
"""
Main entry point for the Gradio application.
"""
try:
demo = create_gradio_interface()
demo.launch(
server_name="0.0.0.0", # Listen on all network interfaces
server_port=7860, # Default Gradio port
share=False # Set to True if you want a public link
)
except Exception as e:
print(f"Error launching Gradio app: {e}")
if __name__ == "__main__":
main() |