Spaces:
Running
on
Zero
Running
on
Zero
update origin
Browse files
app.py
CHANGED
@@ -1,63 +1,41 @@
|
|
1 |
-
import
|
2 |
import boto3
|
3 |
-
import io
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
# AWS S3 configuration
|
9 |
bucket_name = 'flux-image-generation'
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
aws_secret_access_key = os.getenv('aCXXKADXHcOgAIjtp3Nv9lK6uCZpm9D5CcQjWhb0')
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
img = Image.open(io.BytesIO(response.content))
|
37 |
-
|
38 |
-
# Save image to in-memory file
|
39 |
-
in_mem_file = io.BytesIO()
|
40 |
-
img.save(in_mem_file, format='PNG')
|
41 |
-
in_mem_file.seek(0)
|
42 |
-
|
43 |
-
# Upload image to S3
|
44 |
-
image_name = f'{prompt.replace(" ", "_")}.png' # Unique image name based on prompt
|
45 |
-
s3.upload_fileobj(in_mem_file, bucket_name, image_name)
|
46 |
-
|
47 |
-
# Return S3 URL of the image
|
48 |
-
s3_url = f'https://{bucket_name}.s3.amazonaws.com/{image_name}'
|
49 |
-
return s3_url
|
50 |
-
except Exception as e:
|
51 |
-
return f"Error generating or uploading image: {e}"
|
52 |
-
|
53 |
-
# Set up Gradio interface using the latest API
|
54 |
-
iface = gr.Interface(
|
55 |
-
fn=generate_and_store_image,
|
56 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter a prompt for the FLUX model"),
|
57 |
-
outputs=gr.Textbox(), # Change to `gr.Textbox()` for text output
|
58 |
-
title="Generate and Store Image with FLUX Model",
|
59 |
-
description="Enter a prompt, and the image will be generated and saved to S3."
|
60 |
-
)
|
61 |
|
62 |
-
|
63 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
import boto3
|
|
|
3 |
from PIL import Image
|
4 |
+
import io
|
5 |
+
|
6 |
+
# Define your AWS S3 setup here
|
7 |
+
s3_client = boto3.client(
|
8 |
+
's3',
|
9 |
+
aws_access_key_id='AKIA34AOT6TNS2T5EZOB',
|
10 |
+
aws_secret_access_key='aCXXKADXHcOgAIjtp3Nv9lK6uCZpm9D5CcQjWhb0',
|
11 |
+
region_name='ap-southeast-1'
|
12 |
+
)
|
13 |
|
|
|
14 |
bucket_name = 'flux-image-generation'
|
15 |
|
16 |
+
def upload_to_s3(image, bucket_name, s3_key):
|
17 |
+
s3_client.upload_fileobj(image, bucket_name, s3_key)
|
|
|
18 |
|
19 |
+
def process_and_upload_image(image):
|
20 |
+
# Convert image to bytes
|
21 |
+
image_bytes = io.BytesIO()
|
22 |
+
image.save(image_bytes, format='PNG')
|
23 |
+
image_bytes.seek(0)
|
|
|
24 |
|
25 |
+
# Generate a unique key for the S3 object
|
26 |
+
s3_key = f'generated_images/{str(uuid.uuid4())}.png'
|
27 |
+
|
28 |
+
# Upload the image to S3
|
29 |
+
upload_to_s3(image_bytes, bucket_name, s3_key)
|
30 |
+
|
31 |
+
return s3_key # Return the S3 key or URL for reference
|
32 |
+
|
33 |
+
# Define your Gradio interface
|
34 |
+
def generate_image():
|
35 |
+
# Your code to generate an image
|
36 |
+
image = ... # This should be the generated image
|
37 |
+
s3_key = process_and_upload_image(image)
|
38 |
+
return s3_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
iface = gr.Interface(fn=generate_image, inputs=[], outputs="text")
|
41 |
iface.launch()
|