Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +14 -0
- main.py +75 -0
- 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,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from fastapi import FastAPI
|
3 |
+
import gspread
|
4 |
+
from google.oauth2.service_account import Credentials
|
5 |
+
from google.auth.exceptions import GoogleAuthError
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Define the Academic and Non-Academic panic buttons
|
9 |
+
academic_panic_buttons = ["MISSED CLASSES", "BACKLOGS", "LACK OF MOTIVATION", "NOT UNDERSTANDING", "BAD MARKS"]
|
10 |
+
non_academic_panic_buttons = ["EMOTIONAL FACTORS", "PROCRASTINATE", "LOST INTEREST", "LACK OF FOCUS", "GOALS NOT ACHIEVED", "LACK OF DISCIPLINE"]
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Function to fetch the credentials
|
15 |
+
def get_credentials_from_env():
|
16 |
+
service_account_info = {
|
17 |
+
"type": os.getenv("SERVICE_ACCOUNT_TYPE"),
|
18 |
+
"project_id": os.getenv("PROJECT_ID"),
|
19 |
+
"private_key_id": os.getenv("PRIVATE_KEY_ID"),
|
20 |
+
"private_key": os.getenv("PRIVATE_KEY").replace('\\n', '\n'),
|
21 |
+
"client_email": os.getenv("CLIENT_EMAIL"),
|
22 |
+
"client_id": os.getenv("CLIENT_ID"),
|
23 |
+
"auth_uri": os.getenv("AUTH_URI"),
|
24 |
+
"token_uri": os.getenv("TOKEN_URI"),
|
25 |
+
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
|
26 |
+
"client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
|
27 |
+
"universe_domain": os.getenv("UNIVERSE_DOMAIN")
|
28 |
+
}
|
29 |
+
|
30 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
31 |
+
creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
|
32 |
+
return creds
|
33 |
+
|
34 |
+
# Main function to fetch the panic button occurrences
|
35 |
+
@app.get("/panic-button-occurances")
|
36 |
+
def get_panic_button_occurrences():
|
37 |
+
try:
|
38 |
+
# Set up credentials
|
39 |
+
creds = get_credentials_from_env()
|
40 |
+
client = gspread.authorize(creds)
|
41 |
+
|
42 |
+
# Open the Google Sheet
|
43 |
+
sheet = client.open_by_url('https://docs.google.com/spreadsheets/d/1nFZGkCvRV6qS-mhsORhX3dxI0JSge32_UwWgWKl3eyw/edit?gid=0#gid=0').worksheet('Sheet1')
|
44 |
+
|
45 |
+
# Get all values from the sheet
|
46 |
+
data = sheet.get_all_values()
|
47 |
+
|
48 |
+
# Initialize the lists with panic button names and 0 counts
|
49 |
+
academic_list = {button: 0 for button in academic_panic_buttons}
|
50 |
+
non_academic_list = {button: 0 for button in non_academic_panic_buttons}
|
51 |
+
|
52 |
+
# Iterate through all rows in the data
|
53 |
+
for row in data:
|
54 |
+
for panic_button in row:
|
55 |
+
# Stop when an empty column is encountered
|
56 |
+
if not panic_button:
|
57 |
+
break
|
58 |
+
|
59 |
+
# Check and update counts for academic and non-academic panic buttons
|
60 |
+
if panic_button in academic_list:
|
61 |
+
academic_list[panic_button] += 1
|
62 |
+
elif panic_button in non_academic_list:
|
63 |
+
non_academic_list[panic_button] += 1
|
64 |
+
|
65 |
+
return {
|
66 |
+
"academic": academic_list,
|
67 |
+
"non_academic": non_academic_list
|
68 |
+
}
|
69 |
+
|
70 |
+
except GoogleAuthError as e:
|
71 |
+
return {"error": f"Authentication error: {e}"}
|
72 |
+
except gspread.exceptions.SpreadsheetNotFound:
|
73 |
+
return {"error": "Spreadsheet not found. Please check the URL."}
|
74 |
+
except Exception as e:
|
75 |
+
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
|