Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from together import Together
|
4 |
+
import base64
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
|
8 |
+
def generate_image(api_key, prompt):
|
9 |
+
# Use the provided API key or fall back to the environment variable
|
10 |
+
api_key = api_key or os.environ.get('TOGETHER_API_KEY')
|
11 |
+
|
12 |
+
if not api_key:
|
13 |
+
return None, "Please provide a valid Together API key or set the TOGETHER_API_KEY environment variable."
|
14 |
+
|
15 |
+
try:
|
16 |
+
# Initialize the Together client with the API key
|
17 |
+
client = Together(api_key=api_key)
|
18 |
+
|
19 |
+
response = client.images.generate(
|
20 |
+
prompt=prompt,
|
21 |
+
model="black-forest-labs/FLUX.1.1-pro",
|
22 |
+
width=1024,
|
23 |
+
height=768,
|
24 |
+
steps=1,
|
25 |
+
n=1,
|
26 |
+
response_format="b64_json"
|
27 |
+
)
|
28 |
+
|
29 |
+
# Decode the base64 image
|
30 |
+
image_data = base64.b64decode(response.data[0].b64_json)
|
31 |
+
image = Image.open(io.BytesIO(image_data))
|
32 |
+
|
33 |
+
return image, "Image generated successfully!"
|
34 |
+
except Exception as e:
|
35 |
+
return None, f"An error occurred: {str(e)}"
|
36 |
+
|
37 |
+
# Create the Gradio interface
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=generate_image,
|
40 |
+
inputs=[
|
41 |
+
gr.Textbox(type="password", label="Together API Key (optional if set as environment variable)"),
|
42 |
+
gr.Textbox(lines=3, placeholder="Enter your image prompt here...", label="Prompt")
|
43 |
+
],
|
44 |
+
outputs=[
|
45 |
+
gr.Image(label="Generated Image"),
|
46 |
+
gr.Textbox(label="Status")
|
47 |
+
],
|
48 |
+
title="Image Generation with FLUX.1.1-pro",
|
49 |
+
description="Generate images using the FLUX.1.1-pro model via the Together API. You can provide your API key here or set it as the TOGETHER_API_KEY environment variable."
|
50 |
+
)
|
51 |
+
|
52 |
+
# Launch the interface
|
53 |
+
iface.launch()
|