bestroi commited on
Commit
224553a
·
1 Parent(s): 9d2616d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -14
app.py CHANGED
@@ -3,28 +3,69 @@ import pandas as pd
3
  from PIL import Image
4
  import os
5
  import uuid
 
6
 
7
- # Create a directory to store the images
8
  image_directory = "images"
9
  os.makedirs(image_directory, exist_ok=True)
10
 
11
- # Define the path to the CSV file in the current directory
12
- csv_file_path = "descriptions.csv"
 
 
 
 
 
13
 
14
- # Initialize an empty list to store the data
15
  data = []
16
 
17
- # Function to handle the user's input and store it in the list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def describe_image(image, filename, description, copyright):
19
  global data
20
- # Generate a unique filename for each image
21
- image_filename = os.path.join(image_directory, f"{str(uuid.uuid4())}.jpg")
22
- image.save(image_filename)
23
- data.append({"Image": image_filename, "Filename": filename, "Description": description, "Copyright": copyright})
24
- # Convert the list to a DataFrame
 
 
 
 
 
 
 
 
 
 
 
25
  data_df = pd.DataFrame(data)
26
- data_df.to_csv(csv_file_path, index=False) # Save the data to a CSV file in the current directory
27
- return None # Return None for no specific output
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  iface = gr.Interface(
30
  fn=describe_image,
@@ -34,7 +75,7 @@ iface = gr.Interface(
34
  "text",
35
  "text"
36
  ],
37
- outputs=gr.Info("Data saved successfully"), # Display the success message
38
  live=False,
39
  title="Image Description App",
40
  description="Upload an image and provide three descriptions for it.",
@@ -45,4 +86,3 @@ iface = gr.Interface(
45
  )
46
  )
47
 
48
- iface.launch(share=True)
 
3
  from PIL import Image
4
  import os
5
  import uuid
6
+ import boto3
7
 
 
8
  image_directory = "images"
9
  os.makedirs(image_directory, exist_ok=True)
10
 
11
+ aws_access_key = os.environ['AWS_ACCESS_KEY']
12
+ aws_secret_key = os.environ['AWS_SECRET_KEY']
13
+ aws_region = os.environ['AWS_REGION']
14
+
15
+ s3 = boto3.client('s3', aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key, region_name=aws_region)
16
+
17
+ s3_bucket_name = os.environ['S3_BUCKET_NAME']
18
 
 
19
  data = []
20
 
21
+
22
+ base_csv_file_path = "descriptions"
23
+ csv_file_path = f"{base_csv_file_path}.csv"
24
+
25
+
26
+ def file_exists(file_path):
27
+ return os.path.isfile(file_path)
28
+
29
+
30
+ def generate_unique_csv_file():
31
+ count = 1
32
+ while file_exists(f"{base_csv_file_path}{count}.csv"):
33
+ count += 1
34
+ return f"{base_csv_file_path}{count}.csv"
35
+
36
+
37
  def describe_image(image, filename, description, copyright):
38
  global data
39
+
40
+
41
+ image_filename = f"{str(uuid.uuid4())}.jpg"
42
+
43
+
44
+ local_image_path = os.path.join(image_directory, image_filename)
45
+ image.save(local_image_path)
46
+
47
+
48
+ s3.upload_file(local_image_path, s3_bucket_name, f"images/{image_filename}")
49
+
50
+
51
+ os.remove(local_image_path)
52
+
53
+
54
+ data.append({"Image": image_filename, "Filename": filename, "Description": description, "Copyright": copyright})
55
  data_df = pd.DataFrame(data)
56
+
57
+
58
+ if file_exists(csv_file_path):
59
+
60
+ new_csv_file_path = generate_unique_csv_file()
61
+ data_df.to_csv(new_csv_file_path, index=False)
62
+ s3.upload_file(new_csv_file_path, s3_bucket_name, new_csv_file_path)
63
+ else:
64
+
65
+ data_df.to_csv(csv_file_path, index=False)
66
+ s3.upload_file(csv_file_path, s3_bucket_name, csv_file_path)
67
+
68
+ return None
69
 
70
  iface = gr.Interface(
71
  fn=describe_image,
 
75
  "text",
76
  "text"
77
  ],
78
+ outputs=gr.Info("Data saved successfully"),
79
  live=False,
80
  title="Image Description App",
81
  description="Upload an image and provide three descriptions for it.",
 
86
  )
87
  )
88