Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +101 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from together import Together
|
| 6 |
+
|
| 7 |
+
def generate_image(api_key, prompt, width, height, steps):
|
| 8 |
+
try:
|
| 9 |
+
# Initialize the Together AI client with the provided API key
|
| 10 |
+
client = Together(api_key=api_key)
|
| 11 |
+
|
| 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=width,
|
| 17 |
+
height=height,
|
| 18 |
+
steps=steps,
|
| 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(scale=1):
|
| 41 |
+
api_key_input = gr.Textbox(
|
| 42 |
+
label="Together API Key",
|
| 43 |
+
placeholder="Enter your Together API key here...",
|
| 44 |
+
type="password" # Mask the API key for security
|
| 45 |
+
)
|
| 46 |
+
prompt_input = gr.Textbox(
|
| 47 |
+
label="Enter your prompt",
|
| 48 |
+
placeholder="A beautiful sunset over mountains...",
|
| 49 |
+
lines=3
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
width_input = gr.Number(
|
| 54 |
+
label="Width",
|
| 55 |
+
value=1024,
|
| 56 |
+
minimum=256,
|
| 57 |
+
maximum=2048,
|
| 58 |
+
step=64
|
| 59 |
+
)
|
| 60 |
+
height_input = gr.Number(
|
| 61 |
+
label="Height",
|
| 62 |
+
value=768,
|
| 63 |
+
minimum=256,
|
| 64 |
+
maximum=2048,
|
| 65 |
+
step=64
|
| 66 |
+
)
|
| 67 |
+
steps_input = gr.Slider(
|
| 68 |
+
label="Steps",
|
| 69 |
+
minimum=1,
|
| 70 |
+
maximum=50,
|
| 71 |
+
value=4,
|
| 72 |
+
step=1
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
generate_button = gr.Button("Generate Image")
|
| 76 |
+
|
| 77 |
+
with gr.Column(scale=1):
|
| 78 |
+
image_output = gr.Image(label="Generated Image")
|
| 79 |
+
|
| 80 |
+
generate_button.click(
|
| 81 |
+
fn=generate_image,
|
| 82 |
+
inputs=[api_key_input, prompt_input, width_input, height_input, steps_input],
|
| 83 |
+
outputs=image_output
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
gr.Markdown("""
|
| 87 |
+
## Instructions
|
| 88 |
+
1. Enter your Together API Key (get one from https://www.together.ai)
|
| 89 |
+
2. Enter a descriptive prompt in the text box
|
| 90 |
+
3. Adjust the width, height, and steps as needed
|
| 91 |
+
- Width and height control the dimensions of the generated image
|
| 92 |
+
- Steps control the number of diffusion steps (higher = more detail but slower)
|
| 93 |
+
4. Click "Generate Image"
|
| 94 |
+
5. Wait for the image to be generated
|
| 95 |
+
|
| 96 |
+
Note: Your API key is not stored and is only used for the current session.
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
# Launch the app
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
together
|
| 2 |
+
pillow
|