veasnakao commited on
Commit
a530517
·
verified ·
1 Parent(s): f7618f2

update origin

Browse files
Files changed (1) hide show
  1. app.py +32 -54
app.py CHANGED
@@ -1,63 +1,41 @@
1
- import os
2
  import boto3
3
- import io
4
  from PIL import Image
5
- import requests
6
- import gradio as gr
 
 
 
 
 
 
 
7
 
8
- # AWS S3 configuration
9
  bucket_name = 'flux-image-generation'
10
 
11
- # Retrieve AWS credentials from environment variables
12
- aws_access_key_id = os.getenv('AKIA34AOT6TNS2T5EZOB')
13
- aws_secret_access_key = os.getenv('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')
14
 
15
- # Initialize S3 client
16
- s3 = boto3.client(
17
- 's3',
18
- aws_access_key_id=aws_access_key_id,
19
- aws_secret_access_key=aws_secret_access_key
20
- )
21
 
22
- # Load the Gradio model (replace with the actual Gradio loading function if needed)
23
- model = gr.load("models/black-forest-labs/FLUX.1-dev")
24
-
25
- def generate_and_store_image(prompt):
26
- try:
27
- # Generate an image URL from the prompt using the FLUX model
28
- # Assuming the model returns a URL to the generated image
29
- image_url = model(prompt)
30
-
31
- # Download the image from the URL
32
- response = requests.get(image_url)
33
- response.raise_for_status() # Check for HTTP errors
34
-
35
- # Convert downloaded image to PIL Image
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
- # Launch the Gradio interface
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()