Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
from together import Together
|
| 3 |
+
from IPython.display import Image, display
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Retrieve API key from Hugging Face Secrets
|
| 7 |
+
API_KEY = os.getenv("MY_API_KEY")
|
| 8 |
+
|
| 9 |
+
client = Together(api_key=API_KEY)
|
| 10 |
+
|
| 11 |
+
def generate_image(prompt, model="black-forest-labs/FLUX.1-schnell", steps=4):
|
| 12 |
+
try:
|
| 13 |
+
if not prompt.strip():
|
| 14 |
+
return None # Avoid empty prompts
|
| 15 |
+
print(f"Generating image for: {prompt}")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
response = client.images.generate(
|
| 19 |
+
prompt=prompt,
|
| 20 |
+
model=model,
|
| 21 |
+
steps=steps
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Extract the image URL
|
| 25 |
+
image_url = response.data[0].url
|
| 26 |
+
print("Generated Image URL:", image_url)
|
| 27 |
+
return image_url
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print("Error generating image:", e)
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
# Gradio Interface Setup
|
| 33 |
+
with gr.Blocks() as app:
|
| 34 |
+
gr.Markdown("## Real-Time AI Image Generator 🚀")
|
| 35 |
+
gr.Markdown("Start typing a prompt below to generate images in real-time using Together AI's FLUX model.")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
prompt_box = gr.Textbox(label="Enter your prompt", placeholder="A horse on the moon")
|
| 39 |
+
image_output = gr.Image(label="Generated Image")
|
| 40 |
+
|
| 41 |
+
# Automatically trigger image generation when input changes
|
| 42 |
+
prompt_box.change(fn=generate_image, inputs=prompt_box, outputs=image_output)
|
| 43 |
+
|
| 44 |
+
# Launch the Gradio app
|
| 45 |
+
app.launch()
|
| 46 |
+
|
| 47 |
+
|