bestroi commited on
Commit
f650538
·
1 Parent(s): 99f52af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -13
app.py CHANGED
@@ -1,17 +1,35 @@
 
1
  import gradio as gr
2
  import pandas as pd
 
 
3
 
4
- # Function to handle the user's input and store it in the DataFrame
5
- def describe_image(image, desc1, desc2, desc3, csv_file_path):
6
- # Check if the file exists, and if not, create it
7
- if not pd.api.types.is_file_like(csv_file_path):
8
- data = pd.DataFrame(columns=["Image", "Description 1", "Description 2", "Description 3"])
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  else:
10
- data = pd.read_csv(csv_file_path)
 
 
 
 
 
11
 
12
- data = data.append({"Image": image, "Description 1": desc1, "Description 2": desc2, "Description 3": desc3},
13
- ignore_index=True)
14
- data.to_csv(csv_file_path, index=False) # Save the data to the specified CSV file
15
  return None # Return None for no specific output
16
 
17
  iface = gr.Interface(
@@ -20,14 +38,17 @@ iface = gr.Interface(
20
  gr.Image(type="pil", label="Upload an Image"),
21
  "text",
22
  "text",
23
- "text",
24
- "text" # Add an input field for specifying the CSV file path
25
  ],
26
- outputs=gr.Info("Data saved successfully"), # Display the success message
27
  live=False,
28
  title="Image Description App",
29
  description="Upload an image and provide three descriptions for it.",
30
  allow_flagging=False,
 
 
 
 
31
  )
32
 
33
- iface.launch()
 
1
+ import os
2
  import gradio as gr
3
  import pandas as pd
4
+ from pydrive.auth import GoogleAuth
5
+ from pydrive.drive import GoogleDrive
6
 
7
+ # Load credentials from environment variables
8
+ gauth = GoogleAuth()
9
+ gauth.LocalWebserverAuth()
10
+
11
+ # Initialize Google Drive
12
+ drive = GoogleDrive(gauth)
13
+
14
+ # Function to handle the user's input and store it in a Google Drive file
15
+ def describe_image(image, desc1, desc2, desc3):
16
+ data = pd.DataFrame(columns=["Image", "Description 1", "Description 2", "Description 3"])
17
+ data = data.append({"Image": image, "Description 1": desc1, "Description 2": desc2, "Description 3": desc3}, ignore_index=True)
18
+
19
+ # Create or get a reference to the file on Google Drive
20
+ file_list = drive.ListFile({'q': "'" + os.environ.get("GDRIVE_FOLDER_ID") + "' in parents and trashed=false"}).GetList()
21
+ for file in file_list:
22
+ if file['title'] == os.environ.get("CSV_FILENAME"):
23
+ gdrive_file = file
24
+ break
25
  else:
26
+ gdrive_file = drive.CreateFile({'title': os.environ.get("CSV_FILENAME"), "parents": [{"kind": "drive#fileLink", "id": os.environ.get("GDRIVE_FOLDER_ID")}]})
27
+
28
+ # Write the data to the file and save it on Google Drive
29
+ data.to_csv('temp.csv', index=False)
30
+ gdrive_file.SetContentFile('temp.csv')
31
+ gdrive_file.Upload()
32
 
 
 
 
33
  return None # Return None for no specific output
34
 
35
  iface = gr.Interface(
 
38
  gr.Image(type="pil", label="Upload an Image"),
39
  "text",
40
  "text",
41
+ "text"
 
42
  ],
43
+ outputs=gr.Info("Data saved successfully on Google Drive"), # Display the success message
44
  live=False,
45
  title="Image Description App",
46
  description="Upload an image and provide three descriptions for it.",
47
  allow_flagging=False,
48
+ theme=gr.themes.Base(primary_hue="green").set(
49
+ button_primary_background_fill="*primary_400",
50
+ button_primary_background_fill_hover="*primary_300",
51
+ )
52
  )
53
 
54
+ iface.launch()