Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import base64
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
from together import Together
|
6 |
+
|
7 |
+
# Initialize the Together AI client
|
8 |
+
client = Together()
|
9 |
+
|
10 |
+
def generate_image(prompt):
|
11 |
+
try:
|
12 |
+
# Call the Together API to generate an image
|
13 |
+
response = client.images.generate(
|
14 |
+
prompt=prompt,
|
15 |
+
model="black-forest-labs/FLUX.1-schnell-Free",
|
16 |
+
width=1024,
|
17 |
+
height=768,
|
18 |
+
steps=4,
|
19 |
+
n=1,
|
20 |
+
response_format="b64_json"
|
21 |
+
)
|
22 |
+
|
23 |
+
# Get the base64 encoded image
|
24 |
+
image_b64 = response.data[0].b64_json
|
25 |
+
|
26 |
+
# Convert base64 to image
|
27 |
+
image_data = base64.b64decode(image_b64)
|
28 |
+
image = Image.open(io.BytesIO(image_data))
|
29 |
+
|
30 |
+
return image
|
31 |
+
except Exception as e:
|
32 |
+
return f"Error generating image: {str(e)}"
|
33 |
+
|
34 |
+
# Create the Gradio interface
|
35 |
+
with gr.Blocks() as app:
|
36 |
+
gr.Markdown("# Together AI Image Generator")
|
37 |
+
gr.Markdown("Generate images using the FLUX.1-schnell-Free model from Together AI")
|
38 |
+
|
39 |
+
with gr.Row():
|
40 |
+
with gr.Column():
|
41 |
+
prompt_input = gr.Textbox(
|
42 |
+
label="Enter your prompt",
|
43 |
+
placeholder="A beautiful sunset over mountains...",
|
44 |
+
lines=3
|
45 |
+
)
|
46 |
+
generate_button = gr.Button("Generate Image")
|
47 |
+
|
48 |
+
with gr.Column():
|
49 |
+
image_output = gr.Image(label="Generated Image")
|
50 |
+
|
51 |
+
generate_button.click(
|
52 |
+
fn=generate_image,
|
53 |
+
inputs=prompt_input,
|
54 |
+
outputs=image_output
|
55 |
+
)
|
56 |
+
|
57 |
+
gr.Markdown("""
|
58 |
+
## Instructions
|
59 |
+
1. Enter a descriptive prompt in the text box
|
60 |
+
2. Click "Generate Image"
|
61 |
+
3. Wait for the image to be generated
|
62 |
+
|
63 |
+
Note: You'll need to have your Together AI API key set up properly,
|
64 |
+
usually through environment variables or a config file.
|
65 |
+
""")
|
66 |
+
|
67 |
+
# Launch the app
|
68 |
+
if __name__ == "__main__":
|
69 |
+
app.launch()
|