File size: 1,369 Bytes
2002818 2ece525 2002818 2ece525 2002818 |
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 necessary libraries
from together import Together
import gradio as gr
import os
# Retrieve API key from Hugging Face Secrets
API_KEY = os.getenv("MY_API_KEY")
client = Together(api_key=API_KEY)
def generate_image(prompt, model="black-forest-labs/FLUX.1-schnell", steps=4):
try:
if not prompt.strip():
return None # Avoid empty prompts
print(f"Generating image for: {prompt}")
response = client.images.generate(
prompt=prompt,
model=model,
steps=steps
)
# Extract the image URL
image_url = response.data[0].url
print("Generated Image URL:", image_url)
return image_url
except Exception as e:
print("Error generating image:", e)
return None
# Gradio Interface Setup
with gr.Blocks() as app:
gr.Markdown("## Real-Time AI Image Generator 🚀")
gr.Markdown("Start typing a prompt below to generate images in real-time using FLUX Schnell model.")
with gr.Row():
prompt_box = gr.Textbox(label="Enter your prompt", placeholder="A horse on the moon")
image_output = gr.Image(label="Generated Image")
# Automatically trigger image generation when input changes
prompt_box.change(fn=generate_image, inputs=prompt_box, outputs=image_output)
# Launch the Gradio app
app.launch()
|