File size: 1,937 Bytes
f9cf354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import base64
import io
from PIL import Image
from together import Together

# Initialize the Together AI client
client = Together()

def generate_image(prompt):
    try:
        # Call the Together API to generate an image
        response = client.images.generate(
            prompt=prompt,
            model="black-forest-labs/FLUX.1-schnell-Free",
            width=1024,
            height=768,
            steps=4,
            n=1,
            response_format="b64_json"
        )
        
        # Get the base64 encoded image
        image_b64 = response.data[0].b64_json
        
        # Convert base64 to image
        image_data = base64.b64decode(image_b64)
        image = Image.open(io.BytesIO(image_data))
        
        return image
    except Exception as e:
        return f"Error generating image: {str(e)}"

# Create the Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# Together AI Image Generator")
    gr.Markdown("Generate images using the FLUX.1-schnell-Free model from Together AI")
    
    with gr.Row():
        with gr.Column():
            prompt_input = gr.Textbox(
                label="Enter your prompt",
                placeholder="A beautiful sunset over mountains...",
                lines=3
            )
            generate_button = gr.Button("Generate Image")
        
        with gr.Column():
            image_output = gr.Image(label="Generated Image")
    
    generate_button.click(
        fn=generate_image,
        inputs=prompt_input,
        outputs=image_output
    )
    
    gr.Markdown("""
    ## Instructions
    1. Enter a descriptive prompt in the text box
    2. Click "Generate Image" 
    3. Wait for the image to be generated
    
    Note: You'll need to have your Together AI API key set up properly, 
    usually through environment variables or a config file.
    """)

# Launch the app
if __name__ == "__main__":
    app.launch()