File size: 1,685 Bytes
7808d8a
 
 
 
 
 
 
 
 
 
 
 
1f533df
7808d8a
 
 
 
 
 
 
87437f8
7808d8a
 
87437f8
7808d8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f533df
7808d8a
 
 
 
 
 
e76e772
87437f8
7808d8a
 
 
 
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
import os
import gradio as gr
from together import Together
import base64
from PIL import Image
import io

def generate_image(api_key, prompt):
    # Use the provided API key or fall back to the environment variable
    api_key = api_key or os.environ.get('TOGETHER_API_KEY')
    
    if not api_key:
        return None, "Please provide a valid Together API key"
    
    try:
        # Initialize the Together client with the API key
        client = Together(api_key=api_key)
        
        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"
        )
        
        # Decode the base64 image
        image_data = base64.b64decode(response.data[0].b64_json)
        image = Image.open(io.BytesIO(image_data))
        
        return image, "Image generated successfully!"
    except Exception as e:
        return None, f"An error occurred: {str(e)}"

# Create the Gradio interface
iface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(type="password", label="Together API Key"),
        gr.Textbox(lines=3, placeholder="Enter your image prompt here...", label="Prompt")
    ],
    outputs=[
        gr.Image(label="Generated Image"),
        gr.Textbox(label="Status")
    ],
    title="Image Generation with FLUX Schnell (FREE on Together AI for 3 months)",
    description="Generate images using the FLUX Schnell model via the Together API. You can provide your API key here – it's free until the end of the year!"
)

# Launch the interface
iface.launch()