GVAmaresh
commited on
Commit
·
c87f53a
1
Parent(s):
9748921
ini
Browse files- .gitattributes copy +35 -0
- Dockerfile +13 -0
- Drive_API.pickle +3 -0
- README copy.md +10 -0
- __pycache__/apiConnection.cpython-311.pyc +0 -0
- __pycache__/google.cpython-311.pyc +0 -0
- api_connection/__pycache__/apiConnection.cpython-311.pyc +0 -0
- api_connection/apiConnection.py +63 -0
- app.py +250 -0
- client_secret.json +11 -0
- comparator/__pycache__/compareSummerized.cpython-311.pyc +0 -0
- comparator/__pycache__/report.cpython-311.pyc +0 -0
- comparator/__pycache__/summerized.cpython-311.pyc +0 -0
- comparator/compareSummerized.py +58 -0
- comparator/compare_model/__pycache__/compare.cpython-311.pyc +0 -0
- comparator/compare_model/__pycache__/compare2.cpython-311.pyc +0 -0
- comparator/compare_model/compare.py +42 -0
- comparator/compare_model/compare2.py +25 -0
- comparator/extract/__pycache__/extract.cpython-311.pyc +0 -0
- comparator/extract/extract.py +58 -0
- comparator/report.py +128 -0
- comparator/summerized.py +165 -0
- comparator/text_summerizer/__pycache__/summerize2.cpython-311.pyc +0 -0
- comparator/text_summerizer/summerize2.py +27 -0
- repetitive-project-backend +1 -0
- requirements.txt +15 -0
- token_json.json +1 -0
- token_operation/__pycache__/driveToken.cpython-311.pyc +0 -0
- token_operation/__pycache__/tokenOperation.cpython-311.pyc +0 -0
- token_operation/client_secret.json +11 -0
- token_operation/driveToken.py +62 -0
- token_operation/tokenOperation.py +323 -0
- token_operation/token_secret.json +12 -0
.gitattributes copy
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /
|
4 |
+
|
5 |
+
# Copy requirements.txt and install dependencies
|
6 |
+
COPY ./requirements.txt /requirements.txt
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
8 |
+
|
9 |
+
# Copy the rest of the application files
|
10 |
+
COPY . .
|
11 |
+
|
12 |
+
# Run the application
|
13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
Drive_API.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:39f1df70fd5fc1ab84982447bb71d5501ce5d18ca9e5e7ea3db807b894b129d9
|
3 |
+
size 968
|
README copy.md
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Repetitive Backend
|
3 |
+
emoji: 📈
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: red
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
---
|
9 |
+
|
10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
__pycache__/apiConnection.cpython-311.pyc
ADDED
Binary file (3.8 kB). View file
|
|
__pycache__/google.cpython-311.pyc
ADDED
Binary file (3.15 kB). View file
|
|
api_connection/__pycache__/apiConnection.cpython-311.pyc
ADDED
Binary file (4.15 kB). View file
|
|
api_connection/apiConnection.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import os
|
3 |
+
import datetime
|
4 |
+
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
|
5 |
+
from googleapiclient.discovery import build
|
6 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
|
7 |
+
from google.auth.transport.requests import Request
|
8 |
+
|
9 |
+
|
10 |
+
def token_file_exists(prefix="token_"):
|
11 |
+
files = os.listdir()
|
12 |
+
for file in files:
|
13 |
+
if file.startswith(prefix):
|
14 |
+
return True
|
15 |
+
|
16 |
+
return False
|
17 |
+
|
18 |
+
def Create_Service(client_secret_file, api_name, api_version, *scopes):
|
19 |
+
CLIENT_SECRET_FILE = client_secret_file
|
20 |
+
API_SERVICE_NAME = api_name
|
21 |
+
API_VERSION = api_version
|
22 |
+
SCOPES = [scope for scope in scopes[0]]
|
23 |
+
|
24 |
+
cred = None
|
25 |
+
pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
|
26 |
+
|
27 |
+
if os.path.exists(pickle_file):
|
28 |
+
with open(pickle_file, 'rb') as token:
|
29 |
+
cred = pickle.load(token)
|
30 |
+
|
31 |
+
if not cred or not cred.valid:
|
32 |
+
if cred and cred.expired and cred.refresh_token:
|
33 |
+
cred.refresh(Request())
|
34 |
+
else:
|
35 |
+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
|
36 |
+
cred = flow.run_local_server()
|
37 |
+
|
38 |
+
with open(pickle_file, 'wb') as token:
|
39 |
+
pickle.dump(cred, token)
|
40 |
+
|
41 |
+
try:
|
42 |
+
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
|
43 |
+
return service
|
44 |
+
except Exception as e:
|
45 |
+
print('Unable to connect.')
|
46 |
+
print(e)
|
47 |
+
return None
|
48 |
+
|
49 |
+
def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
|
50 |
+
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
|
51 |
+
return dt
|
52 |
+
|
53 |
+
import os
|
54 |
+
def removeAccount():
|
55 |
+
directory = os.getcwd()
|
56 |
+
files = os.listdir(directory)
|
57 |
+
for file in files:
|
58 |
+
if "token" in file:
|
59 |
+
try:
|
60 |
+
os.remove(os.path.join(directory, file))
|
61 |
+
print(f"File '{file}' deleted successfully.")
|
62 |
+
except Exception as e:
|
63 |
+
print(f"Error deleting file '{file}': {e}")
|
app.py
ADDED
@@ -0,0 +1,250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import uvicorn
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
5 |
+
from fastapi.responses import JSONResponse
|
6 |
+
from typing import Annotated
|
7 |
+
import os
|
8 |
+
import shutil
|
9 |
+
from typing import List
|
10 |
+
from pydantic import BaseModel
|
11 |
+
from api_connection.apiConnection import Create_Service
|
12 |
+
from comparator.report import (
|
13 |
+
AddReport,
|
14 |
+
CheckFolders,
|
15 |
+
DeleteReport,
|
16 |
+
)
|
17 |
+
import json
|
18 |
+
import asyncio
|
19 |
+
from concurrent.futures import ThreadPoolExecutor
|
20 |
+
from api_connection.apiConnection import token_file_exists
|
21 |
+
from comparator.summerized import AddSummary, DeleteSummary, Get_All_Reports
|
22 |
+
|
23 |
+
from api_connection.apiConnection import removeAccount
|
24 |
+
import uuid
|
25 |
+
from comparator.text_summerizer.summerize2 import Summerized_Text
|
26 |
+
from comparator.extract.extract import extract_text_from_pdf
|
27 |
+
|
28 |
+
from comparator.compareSummerized import compareText
|
29 |
+
|
30 |
+
from token_operation.driveToken import Delete_Drive_Token, Create_Drive_Token
|
31 |
+
from token_operation.tokenOperation import Create_Token_Drive, Delete_Token_File, Expire_Token_File
|
32 |
+
|
33 |
+
from pydantic import BaseModel
|
34 |
+
class AccountInfo(BaseModel):
|
35 |
+
oldName: str
|
36 |
+
newName: str
|
37 |
+
|
38 |
+
|
39 |
+
"""
|
40 |
+
Folder Path in drive would be:
|
41 |
+
Fake/reports
|
42 |
+
Fake/summerized
|
43 |
+
|
44 |
+
"""
|
45 |
+
|
46 |
+
class IDRequest(BaseModel):
|
47 |
+
ids: List[str]
|
48 |
+
|
49 |
+
global API_VERSION, CLIENT_FILE_NAME, API_DRIVE, SCOPES
|
50 |
+
CLIENT_FILE_NAME = "./client_secret.json"
|
51 |
+
API_DRIVE = "drive"
|
52 |
+
API_VERSION = "v3"
|
53 |
+
SCOPES = ["https://www.googleapis.com/auth/drive"]
|
54 |
+
|
55 |
+
app = FastAPI(docs_url="/api/docs", openapi_url="/api/openapi.json")
|
56 |
+
app.add_middleware(
|
57 |
+
CORSMiddleware,
|
58 |
+
allow_origins=["*"],
|
59 |
+
allow_credentials=True,
|
60 |
+
allow_methods=["*"],
|
61 |
+
allow_headers=["*"],
|
62 |
+
)
|
63 |
+
class AccountCheck(BaseModel):
|
64 |
+
name: str
|
65 |
+
|
66 |
+
@app.post("/api/isLogin")
|
67 |
+
async def is_login(request: AccountCheck):
|
68 |
+
try:
|
69 |
+
global services
|
70 |
+
services, name = Create_Token_Drive(request.name)
|
71 |
+
if services:
|
72 |
+
return {"message": "Successfully logged in", "data": name, "status":"success"}
|
73 |
+
else:
|
74 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
75 |
+
except Exception as e:
|
76 |
+
print(f"An error occurred: {e}")
|
77 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
78 |
+
|
79 |
+
@app.get("/api/check-working")
|
80 |
+
def check_working():
|
81 |
+
return {"message": "Message working Successfully", "status":"success"}
|
82 |
+
|
83 |
+
async def process_file(file):
|
84 |
+
try:
|
85 |
+
summerized_id = str(uuid.uuid4())
|
86 |
+
file_contents = await file.read()
|
87 |
+
directory = "./delete"
|
88 |
+
os.makedirs(directory, exist_ok=True)
|
89 |
+
path = os.path.join(directory, file.filename)
|
90 |
+
path = f"./delete/{file.filename}"
|
91 |
+
with open(path, "wb") as f:
|
92 |
+
f.write(file_contents)
|
93 |
+
text = extract_text_from_pdf(path)
|
94 |
+
summary = Summerized_Text(text)
|
95 |
+
report_id = AddReport(services, summerized_id, path)
|
96 |
+
AddSummary(
|
97 |
+
services,
|
98 |
+
{
|
99 |
+
"id": summerized_id,
|
100 |
+
"project": "",
|
101 |
+
"summary": summary,
|
102 |
+
"drive": f"https://drive.google.com/file/d/{report_id}/view?usp=sharing",
|
103 |
+
"year": "2023",
|
104 |
+
"category": ["wanna check"],
|
105 |
+
},
|
106 |
+
)
|
107 |
+
os.remove(path)
|
108 |
+
return {
|
109 |
+
"message": f"Successfully added Report and Summary for {file.filename}",
|
110 |
+
"data": {
|
111 |
+
"id": summerized_id,
|
112 |
+
"compare": "",
|
113 |
+
"title": "",
|
114 |
+
"summary": summary,
|
115 |
+
"drive": f"https://drive.google.com/file/d/{report_id}/view?usp=sharing",
|
116 |
+
"year": "2023",
|
117 |
+
"category": ["wanna check"],
|
118 |
+
},
|
119 |
+
"success": True,
|
120 |
+
}
|
121 |
+
except Exception as e:
|
122 |
+
return {
|
123 |
+
"message": f"Error processing {file.filename}: {str(e)}",
|
124 |
+
"status": "failed",
|
125 |
+
}
|
126 |
+
|
127 |
+
@app.post("/api/upload")
|
128 |
+
async def upload_files(files: List[UploadFile] = File(...)):
|
129 |
+
try:
|
130 |
+
loop = asyncio.get_event_loop()
|
131 |
+
with ThreadPoolExecutor() as pool:
|
132 |
+
responses = await asyncio.gather(*[loop.run_in_executor(pool, process_file, file) for file in files])
|
133 |
+
print("Uploaded Successfully")
|
134 |
+
return {"data": [await response for response in responses], "status": "success"}
|
135 |
+
except Exception as e:
|
136 |
+
print(f"An error occurred: {str(e)}")
|
137 |
+
return {"message": "Failed to Upload files", "data": False, "status": "failed"}
|
138 |
+
|
139 |
+
|
140 |
+
@app.post("/api/delete")
|
141 |
+
async def delete_files(request: IDRequest):
|
142 |
+
try:
|
143 |
+
for report_name in request.ids:
|
144 |
+
DeleteSummary(services, report_name)
|
145 |
+
DeleteReport(services, report_name)
|
146 |
+
try:
|
147 |
+
directory = "."
|
148 |
+
for filename in os.listdir(directory):
|
149 |
+
if filename.startswith("data-"):
|
150 |
+
file_path = "./" + filename
|
151 |
+
os.remove(file_path)
|
152 |
+
except Exception as e:
|
153 |
+
print(f"An error occurred: {str(e)}")
|
154 |
+
return {"message": "Successfully Deleted Summary and Report", "success": True}
|
155 |
+
except Exception as e:
|
156 |
+
print(f"An error occurred: {str(e)}")
|
157 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
@app.post("/api/logout")
|
162 |
+
async def delete_account(request: AccountInfo):
|
163 |
+
try:
|
164 |
+
Delete_Token_File(request.oldName)
|
165 |
+
global services
|
166 |
+
services, name = Create_Token_Drive(request.newName)
|
167 |
+
return {"message": "Successfully Deleted Account", "data": name, "success": True}
|
168 |
+
except Exception as e:
|
169 |
+
print(f"An error occurred: {str(e)}")
|
170 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
171 |
+
|
172 |
+
|
173 |
+
@app.get("/api/getReports")
|
174 |
+
async def get_reports():
|
175 |
+
try:
|
176 |
+
data = Get_All_Reports(services)
|
177 |
+
if len(data) == 0:
|
178 |
+
return {"data": None, "status": "failed" }
|
179 |
+
try:
|
180 |
+
directory = "."
|
181 |
+
for filename in os.listdir(directory):
|
182 |
+
if filename.startswith("data-"):
|
183 |
+
file_path = "./" + filename
|
184 |
+
os.remove(file_path)
|
185 |
+
except Exception as e:
|
186 |
+
print(f"An error occurred: {str(e)}")
|
187 |
+
return {"data": data, "success": True}
|
188 |
+
except Exception as e:
|
189 |
+
print(f"An error occurred: {str(e)}")
|
190 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
@app.post("/api/compare")
|
195 |
+
async def compare(file: UploadFile = File(...)):
|
196 |
+
try:
|
197 |
+
CheckFolders(services)
|
198 |
+
summerized_id = str(uuid.uuid4())
|
199 |
+
file_contents = await file.read()
|
200 |
+
path = f"./delete/{file.filename}"
|
201 |
+
with open(path, "wb") as f:
|
202 |
+
f.write(file_contents)
|
203 |
+
text = extract_text_from_pdf(path)
|
204 |
+
summary = Summerized_Text(text)
|
205 |
+
path = f"./delete/{file.filename}"
|
206 |
+
with open(path, "wb") as f:
|
207 |
+
f.write(file_contents)
|
208 |
+
data = compareText(services, summary)
|
209 |
+
try:
|
210 |
+
directory = "."
|
211 |
+
for filename in os.listdir(directory):
|
212 |
+
if filename.startswith("data-"):
|
213 |
+
file_path = "./" + filename
|
214 |
+
os.remove(file_path)
|
215 |
+
if filename.startswith("delete"):
|
216 |
+
file_path = path
|
217 |
+
os.remove(file_path)
|
218 |
+
except Exception as e:
|
219 |
+
print(f"An error occurred: {str(e)}")
|
220 |
+
return {"summary": summary, "data": data, "success": True}
|
221 |
+
except Exception as e:
|
222 |
+
print(f"An error occurred: {str(e)}")
|
223 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
224 |
+
|
225 |
+
@app.get("/api/Check-Expired")
|
226 |
+
async def checkExpired():
|
227 |
+
try:
|
228 |
+
Expire_Token_File()
|
229 |
+
return {"message": "Successfully Expired", "status": "success"}
|
230 |
+
except Exception as e:
|
231 |
+
print(f"An error occurred: {str(e)}")
|
232 |
+
return {"message": "Failed to Check Expired Tokens", "data": False, "status":"failed"}
|
233 |
+
|
234 |
+
|
235 |
+
@app.post("/api/New-Drive")
|
236 |
+
async def NewDrive():
|
237 |
+
try:
|
238 |
+
Delete_Drive_Token()
|
239 |
+
Create_Drive_Token()
|
240 |
+
return {"message": "Successfully Created a new drive", "status": "success"}
|
241 |
+
except Exception as e:
|
242 |
+
print(f"An error occurred: {str(e)}")
|
243 |
+
return {"message": "Failed to login", "data": False, "status":"failed"}
|
244 |
+
|
245 |
+
|
246 |
+
if __name__ == "__main__":
|
247 |
+
global services
|
248 |
+
Create_Drive_Token()
|
249 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
250 |
+
|
client_secret.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"\u0069\u006e\u0073\u0074\u0061\u006c\u006c\u0065\u0064": {
|
3 |
+
"\u0063\u006c\u0069\u0065\u006e\u0074\u005f\u0069\u0064": "\u0034\u0039\u0032\u0037\u0037\u0030\u0030\u0035\u0039\u0031\u0039\u0037\u002d\u0034\u006e\u0071\u006c\u006e\u0037\u0034\u0035\u0034\u0076\u0034\u0035\u0038\u0073\u0033\u006f\u006f\u0039\u0074\u006e\u006a\u0061\u006d\u0067\u0064\u0039\u006f\u0067\u0068\u0068\u0035\u006f\u002e\u0061\u0070\u0070\u0073\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0075\u0073\u0065\u0072\u0063\u006f\u006e\u0074\u0065\u006e\u0074\u002e\u0063\u006f\u006d",
|
4 |
+
"\u0070\u0072\u006f\u006a\u0065\u0063\u0074\u005f\u0069\u0064": "\u0072\u0065\u0070\u0065\u0074\u0061\u0074\u0069\u0076\u0065",
|
5 |
+
"\u0061\u0075\u0074\u0068\u005f\u0075\u0072\u0069": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0061\u0063\u0063\u006f\u0075\u006e\u0074\u0073\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u002e\u0063\u006f\u006d\u002f\u006f\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002f\u0061\u0075\u0074\u0068",
|
6 |
+
"\u0074\u006f\u006b\u0065\u006e\u005f\u0075\u0072\u0069": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0061\u0070\u0069\u0073\u002e\u0063\u006f\u006d\u002f\u0074\u006f\u006b\u0065\u006e",
|
7 |
+
"\u0061\u0075\u0074\u0068\u005f\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0072\u005f\u0078\u0035\u0030\u0039\u005f\u0063\u0065\u0072\u0074\u005f\u0075\u0072\u006c": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0061\u0070\u0069\u0073\u002e\u0063\u006f\u006d\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002f\u0076\u0031\u002f\u0063\u0065\u0072\u0074\u0073",
|
8 |
+
"\u0063\u006c\u0069\u0065\u006e\u0074\u005f\u0073\u0065\u0063\u0072\u0065\u0074": "\u0047\u004f\u0043\u0053\u0050\u0058\u002d\u0043\u0061\u0059\u0070\u004f\u0074\u0046\u0047\u0056\u006c\u0066\u007a\u0042\u006f\u002d\u0063\u0036\u0079\u0062\u0039\u006c\u0059\u004e\u006a\u0039\u0067\u0074\u0064",
|
9 |
+
"\u0072\u0065\u0064\u0069\u0072\u0065\u0063\u0074\u005f\u0075\u0072\u0069\u0073": ["\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u006c\u006f\u0063\u0061\u006c\u0068\u006f\u0073\u0074"]
|
10 |
+
}
|
11 |
+
}
|
comparator/__pycache__/compareSummerized.cpython-311.pyc
ADDED
Binary file (2.88 kB). View file
|
|
comparator/__pycache__/report.cpython-311.pyc
ADDED
Binary file (4.73 kB). View file
|
|
comparator/__pycache__/summerized.cpython-311.pyc
ADDED
Binary file (8.79 kB). View file
|
|
comparator/compareSummerized.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from io import BytesIO
|
5 |
+
import time
|
6 |
+
import io
|
7 |
+
from comparator.report import checkRespectiveFolder
|
8 |
+
from comparator.compare_model.compare import compare
|
9 |
+
def get_compare_value(report):
|
10 |
+
return report["compare"]
|
11 |
+
|
12 |
+
def compareText(service, summary):
|
13 |
+
try:
|
14 |
+
fake_folder_id = checkRespectiveFolder(service)
|
15 |
+
result_folder_id = checkRespectiveFolder(
|
16 |
+
service, path=fake_folder_id, Folder_Name="Result"
|
17 |
+
)
|
18 |
+
response = (
|
19 |
+
service.files()
|
20 |
+
.list(
|
21 |
+
q="name contains 'data-'",
|
22 |
+
spaces="drive",
|
23 |
+
fields="files(id, name)",
|
24 |
+
)
|
25 |
+
.execute()
|
26 |
+
)
|
27 |
+
list_all_files = response.get("files", [])
|
28 |
+
all_reports = []
|
29 |
+
|
30 |
+
for list_files in list_all_files:
|
31 |
+
if list_files["name"].startswith("data-"):
|
32 |
+
latest_file_id = str(list_files["id"])
|
33 |
+
|
34 |
+
existing_data = (
|
35 |
+
service.files().get_media(fileId=latest_file_id).execute()
|
36 |
+
)
|
37 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
38 |
+
file_name = list_files["name"]
|
39 |
+
|
40 |
+
for json_data in existing_details:
|
41 |
+
print(summary)
|
42 |
+
value = compare(summary, json_data["summary"])
|
43 |
+
all_reports.append(
|
44 |
+
{
|
45 |
+
"id": json_data["id"],
|
46 |
+
"year": json_data["year"],
|
47 |
+
"drive": json_data["drive"],
|
48 |
+
"summary": json_data["summary"],
|
49 |
+
"category": json_data["category"],
|
50 |
+
"title": "",
|
51 |
+
"compare": value,
|
52 |
+
}
|
53 |
+
)
|
54 |
+
sorted_reports = sorted(all_reports, key=get_compare_value, reverse=True)
|
55 |
+
return sorted_reports
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
print(f"An error occurred: {str(e)}")
|
comparator/compare_model/__pycache__/compare.cpython-311.pyc
ADDED
Binary file (2.53 kB). View file
|
|
comparator/compare_model/__pycache__/compare2.cpython-311.pyc
ADDED
Binary file (864 Bytes). View file
|
|
comparator/compare_model/compare.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModel
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
|
6 |
+
def mean_pooling(model_output, attention_mask):
|
7 |
+
token_embeddings = model_output[
|
8 |
+
0
|
9 |
+
]
|
10 |
+
input_mask_expanded = (
|
11 |
+
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
12 |
+
)
|
13 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
|
14 |
+
input_mask_expanded.sum(1), min=1e-9
|
15 |
+
)
|
16 |
+
|
17 |
+
def cosine_similarity(u, v):
|
18 |
+
return F.cosine_similarity(u, v, dim=1)
|
19 |
+
|
20 |
+
|
21 |
+
def compare(text1, text2):
|
22 |
+
|
23 |
+
sentences = [text1, text2]
|
24 |
+
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained("dmlls/all-mpnet-base-v2-negation")
|
26 |
+
model = AutoModel.from_pretrained("dmlls/all-mpnet-base-v2-negation")
|
27 |
+
|
28 |
+
encoded_input = tokenizer(
|
29 |
+
sentences, padding=True, truncation=True, return_tensors="pt"
|
30 |
+
)
|
31 |
+
|
32 |
+
with torch.no_grad():
|
33 |
+
model_output = model(**encoded_input)
|
34 |
+
|
35 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input["attention_mask"])
|
36 |
+
|
37 |
+
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
38 |
+
|
39 |
+
similarity_score = cosine_similarity(
|
40 |
+
sentence_embeddings[0].unsqueeze(0), sentence_embeddings[1].unsqueeze(0)
|
41 |
+
)
|
42 |
+
return similarity_score.item()
|
comparator/compare_model/compare2.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
|
3 |
+
# sentences = [
|
4 |
+
# "I like rainy days because they make me feel relaxed.",
|
5 |
+
# "I don't like rainy days because they don't make me feel relaxed."
|
6 |
+
# ]
|
7 |
+
|
8 |
+
# model = SentenceTransformer('dmlls/all-mpnet-base-v2-negation')
|
9 |
+
# embeddings = model.encode(sentences)
|
10 |
+
# from sklearn.metrics.pairwise import cosine_similarity
|
11 |
+
|
12 |
+
# similarity_score = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]
|
13 |
+
# similarity = (similarity_score + 1) / 2
|
14 |
+
|
15 |
+
# print("Similarity:", similarity)
|
16 |
+
|
17 |
+
def checkSimilarity(text1, text2):
|
18 |
+
model = SentenceTransformer('dmlls/all-mpnet-base-v2-negation')
|
19 |
+
embeddings = model.encode([text1, text2])
|
20 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
21 |
+
|
22 |
+
similarity_score = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]
|
23 |
+
similarity = (similarity_score + 1) / 2
|
24 |
+
|
25 |
+
return similarity
|
comparator/extract/__pycache__/extract.cpython-311.pyc
ADDED
Binary file (1.4 kB). View file
|
|
comparator/extract/extract.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PyPDF2
|
2 |
+
from PIL import Image
|
3 |
+
import pytesseract
|
4 |
+
import io
|
5 |
+
import sys
|
6 |
+
|
7 |
+
|
8 |
+
# def extract_text(file_path, output_file_path):
|
9 |
+
# text = ""
|
10 |
+
# try:
|
11 |
+
# if file_path.lower().endswith(".pdf"):
|
12 |
+
# text = extract_text_from_pdf(file_path)
|
13 |
+
# else:
|
14 |
+
# print("Unsupported file format")
|
15 |
+
|
16 |
+
# with open(output_file_path, "w") as output_file:
|
17 |
+
# print("Run output")
|
18 |
+
# for line in text.splitlines():
|
19 |
+
# print(line)
|
20 |
+
# output_file.write(text)
|
21 |
+
# print(f"Extracted text saved to {output_file_path}")
|
22 |
+
|
23 |
+
# except Exception as e:
|
24 |
+
# print("An error occurred:", e)
|
25 |
+
|
26 |
+
|
27 |
+
# def extract_text_from_image(file_path):
|
28 |
+
# image_path = file_path
|
29 |
+
# img = Image.open(image_path)
|
30 |
+
# text = pytesseract.image_to_string(img)
|
31 |
+
# print(text[:-1])
|
32 |
+
|
33 |
+
|
34 |
+
import PyPDF2
|
35 |
+
|
36 |
+
def extract_text_from_pdf(pdf_file_path):
|
37 |
+
extracted_text = ""
|
38 |
+
try:
|
39 |
+
with open(pdf_file_path, "rb") as file:
|
40 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
41 |
+
num_pages = len(pdf_reader.pages)
|
42 |
+
for i in range(num_pages):
|
43 |
+
page = pdf_reader.pages[i]
|
44 |
+
page_text = page.extract_text()
|
45 |
+
if "ABSTRACT" in page_text:
|
46 |
+
extracted_text += page_text + "\n"
|
47 |
+
break
|
48 |
+
return extracted_text
|
49 |
+
except Exception as e:
|
50 |
+
print("An error occurred:", e)
|
51 |
+
return None
|
52 |
+
|
53 |
+
# if __name__ == "__main__":
|
54 |
+
# import PyPDF2
|
55 |
+
|
56 |
+
# file_path = "./report.pdf"
|
57 |
+
# output_file_path = "./extracted_text.txt"
|
58 |
+
# extract_text_from_pdf(file_path, output_file_path)
|
comparator/report.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from io import BytesIO
|
5 |
+
import time
|
6 |
+
import io
|
7 |
+
|
8 |
+
file_metadata = {
|
9 |
+
"name": "Fake",
|
10 |
+
"mimeType": "application/vnd.google-apps.folder",
|
11 |
+
}
|
12 |
+
file_result = {
|
13 |
+
"name": "Result",
|
14 |
+
"mimeType": "application/vnd.google-apps.folder",
|
15 |
+
}
|
16 |
+
file_report = {
|
17 |
+
"name": "Report",
|
18 |
+
"mimeType": "application/vnd.google-apps.folder",
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
def checkFake(service, path="root", Folder_Name="Fake"):
|
23 |
+
resource = service.files()
|
24 |
+
result = resource.list(
|
25 |
+
q=f"mimeType = 'application/vnd.google-apps.folder' and '{path}' in parents",
|
26 |
+
fields="nextPageToken, files(id, name)",
|
27 |
+
).execute()
|
28 |
+
list_folders = result.get("files")
|
29 |
+
fake_folder_id = None
|
30 |
+
result_folder_id = None
|
31 |
+
report_folder_id = None
|
32 |
+
|
33 |
+
for folder in list_folders:
|
34 |
+
if folder["name"] == Folder_Name:
|
35 |
+
fake_folder_id = folder["id"]
|
36 |
+
break
|
37 |
+
|
38 |
+
if not fake_folder_id:
|
39 |
+
fake_folder = service.files().create(body=file_metadata, fields="id").execute()
|
40 |
+
fake_folder_id = fake_folder["id"]
|
41 |
+
result_folder = (
|
42 |
+
service.files()
|
43 |
+
.create(
|
44 |
+
body={
|
45 |
+
"name": "Result",
|
46 |
+
"mimeType": "application/vnd.google-apps.folder",
|
47 |
+
"parents": [fake_folder_id],
|
48 |
+
"type": "anyone",
|
49 |
+
"role": "reader",
|
50 |
+
},
|
51 |
+
fields="id",
|
52 |
+
)
|
53 |
+
.execute()
|
54 |
+
)
|
55 |
+
result_folder_id = result_folder["id"]
|
56 |
+
|
57 |
+
report_folder = (
|
58 |
+
service.files()
|
59 |
+
.create(
|
60 |
+
body={
|
61 |
+
"name": "Report",
|
62 |
+
"mimeType": "application/vnd.google-apps.folder",
|
63 |
+
"parents": [fake_folder_id],
|
64 |
+
},
|
65 |
+
fields="id",
|
66 |
+
)
|
67 |
+
.execute()
|
68 |
+
)
|
69 |
+
report_folder_id = report_folder["id"]
|
70 |
+
|
71 |
+
return fake_folder_id, result_folder_id, report_folder_id
|
72 |
+
|
73 |
+
|
74 |
+
def checkRespectiveFolder(service, path="root", Folder_Name="Fake"):
|
75 |
+
resource = service.files()
|
76 |
+
result = resource.list(
|
77 |
+
q=f"mimeType = 'application/vnd.google-apps.folder' and '{path}' in parents",
|
78 |
+
fields="nextPageToken, files(id, name)",
|
79 |
+
).execute()
|
80 |
+
list_folders = result.get("files")
|
81 |
+
fake_folder_id = None
|
82 |
+
for folder in list_folders:
|
83 |
+
if folder["name"] == Folder_Name:
|
84 |
+
fake_folder_id = folder["id"]
|
85 |
+
break
|
86 |
+
return fake_folder_id
|
87 |
+
|
88 |
+
|
89 |
+
def CheckFolders(service):
|
90 |
+
fake_folder_id = checkFake(service)
|
91 |
+
return "Folders created or already existed."
|
92 |
+
|
93 |
+
def AddReport(service, fileName, file):
|
94 |
+
fake_folder_id = checkRespectiveFolder(service)
|
95 |
+
report_folder_id = checkRespectiveFolder(
|
96 |
+
service, path=fake_folder_id, Folder_Name="Report"
|
97 |
+
)
|
98 |
+
|
99 |
+
file_metadata = {"name": fileName, "parents": [report_folder_id]}
|
100 |
+
media = MediaFileUpload(file, mimetype="application/pdf")
|
101 |
+
newFile = (
|
102 |
+
service.files()
|
103 |
+
.create(body=file_metadata, media_body=media, fields="id")
|
104 |
+
.execute()
|
105 |
+
)
|
106 |
+
return newFile.get("id")
|
107 |
+
|
108 |
+
def DeleteReport(service, fileName):
|
109 |
+
fake_folder_id = checkRespectiveFolder(service)
|
110 |
+
report_folder_id = checkRespectiveFolder(
|
111 |
+
service, path=fake_folder_id, Folder_Name="Report"
|
112 |
+
)
|
113 |
+
response = (
|
114 |
+
service.files()
|
115 |
+
.list(
|
116 |
+
q="mimeType='application/pdf' and '" + report_folder_id + "' in parents",
|
117 |
+
spaces="drive",
|
118 |
+
fields="files(id, name)",
|
119 |
+
pageToken=None,
|
120 |
+
)
|
121 |
+
.execute()
|
122 |
+
)
|
123 |
+
for i in response["files"]:
|
124 |
+
if i["name"] == fileName:
|
125 |
+
file_id = i["id"]
|
126 |
+
service.files().delete(fileId=file_id).execute()
|
127 |
+
break
|
128 |
+
|
comparator/summerized.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from io import BytesIO
|
5 |
+
import time
|
6 |
+
import io
|
7 |
+
from comparator.report import checkRespectiveFolder
|
8 |
+
|
9 |
+
|
10 |
+
def DeleteSummary(service, fileID):
|
11 |
+
try:
|
12 |
+
fake_folder_id = checkRespectiveFolder(service)
|
13 |
+
result_folder_id = checkRespectiveFolder(
|
14 |
+
service, path=fake_folder_id, Folder_Name="Result"
|
15 |
+
)
|
16 |
+
response = (
|
17 |
+
service.files()
|
18 |
+
.list(
|
19 |
+
q="name contains 'data-'",
|
20 |
+
spaces="drive",
|
21 |
+
fields="files(id, name)",
|
22 |
+
)
|
23 |
+
.execute()
|
24 |
+
)
|
25 |
+
list_all_files = response.get("files", [])
|
26 |
+
|
27 |
+
for list_files in list_all_files:
|
28 |
+
if list_files["name"].startswith("data-"):
|
29 |
+
latest_file_id = list_files["id"]
|
30 |
+
|
31 |
+
existing_data = (
|
32 |
+
service.files().get_media(fileId=latest_file_id).execute()
|
33 |
+
)
|
34 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
35 |
+
|
36 |
+
updated_data = [
|
37 |
+
data for data in existing_details if data["id"] != fileID
|
38 |
+
]
|
39 |
+
|
40 |
+
with open(list_files["name"], "w", encoding="utf-8") as json_file:
|
41 |
+
json.dump(updated_data, json_file, ensure_ascii=False)
|
42 |
+
|
43 |
+
media = MediaFileUpload(list_files["name"], mimetype="application/json")
|
44 |
+
|
45 |
+
service.files().update(
|
46 |
+
fileId=latest_file_id, media_body=media
|
47 |
+
).execute()
|
48 |
+
return
|
49 |
+
return
|
50 |
+
except Exception as e:
|
51 |
+
print(f"An error occurred: {str(e)}")
|
52 |
+
|
53 |
+
|
54 |
+
def AddSummary(service, details):
|
55 |
+
try:
|
56 |
+
fake_folder_id = checkRespectiveFolder(service)
|
57 |
+
result_folder_id = checkRespectiveFolder(
|
58 |
+
service, path=fake_folder_id, Folder_Name="Result"
|
59 |
+
)
|
60 |
+
response = (
|
61 |
+
service.files()
|
62 |
+
.list(
|
63 |
+
q="name contains 'data-'",
|
64 |
+
spaces="drive",
|
65 |
+
fields="files(id, name)",
|
66 |
+
)
|
67 |
+
.execute()
|
68 |
+
)
|
69 |
+
list_all_files = response.get("files", [])
|
70 |
+
|
71 |
+
is_selected = False
|
72 |
+
file_name = ""
|
73 |
+
|
74 |
+
for list_files in list_all_files:
|
75 |
+
if list_files["name"].startswith("data-"):
|
76 |
+
latest_file_id = str(list_files["id"])
|
77 |
+
|
78 |
+
existing_data = service.files().get_media(fileId=latest_file_id).execute()
|
79 |
+
|
80 |
+
if existing_data:
|
81 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
82 |
+
|
83 |
+
with open(list_files["name"], "w", encoding="utf-8") as json_file:
|
84 |
+
json.dump(existing_details, json_file, ensure_ascii=False)
|
85 |
+
|
86 |
+
if len(existing_details) < 100:
|
87 |
+
is_selected = True
|
88 |
+
file_name = list_files["name"]
|
89 |
+
|
90 |
+
with open(file_name, "r", encoding="utf-8") as json_file:
|
91 |
+
existing_data = json.load(json_file)
|
92 |
+
|
93 |
+
existing_data.append(details)
|
94 |
+
|
95 |
+
with open(file_name, "w", encoding="utf-8") as json_file:
|
96 |
+
json.dump(existing_data, json_file)
|
97 |
+
|
98 |
+
json_file.close()
|
99 |
+
|
100 |
+
file_metadata = {"name": file_name, "parents": [result_folder_id]}
|
101 |
+
media = MediaFileUpload(file_name, mimetype="application/json")
|
102 |
+
|
103 |
+
service.files().update(fileId=latest_file_id, media_body=media).execute()
|
104 |
+
|
105 |
+
return
|
106 |
+
else:
|
107 |
+
break
|
108 |
+
|
109 |
+
if not is_selected:
|
110 |
+
existing_details = [details]
|
111 |
+
file_name = f"data-{len(list_all_files)}.json"
|
112 |
+
json_data = json.dumps(existing_details)
|
113 |
+
with open(file_name, "w") as f:
|
114 |
+
f.write(json_data)
|
115 |
+
file_metadata = {"name": file_name, "parents": [result_folder_id]}
|
116 |
+
media = MediaFileUpload(
|
117 |
+
file_name, mimetype="application/json", resumable=True
|
118 |
+
)
|
119 |
+
service.files().create(
|
120 |
+
body=file_metadata, media_body=media, fields="id"
|
121 |
+
).execute()
|
122 |
+
return
|
123 |
+
|
124 |
+
except Exception as e:
|
125 |
+
print(f"An error occurred here: {str(e)}")
|
126 |
+
|
127 |
+
def Get_All_Reports(service):
|
128 |
+
try:
|
129 |
+
fake_folder_id = checkRespectiveFolder(service)
|
130 |
+
result_folder_id = checkRespectiveFolder(
|
131 |
+
service, path=fake_folder_id, Folder_Name="Result"
|
132 |
+
)
|
133 |
+
response = (
|
134 |
+
service.files()
|
135 |
+
.list(
|
136 |
+
q="name contains 'data-'",
|
137 |
+
spaces="drive",
|
138 |
+
fields="files(id, name)",
|
139 |
+
)
|
140 |
+
.execute()
|
141 |
+
)
|
142 |
+
list_all_files = response["files"]
|
143 |
+
all_reports = []
|
144 |
+
|
145 |
+
for list_files in list_all_files:
|
146 |
+
if list_files["name"].startswith("data-"):
|
147 |
+
latest_file_id = str(list_files["id"])
|
148 |
+
|
149 |
+
existing_data = (
|
150 |
+
service.files().get_media(fileId=latest_file_id).execute()
|
151 |
+
)
|
152 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
153 |
+
file_name = list_files["name"]
|
154 |
+
|
155 |
+
with open(file_name, "w", encoding="utf-8") as json_file:
|
156 |
+
json.dump(existing_details, json_file, ensure_ascii=False)
|
157 |
+
|
158 |
+
with open(file_name, "r", encoding="utf-8") as json_file:
|
159 |
+
existing_data = json.load(json_file)
|
160 |
+
|
161 |
+
all_reports.extend(existing_details)
|
162 |
+
return all_reports
|
163 |
+
|
164 |
+
except Exception as e:
|
165 |
+
print(f"An error occurred: {str(e)}")
|
comparator/text_summerizer/__pycache__/summerize2.cpython-311.pyc
ADDED
Binary file (2.27 kB). View file
|
|
comparator/text_summerizer/summerize2.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
4 |
+
|
5 |
+
ARTICLE = """Parkinson’s disease (PD) is one of the long-term regressive disorders of the central nervous
|
6 |
+
system that mainly affects the nervous system in humans due to which there is the recurrent
|
7 |
+
occurrence of falls which might even lead to death or put them in a serious situation. For
|
8 |
+
solving this issue, we have proposed an idea that detects patient fall and reports to the
|
9 |
+
caretaker or family member utilizing message/call/Alarm thereby avoiding deaths. Our
|
10 |
+
work focuses on developing and apply the wearable fall -detection system for Parkinson’s
|
11 |
+
disease long -suffers formed on a Wi -Fi module. Patient falls were precisely traced on the
|
12 |
+
outcome of an accelerometer, oximeter, and pressure sensors which were sent to the cloud
|
13 |
+
via NodeMCU -ESP32. Furthermore, Node MCU ESP32 is planned for attaining smooth
|
14 |
+
communicat ion between the fall detection device and the system for obtaining and
|
15 |
+
processing the data anytime and anywhere until we have a Wi -Fi connection. In
|
16 |
+
consequence, the cloud will perform the calculations based on SVM which is the Support
|
17 |
+
Vector Machine. In t urn, it sends the fall detection outcomes to the MIT application. This
|
18 |
+
WFDS turns out to be the best among the existing models in terms of fall detection
|
19 |
+
sensitivity, specificity, and accuracy. The fall of direction in PD can be performed
|
20 |
+
accurately based on the fall detection algorithm at the receiver end. The preliminary
|
21 |
+
outcomes show that the wearable fall -detection system attains eighty -. eight percent
|
22 |
+
accuracy in identifying the patient’s fall."""
|
23 |
+
|
24 |
+
def Summerized_Text(text):
|
25 |
+
text = text.strip()
|
26 |
+
a = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
27 |
+
return a[0]['summary_text']
|
repetitive-project-backend
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Subproject commit 02e651a414b44be04759eb6cb0336417e3dcb782
|
requirements.txt
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.70.0
|
2 |
+
uvicorn==0.15.0
|
3 |
+
google-auth==2.29.0
|
4 |
+
google-auth-oauthlib==1.2.0
|
5 |
+
google-api-python-client==2.125.0
|
6 |
+
transformers==4.39.3
|
7 |
+
tensorflow==2.13.1
|
8 |
+
torch==2.2.2
|
9 |
+
torchvision==0.17.2
|
10 |
+
torchaudio==2.2.2
|
11 |
+
PyPDF2==3.0.1
|
12 |
+
pytesseract==0.3.10
|
13 |
+
sentence-transformers==2.6.1
|
14 |
+
python-multipart
|
15 |
+
asyncio
|
token_json.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"01d63f7d-ec01-4ab9-a4c2-865039408b18": {"token_name": "01d63f7d-ec01-4ab9-a4c2-865039408b18", "token_file_id": "12C8dbqBSg-tgMFLzPHiUBtmc0ZNmWI9N", "expires_at": "2024-04-21T06:37:55Z"}, "91030212-6d4b-4a3a-b07c-7a9af8eb4b3f": {"token_name": "91030212-6d4b-4a3a-b07c-7a9af8eb4b3f", "token_file_id": "1dWn--JJA4CNjANuBguBUucwW5bFVc4VR", "expires_at": "2024-04-21T06:38:19Z"}, "a033f0d9-c4d4-41a4-8345-cd1f51666e11": {"token_name": "a033f0d9-c4d4-41a4-8345-cd1f51666e11", "token_file_id": "1ybgR3Pyo5IGkeY7fylqi6bwZiJBLCWO-", "expires_at": "2024-04-21T07:01:15Z"}, "c578af4e-a61d-4ebc-925a-9eca1656fecf": {"token_name": "c578af4e-a61d-4ebc-925a-9eca1656fecf", "token_file_id": "1f3Nf_jyhFGF2hChWr3aATvwBh49Nfgxq", "expires_at": "2024-04-21T07:01:43Z"}, "e6e7141c-850f-41de-9cf7-8db123ea48b5": {"token_name": "e6e7141c-850f-41de-9cf7-8db123ea48b5", "token_file_id": "1FVKdEeN6onZA7ppJys5VL7ozXCw_CaGu", "expires_at": "2024-04-21T16:21:48Z"}, "a394423a-b49a-45f1-8d96-eed459d2a0ce": {"token_name": "a394423a-b49a-45f1-8d96-eed459d2a0ce", "token_file_id": "1IgXKi83S540F-ihq_5OWKLSmdp1xxwMu", "expires_at": "2024-04-21T16:34:09Z"}, "e8ebc7f1-12eb-461e-83c3-69e55d4fea8a": {"token_name": "e8ebc7f1-12eb-461e-83c3-69e55d4fea8a", "token_file_id": "1bGb8kdKr4EKjlLEMQcs_2F4HaNNtJkw6", "expires_at": "2024-04-21T16:34:38Z"}, "a375311a-0779-4199-9f9c-e8b7a665994c": {"token_name": "a375311a-0779-4199-9f9c-e8b7a665994c", "token_file_id": "1jBmcxhAOTrqvarFQ8zeNDmGI80noSDhv", "expires_at": "2024-04-21T17:48:42Z"}, "95b533d6-1d5c-4852-945f-578130d939b6": {"token_name": "95b533d6-1d5c-4852-945f-578130d939b6", "token_file_id": "19q-zGO5uBAtAknF-pvFTyqp03nT1eV6p", "expires_at": "2024-04-22T19:02:29Z"}, "96779b9b-4dbb-4136-a263-b5bf3f06f5cd": {"token_name": "96779b9b-4dbb-4136-a263-b5bf3f06f5cd", "token_file_id": "1t_HU7LuO2XtrQk_GSCgC0ZdyHwLCk-Lm", "expires_at": "2024-04-23T05:54:21Z"}, "7debcd64-c89b-44a7-ae74-da3af4201d33": {"token_name": "7debcd64-c89b-44a7-ae74-da3af4201d33", "token_file_id": "18EZlC41-hz1eNy_EtImVXz2mOYPrE9fH", "expires_at": "2024-04-23T22:19:22Z"}}
|
token_operation/__pycache__/driveToken.cpython-311.pyc
ADDED
Binary file (3.51 kB). View file
|
|
token_operation/__pycache__/tokenOperation.cpython-311.pyc
ADDED
Binary file (17.2 kB). View file
|
|
token_operation/client_secret.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"\u0069\u006e\u0073\u0074\u0061\u006c\u006c\u0065\u0064": {
|
3 |
+
"\u0063\u006c\u0069\u0065\u006e\u0074\u005f\u0069\u0064": "\u0034\u0039\u0032\u0037\u0037\u0030\u0030\u0035\u0039\u0031\u0039\u0037\u002d\u0034\u006e\u0071\u006c\u006e\u0037\u0034\u0035\u0034\u0076\u0034\u0035\u0038\u0073\u0033\u006f\u006f\u0039\u0074\u006e\u006a\u0061\u006d\u0067\u0064\u0039\u006f\u0067\u0068\u0068\u0035\u006f\u002e\u0061\u0070\u0070\u0073\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0075\u0073\u0065\u0072\u0063\u006f\u006e\u0074\u0065\u006e\u0074\u002e\u0063\u006f\u006d",
|
4 |
+
"\u0070\u0072\u006f\u006a\u0065\u0063\u0074\u005f\u0069\u0064": "\u0072\u0065\u0070\u0065\u0074\u0061\u0074\u0069\u0076\u0065",
|
5 |
+
"\u0061\u0075\u0074\u0068\u005f\u0075\u0072\u0069": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0061\u0063\u0063\u006f\u0075\u006e\u0074\u0073\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u002e\u0063\u006f\u006d\u002f\u006f\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002f\u0061\u0075\u0074\u0068",
|
6 |
+
"\u0074\u006f\u006b\u0065\u006e\u005f\u0075\u0072\u0069": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0061\u0070\u0069\u0073\u002e\u0063\u006f\u006d\u002f\u0074\u006f\u006b\u0065\u006e",
|
7 |
+
"\u0061\u0075\u0074\u0068\u005f\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0072\u005f\u0078\u0035\u0030\u0039\u005f\u0063\u0065\u0072\u0074\u005f\u0075\u0072\u006c": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0061\u0070\u0069\u0073\u002e\u0063\u006f\u006d\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002f\u0076\u0031\u002f\u0063\u0065\u0072\u0074\u0073",
|
8 |
+
"\u0063\u006c\u0069\u0065\u006e\u0074\u005f\u0073\u0065\u0063\u0072\u0065\u0074": "\u0047\u004f\u0043\u0053\u0050\u0058\u002d\u0043\u0061\u0059\u0070\u004f\u0074\u0046\u0047\u0056\u006c\u0066\u007a\u0042\u006f\u002d\u0063\u0036\u0079\u0062\u0039\u006c\u0059\u004e\u006a\u0039\u0067\u0074\u0064",
|
9 |
+
"\u0072\u0065\u0064\u0069\u0072\u0065\u0063\u0074\u005f\u0075\u0072\u0069\u0073": ["\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u006c\u006f\u0063\u0061\u006c\u0068\u006f\u0073\u0074"]
|
10 |
+
}
|
11 |
+
}
|
token_operation/driveToken.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import os
|
3 |
+
import datetime
|
4 |
+
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
|
5 |
+
from googleapiclient.discovery import build
|
6 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
|
7 |
+
from google.auth.transport.requests import Request
|
8 |
+
import json
|
9 |
+
|
10 |
+
from comparator.compareSummerized import compareText
|
11 |
+
|
12 |
+
global API_VERSION, CLIENT_FILE_NAME, API_DRIVE, SCOPES
|
13 |
+
CLIENT_FILE_NAME = "./token_operation/token_secret.json"
|
14 |
+
API_DRIVE = "drive"
|
15 |
+
API_VERSION = "v3"
|
16 |
+
SCOPES = ["https://www.googleapis.com/auth/drive"]
|
17 |
+
|
18 |
+
# file_metadata = {
|
19 |
+
# "name": "Token",
|
20 |
+
# "mimeType": "application/vnd.google-apps.folder",
|
21 |
+
# }
|
22 |
+
|
23 |
+
def Create_Drive_Token():
|
24 |
+
CLIENT_SECRET_FILE = CLIENT_FILE_NAME
|
25 |
+
API_SERVICE_NAME = API_DRIVE
|
26 |
+
# API_VERSION = API_VERSION
|
27 |
+
# SCOPES = SCOPES
|
28 |
+
|
29 |
+
cred = None
|
30 |
+
pickle_file = f'./Drive_API.pickle'
|
31 |
+
|
32 |
+
if os.path.exists(pickle_file):
|
33 |
+
with open(pickle_file, 'rb') as token:
|
34 |
+
cred = pickle.load(token)
|
35 |
+
|
36 |
+
if not cred or not cred.valid:
|
37 |
+
if cred and cred.expired and cred.refresh_token:
|
38 |
+
cred.refresh(Request())
|
39 |
+
else:
|
40 |
+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
|
41 |
+
cred = flow.run_local_server()
|
42 |
+
|
43 |
+
with open(pickle_file, 'wb') as token:
|
44 |
+
pickle.dump(cred, token)
|
45 |
+
try:
|
46 |
+
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
|
47 |
+
return service
|
48 |
+
except Exception as e:
|
49 |
+
print('Unable to connect.')
|
50 |
+
print(e)
|
51 |
+
|
52 |
+
def Delete_Drive_Token():
|
53 |
+
directory = os.getcwd()
|
54 |
+
files = os.listdir(directory)
|
55 |
+
for file in files:
|
56 |
+
if "Drive_API" in file:
|
57 |
+
try:
|
58 |
+
os.remove(os.path.join(directory, file))
|
59 |
+
print(f"File '{file}' deleted successfully.")
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Error deleting file '{file}': {e}")
|
62 |
+
print("No File named Drive_API was Found !!! ")
|
token_operation/tokenOperation.py
ADDED
@@ -0,0 +1,323 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import os
|
3 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
4 |
+
from googleapiclient.discovery import build
|
5 |
+
from google.auth.transport.requests import Request
|
6 |
+
import json
|
7 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload
|
8 |
+
from datetime import datetime, timedelta
|
9 |
+
import gdown
|
10 |
+
from token_operation.driveToken import Create_Drive_Token
|
11 |
+
|
12 |
+
global API_VERSION, CLIENT_FILE_NAME, API_DRIVE, SCOPES
|
13 |
+
CLIENT_FILE_NAME = "./client_secret.json"
|
14 |
+
API_DRIVE = "drive"
|
15 |
+
API_VERSION = "v3"
|
16 |
+
SCOPES = ["https://www.googleapis.com/auth/drive"]
|
17 |
+
|
18 |
+
file_metadata = {
|
19 |
+
"name": "Token_Folder",
|
20 |
+
"mimeType": "application/vnd.google-apps.folder",
|
21 |
+
}
|
22 |
+
|
23 |
+
# ///////////////////////// CHECK THE PATH OF TOKEN FOLDER /////////////////////////////////
|
24 |
+
def Check_Token_Folder(service, path="root", Folder_Name="Token"):
|
25 |
+
resource = service.files()
|
26 |
+
result = resource.list(
|
27 |
+
q=f"mimeType = 'application/vnd.google-apps.folder' and '{path}' in parents",
|
28 |
+
fields="nextPageToken, files(id, name)",
|
29 |
+
).execute()
|
30 |
+
list_folders = result.get("files")
|
31 |
+
token_folder_id = None
|
32 |
+
for folder in list_folders:
|
33 |
+
if folder["name"] == Folder_Name:
|
34 |
+
token_folder_id = folder["id"]
|
35 |
+
break
|
36 |
+
if not token_folder_id:
|
37 |
+
token_folder = service.files().create(body=file_metadata, fields="id").execute()
|
38 |
+
token_folder_id= token_folder["id"]
|
39 |
+
|
40 |
+
result_folder = (
|
41 |
+
service.files()
|
42 |
+
.create(
|
43 |
+
body={
|
44 |
+
"name": Folder_Name,
|
45 |
+
"mimeType": "application/vnd.google-apps.folder",
|
46 |
+
"parents": [token_folder_id],
|
47 |
+
"type": "anyone",
|
48 |
+
"role": "reader",
|
49 |
+
},
|
50 |
+
fields="id",
|
51 |
+
)
|
52 |
+
.execute()
|
53 |
+
)
|
54 |
+
token_folder_id = result_folder["id"]
|
55 |
+
|
56 |
+
return token_folder_id
|
57 |
+
|
58 |
+
|
59 |
+
def Check_Token_Main_Folder(path="root",Folder_Name="Token_Folder"):
|
60 |
+
service = Create_Drive_Token()
|
61 |
+
resource = service.files()
|
62 |
+
result = resource.list(
|
63 |
+
q=f"mimeType = 'application/vnd.google-apps.folder' and '{path}' in parents",
|
64 |
+
fields="nextPageToken, files(id, name)",
|
65 |
+
).execute()
|
66 |
+
|
67 |
+
list_folders = result.get("files")
|
68 |
+
token_main_folder_id = None
|
69 |
+
token_folder_id = None
|
70 |
+
|
71 |
+
for folder in list_folders:
|
72 |
+
if folder["name"] == Folder_Name:
|
73 |
+
token_main_folder_id = folder["id"]
|
74 |
+
token_folder_id = Check_Token_Folder(service, path=token_main_folder_id, Folder_Name="Token")
|
75 |
+
break
|
76 |
+
|
77 |
+
if not token_main_folder_id:
|
78 |
+
token_main_folder = service.files().create(body=file_metadata, fields="id").execute()
|
79 |
+
token_main_folder_id= token_main_folder["id"]
|
80 |
+
|
81 |
+
result_folder = (
|
82 |
+
service.files()
|
83 |
+
.create(
|
84 |
+
body={
|
85 |
+
"name": "Token",
|
86 |
+
"mimeType": "application/vnd.google-apps.folder",
|
87 |
+
"parents": [token_main_folder_id],
|
88 |
+
"type": "anyone",
|
89 |
+
"role": "reader",
|
90 |
+
},
|
91 |
+
fields="id",
|
92 |
+
)
|
93 |
+
.execute()
|
94 |
+
)
|
95 |
+
token_folder_id = result_folder["id"]
|
96 |
+
permission = {
|
97 |
+
'type': 'anyone',
|
98 |
+
'role': 'reader'
|
99 |
+
}
|
100 |
+
service.permissions().create(fileId=token_folder_id, body=permission).execute()
|
101 |
+
|
102 |
+
return token_main_folder_id, token_folder_id
|
103 |
+
|
104 |
+
def Create_Token(name):
|
105 |
+
CLIENT_SECRET_FILE = CLIENT_FILE_NAME
|
106 |
+
API_SERVICE_NAME = API_DRIVE
|
107 |
+
# API_VERSION = API_VERSION
|
108 |
+
# SCOPES = SCOPES
|
109 |
+
|
110 |
+
cred = None
|
111 |
+
pickle_file = f'{name}.pickle'
|
112 |
+
|
113 |
+
if os.path.exists(pickle_file):
|
114 |
+
with open(pickle_file, 'rb') as token:
|
115 |
+
cred = pickle.load(token)
|
116 |
+
|
117 |
+
if not cred or not cred.valid:
|
118 |
+
if cred and cred.expired and cred.refresh_token:
|
119 |
+
cred.refresh(Request())
|
120 |
+
else:
|
121 |
+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
|
122 |
+
cred = flow.run_local_server()
|
123 |
+
|
124 |
+
with open(pickle_file, 'wb') as token:
|
125 |
+
pickle.dump(cred, token)
|
126 |
+
|
127 |
+
try:
|
128 |
+
# service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
|
129 |
+
return cred
|
130 |
+
except Exception as e:
|
131 |
+
print('Unable to connect.')
|
132 |
+
print(e)
|
133 |
+
return None
|
134 |
+
|
135 |
+
# ///////////////////////////// FROM HERE IT STARTS //////////////////////////////////
|
136 |
+
|
137 |
+
def Create_Token_Drive(name="current"):
|
138 |
+
service = Create_Drive_Token()
|
139 |
+
token_main_folder_id, token_folder_id = Check_Token_Main_Folder()
|
140 |
+
try:
|
141 |
+
response = (
|
142 |
+
service.files()
|
143 |
+
.list(
|
144 |
+
q=f"'{token_main_folder_id}' in parents and name contains 'token_json'",
|
145 |
+
spaces="drive",
|
146 |
+
fields="files(id, name)",
|
147 |
+
)
|
148 |
+
.execute()
|
149 |
+
)
|
150 |
+
list_all_files = response.get("files", [])
|
151 |
+
is_selected = False
|
152 |
+
file_name = ""
|
153 |
+
for list_files in list_all_files:
|
154 |
+
print("List Files = ", list_files["name"])
|
155 |
+
if list_files["name"].startswith("token_json"):
|
156 |
+
latest_file_id = str(list_files["id"])
|
157 |
+
existing_data = service.files().get_media(fileId=latest_file_id).execute()
|
158 |
+
if existing_data:
|
159 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
160 |
+
with open(list_files["name"], "w", encoding="utf-8") as json_file:
|
161 |
+
json.dump(existing_details, json_file, ensure_ascii=False)
|
162 |
+
if existing_details.get(name):
|
163 |
+
is_selected = True
|
164 |
+
token_id = existing_details[name]["token_file_id"]
|
165 |
+
url = f"https://drive.google.com/file/d/{token_id}/view?usp=sharing"
|
166 |
+
output = "current_token.pickle"
|
167 |
+
gdown.download(url=url, output=output, fuzzy=True)
|
168 |
+
with open(output, "rb") as cred:
|
169 |
+
creds = pickle.load(cred)
|
170 |
+
service = build(API_DRIVE, API_VERSION, credentials=creds)
|
171 |
+
os.remove(output)
|
172 |
+
return service, existing_details[name]["token_name"]
|
173 |
+
|
174 |
+
else:
|
175 |
+
|
176 |
+
is_selected = True
|
177 |
+
token_file = Create_Token(name)
|
178 |
+
token_file_path = f'{name}.pickle'
|
179 |
+
file_metadata = {"name": name, "parents": [token_folder_id]}
|
180 |
+
media = MediaFileUpload(token_file_path, mimetype="application/pdf")
|
181 |
+
newFile = (
|
182 |
+
service.files()
|
183 |
+
.create(body=file_metadata, media_body=media, fields="id")
|
184 |
+
.execute()
|
185 |
+
)
|
186 |
+
token_id = newFile.get("id")
|
187 |
+
expiry_time = datetime.now() + timedelta(hours=24)
|
188 |
+
existing_details[name]={
|
189 |
+
"token_name": name,
|
190 |
+
"token_file_id": token_id,
|
191 |
+
"expires_at": expiry_time.strftime("%Y-%m-%dT%H:%M:%SZ")
|
192 |
+
}
|
193 |
+
file_name = list_files["name"]
|
194 |
+
with open(file_name, "w", encoding="utf-8") as json_file:
|
195 |
+
json.dump(existing_details, json_file)
|
196 |
+
json_file.close()
|
197 |
+
file_metadata = {"name": file_name, "parents": [token_main_folder_id]}
|
198 |
+
media = MediaFileUpload(file_name, mimetype="application/json")
|
199 |
+
service.files().update(fileId=latest_file_id, media_body=media).execute()
|
200 |
+
token_id = existing_details[name]["token_file_id"]
|
201 |
+
url = f"https://drive.google.com/file/d/{token_id}/view?usp=sharing"
|
202 |
+
output = "current_token.pickle"
|
203 |
+
gdown.download(url=url, output=output, fuzzy=True)
|
204 |
+
with open(output, "rb") as cred:
|
205 |
+
creds = pickle.load(cred)
|
206 |
+
service = build(API_DRIVE, API_VERSION, credentials=creds)
|
207 |
+
os.remove(output)
|
208 |
+
os.remove(token_file_path)
|
209 |
+
return service, existing_details[name]["token_name"]
|
210 |
+
|
211 |
+
if not is_selected:
|
212 |
+
token_file = Create_Token(name)
|
213 |
+
file_metadata = {"name": name, "parents": [token_folder_id]}
|
214 |
+
token_file_path = f'{name}.pickle'
|
215 |
+
media = MediaFileUpload(token_file_path, mimetype="application/pdf")
|
216 |
+
# media = MediaFileUpload(token_file, mimetype="application/pdf")
|
217 |
+
newFile = (
|
218 |
+
service.files()
|
219 |
+
.create(body=file_metadata, media_body=media, fields="id")
|
220 |
+
.execute()
|
221 |
+
)
|
222 |
+
token_id = newFile.get("id")
|
223 |
+
expiry_time = datetime.now() + timedelta(hours=24)
|
224 |
+
existing_details = {}
|
225 |
+
existing_details[name]={
|
226 |
+
"token_name": name,
|
227 |
+
"token_file_id": token_id,
|
228 |
+
"expires_at": expiry_time.strftime("%Y-%m-%dT%H:%M:%SZ")
|
229 |
+
}
|
230 |
+
file_name = f"token_json.json"
|
231 |
+
json_data = json.dumps(existing_details)
|
232 |
+
with open(file_name, "w") as f:
|
233 |
+
f.write(json_data)
|
234 |
+
file_metadata = {"name": file_name, "parents": [token_main_folder_id]}
|
235 |
+
media = MediaFileUpload(
|
236 |
+
file_name, mimetype="application/json", resumable=True
|
237 |
+
)
|
238 |
+
service.files().create(
|
239 |
+
body=file_metadata, media_body=media, fields="id"
|
240 |
+
).execute()
|
241 |
+
token_id = existing_details[name]["token_file_id"]
|
242 |
+
url = f"https://drive.google.com/file/d/{token_id}/view?usp=sharing"
|
243 |
+
output = "current_token.pickle"
|
244 |
+
gdown.download(url=url, output=output, fuzzy=True)
|
245 |
+
|
246 |
+
with open(output, "rb") as cred:
|
247 |
+
service = build(API_DRIVE, API_VERSION, credentials=cred)
|
248 |
+
os.remove(output)
|
249 |
+
os.remove(file_name)
|
250 |
+
return service, existing_details[name]["token_name"]
|
251 |
+
|
252 |
+
except Exception as e:
|
253 |
+
print(f"An error occurred here: {str(e)}")
|
254 |
+
|
255 |
+
def Expire_Token_File():
|
256 |
+
service = Create_Drive_Token()
|
257 |
+
token_main_folder_id, token_folder_id = Check_Token_Main_Folder()
|
258 |
+
try:
|
259 |
+
response = (
|
260 |
+
service.files()
|
261 |
+
.list(
|
262 |
+
q=f"'{token_main_folder_id}' in parents and name contains 'token_json'",
|
263 |
+
spaces="drive",
|
264 |
+
fields="files(id, name)",
|
265 |
+
)
|
266 |
+
.execute()
|
267 |
+
)
|
268 |
+
list_all_files = response.get("files", [])
|
269 |
+
for list_files in list_all_files:
|
270 |
+
if list_files["name"].startswith("token_json"):
|
271 |
+
latest_file_id = str(list_files["id"])
|
272 |
+
existing_data = service.files().get_media(fileId=latest_file_id).execute()
|
273 |
+
if existing_data:
|
274 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
275 |
+
|
276 |
+
with open(list_files["name"], "w", encoding="utf-8") as json_file:
|
277 |
+
json.dump(existing_details, json_file, ensure_ascii=False)
|
278 |
+
|
279 |
+
for details in existing_details:
|
280 |
+
if datetime.strptime(details["expires_at"], "%Y-%m-%dT%H:%M:%SZ") < datetime.now():
|
281 |
+
drive_id = details["token_file_id"]
|
282 |
+
service.files().delete(fileId=details["token_file_id"]).execute()
|
283 |
+
del existing_details[details["token_name"]]
|
284 |
+
service.files().delete(fileId=drive_id).execute()
|
285 |
+
return True
|
286 |
+
return False
|
287 |
+
except Exception as e:
|
288 |
+
print(f"An error occurred here: {str(e)}")
|
289 |
+
|
290 |
+
def Delete_Token_File(name):
|
291 |
+
service = Create_Drive_Token()
|
292 |
+
token_main_folder_id, token_folder_id = Check_Token_Main_Folder()
|
293 |
+
try:
|
294 |
+
response = (
|
295 |
+
service.files()
|
296 |
+
.list(
|
297 |
+
q=f"'{token_main_folder_id}' in parents and name contains 'token_json'",
|
298 |
+
spaces="drive",
|
299 |
+
fields="files(id, name)",
|
300 |
+
)
|
301 |
+
.execute()
|
302 |
+
)
|
303 |
+
list_all_files = response.get("files", [])
|
304 |
+
for list_files in list_all_files:
|
305 |
+
if list_files["name"].startswith("token_json"):
|
306 |
+
latest_file_id = str(list_files["id"])
|
307 |
+
existing_data = service.files().get_media(fileId=latest_file_id).execute()
|
308 |
+
if existing_data:
|
309 |
+
existing_details = json.loads(existing_data.decode("utf-8"))
|
310 |
+
|
311 |
+
with open(list_files["name"], "w", encoding="utf-8") as json_file:
|
312 |
+
json.dump(existing_details, json_file, ensure_ascii=False)
|
313 |
+
|
314 |
+
for details in existing_details:
|
315 |
+
if details[name] == name:
|
316 |
+
drive_id = details["token_file_id"]
|
317 |
+
service.files().delete(fileId=details["token_file_id"]).execute()
|
318 |
+
del existing_details[details["token_name"]]
|
319 |
+
service.files().delete(fileId=drive_id).execute()
|
320 |
+
return True
|
321 |
+
return False
|
322 |
+
except Exception as e:
|
323 |
+
print(f"An error occurred here: {str(e)}")
|
token_operation/token_secret.json
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"\u0069\u006e\u0073\u0074\u0061\u006c\u006c\u0065\u0064": {
|
3 |
+
"\u0063\u006c\u0069\u0065\u006e\u0074\u005f\u0069\u0064": "\u0034\u0039\u0032\u0037\u0037\u0030\u0030\u0035\u0039\u0031\u0039\u0037\u002d\u0034\u006e\u0071\u006c\u006e\u0037\u0034\u0035\u0034\u0076\u0034\u0035\u0038\u0073\u0033\u006f\u006f\u0039\u0074\u006e\u006a\u0061\u006d\u0067\u0064\u0039\u006f\u0067\u0068\u0068\u0035\u006f\u002e\u0061\u0070\u0070\u0073\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0075\u0073\u0065\u0072\u0063\u006f\u006e\u0074\u0065\u006e\u0074\u002e\u0063\u006f\u006d",
|
4 |
+
"\u0070\u0072\u006f\u006a\u0065\u0063\u0074\u005f\u0069\u0064": "\u0072\u0065\u0070\u0065\u0074\u0061\u0074\u0069\u0076\u0065",
|
5 |
+
"\u0061\u0075\u0074\u0068\u005f\u0075\u0072\u0069": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0061\u0063\u0063\u006f\u0075\u006e\u0074\u0073\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u002e\u0063\u006f\u006d\u002f\u006f\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002f\u0061\u0075\u0074\u0068",
|
6 |
+
"\u0074\u006f\u006b\u0065\u006e\u005f\u0075\u0072\u0069": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0061\u0070\u0069\u0073\u002e\u0063\u006f\u006d\u002f\u0074\u006f\u006b\u0065\u006e",
|
7 |
+
"\u0061\u0075\u0074\u0068\u005f\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0072\u005f\u0078\u0035\u0030\u0039\u005f\u0063\u0065\u0072\u0074\u005f\u0075\u0072\u006c": "\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u0067\u006f\u006f\u0067\u006c\u0065\u0061\u0070\u0069\u0073\u002e\u0063\u006f\u006d\u002f\u006f\u0061\u0075\u0074\u0068\u0032\u002f\u0076\u0031\u002f\u0063\u0065\u0072\u0074\u0073",
|
8 |
+
"\u0063\u006c\u0069\u0065\u006e\u0074\u005f\u0073\u0065\u0063\u0072\u0065\u0074": "\u0047\u004f\u0043\u0053\u0050\u0058\u002d\u0043\u0061\u0059\u0070\u004f\u0074\u0046\u0047\u0056\u006c\u0066\u007a\u0042\u006f\u002d\u0063\u0036\u0079\u0062\u0039\u006c\u0059\u004e\u006a\u0039\u0067\u0074\u0064",
|
9 |
+
"\u0072\u0065\u0064\u0069\u0072\u0065\u0063\u0074\u005f\u0075\u0072\u0069\u0073": ["\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u006c\u006f\u0063\u0061\u006c\u0068\u006f\u0073\u0074"]
|
10 |
+
}
|
11 |
+
}
|
12 |
+
|