Rakshitjan commited on
Commit
24e8d30
·
verified ·
1 Parent(s): 17eb177

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +14 -0
  2. main.py +94 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /code
7
+
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
+
12
+ COPY . .
13
+
14
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ import gspread
3
+ from google.oauth2.service_account import Credentials
4
+ from google.auth.exceptions import GoogleAuthError
5
+ import os
6
+ from typing import List, Dict
7
+
8
+ app = FastAPI()
9
+
10
+ def get_credentials():
11
+ """Get Google Sheets API credentials from environment variables."""
12
+ try:
13
+ service_account_info = {
14
+ "type": os.getenv("SERVICE_ACCOUNT_TYPE"),
15
+ "project_id": os.getenv("PROJECT_ID"),
16
+ "private_key_id": os.getenv("PRIVATE_KEY_ID"),
17
+ "private_key": os.getenv("PRIVATE_KEY").replace('\\n', '\n'),
18
+ "client_email": os.getenv("CLIENT_EMAIL"),
19
+ "client_id": os.getenv("CLIENT_ID"),
20
+ "auth_uri": os.getenv("AUTH_URI"),
21
+ "token_uri": os.getenv("TOKEN_URI"),
22
+ "auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
23
+ "client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
24
+ "universe_domain": os.getenv("UNIVERSE_DOMAIN")
25
+ }
26
+
27
+ scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
28
+ creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
29
+ return creds
30
+
31
+ except Exception as e:
32
+ print(f"Error getting credentials: {e}")
33
+ return None
34
+
35
+ @app.get("/productivity")
36
+ def get_productivity() -> Dict[str, List[int]]:
37
+ """Endpoint to fetch productivity data from Google Sheets."""
38
+ try:
39
+ creds = get_credentials()
40
+ if creds is None:
41
+ raise Exception("Failed to obtain credentials.")
42
+
43
+ # Authorize the client
44
+ client = gspread.authorize(creds)
45
+
46
+ # Open the Google Sheet
47
+ sheet = client.open_by_url('https://docs.google.com/spreadsheets/d/1EFf2lr4A10nt4RhIqxCD_fxe-l3sXH09II0TEkMmvhA/edit?gid=0#gid=0').worksheet('Sheet1')
48
+
49
+ Yes_productivity = [0] * 11 # List to count 'yes' responses for scores 0-10
50
+ No_productivity = [0] * 11 # List to count 'no' responses for scores 0-10
51
+
52
+ # Get all values from the sheet
53
+ values = sheet.get_all_values()
54
+
55
+ # Iterate through each row in the sheet
56
+ for row in values:
57
+ last_column_value = None
58
+ second_last_value = None
59
+
60
+ # Iterate from the last cell to the first
61
+ for cell in reversed(row):
62
+ if cell: # Check if the cell is not empty
63
+ if last_column_value is None:
64
+ last_column_value = cell # Assign last non-empty value
65
+ elif second_last_value is None:
66
+ second_last_value = cell # Assign second last non-empty value
67
+ break # Exit once both values are found
68
+
69
+ # Check if we have valid last and second last values
70
+ if last_column_value is not None and second_last_value is not None:
71
+ try:
72
+ last_column_value = int(last_column_value) # Convert to integer
73
+ if 0 <= last_column_value <= 10:
74
+ # Update the productivity lists based on second last value
75
+ if second_last_value.lower() == 'yes':
76
+ Yes_productivity[last_column_value] += 1
77
+ elif second_last_value.lower() == 'no':
78
+ No_productivity[last_column_value] += 1
79
+ elif second_last_value.lower() == 'maybe':
80
+ Yes_productivity[last_column_value] += 1
81
+ No_productivity[last_column_value] += 1
82
+ except ValueError:
83
+ print(f"Invalid score '{last_column_value}' in row: {row}")
84
+
85
+ # Return the results as JSON
86
+ return {
87
+ "Yes_Productivity": Yes_productivity,
88
+ "No_Productivity": No_productivity
89
+ }
90
+
91
+ except GoogleAuthError as auth_error:
92
+ return {"error": f"Authentication error: {auth_error}"}
93
+ except Exception as e:
94
+ return {"error": f"An error occurred: {e}"}
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ google-auth
4
+ google-auth-oauthlib
5
+ google-auth-httplib2
6
+ gspread