SathvikGanta commited on
Commit
ab32f57
·
verified ·
1 Parent(s): 2045d48

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import uuid
4
+
5
+ from google.oauth2 import service_account
6
+ from googleapiclient.discovery import build
7
+ from googleapiclient.http import MediaFileUpload
8
+
9
+ # === CONFIG ===
10
+ FOLDER_ID = "YOUR_GOOGLE_DRIVE_FOLDER_ID"
11
+ SERVICE_ACCOUNT_FILE = "service_account.json" # Upload this securely in your Space
12
+
13
+ # === Upload to Google Drive ===
14
+ def upload_to_drive(local_file_path, filename_on_drive):
15
+ SCOPES = ['https://www.googleapis.com/auth/drive.file']
16
+ credentials = service_account.Credentials.from_service_account_file(
17
+ SERVICE_ACCOUNT_FILE, scopes=SCOPES)
18
+ service = build('drive', 'v3', credentials=credentials)
19
+
20
+ file_metadata = {
21
+ 'name': filename_on_drive,
22
+ 'parents': [FOLDER_ID]
23
+ }
24
+ media = MediaFileUpload(local_file_path, mimetype='image/jpeg')
25
+
26
+ file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
27
+ return f"Uploaded to Drive with ID: {file.get('id')}"
28
+
29
+ # === Gradio Camera Interface ===
30
+ def handle_photo(image):
31
+ if image is None:
32
+ return "No image captured."
33
+
34
+ # Save image locally
35
+ filename = f"captured_{uuid.uuid4().hex}.jpg"
36
+ image.save(filename)
37
+
38
+ # Upload to Drive
39
+ result = upload_to_drive(filename, filename)
40
+
41
+ # Clean up (optional)
42
+ os.remove(filename)
43
+
44
+ return result
45
+
46
+ iface = gr.Interface(fn=handle_photo,
47
+ inputs=gr.Image(source="camera", tool=None),
48
+ outputs="text",
49
+ title="Capture and Upload Photo to Drive")
50
+
51
+ iface.launch()