zinoubm commited on
Commit
3cd8a78
·
1 Parent(s): 9f31546

adding google docs functionality

Browse files
app.py CHANGED
@@ -2,9 +2,8 @@ import os
2
  import gradio as gr
3
  from dotenv import load_dotenv
4
  import openai
5
-
6
- # from utils import serialize
7
  from utils import compress
 
8
 
9
  from description import DESCRIPTION
10
 
@@ -38,21 +37,6 @@ def chat(passage, max_tokens=256, temprature=0, debug=False):
38
  return summary["choices"][0]["message"]["content"].strip()
39
 
40
 
41
- # def chat(message, history):
42
- # """
43
- # Sends a request to the OpenAi api based on the user input and the history
44
- # """
45
- # messages = serialize(history)
46
- # messages.append({"role": "user", "content": message})
47
-
48
- # completion = openai.ChatCompletion.create(
49
- # model="gpt-3.5-turbo",
50
- # messages=messages,
51
- # )
52
-
53
- # return completion["choices"][0]["message"]["content"].strip()
54
-
55
-
56
  def transcribe(audio_file):
57
  audio_file = open(audio_file, "rb")
58
  transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
@@ -63,8 +47,18 @@ def transcribe(audio_file):
63
  def predict(input, history=[]):
64
  compress(input)
65
  transcription = transcribe(input)
66
-
67
  answer = chat(transcription)
 
 
 
 
 
 
 
 
 
 
 
68
  history.append((transcription, answer))
69
  response = history
70
  return response, history
 
2
  import gradio as gr
3
  from dotenv import load_dotenv
4
  import openai
 
 
5
  from utils import compress
6
+ from google_manager.fassade import Fassade
7
 
8
  from description import DESCRIPTION
9
 
 
37
  return summary["choices"][0]["message"]["content"].strip()
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def transcribe(audio_file):
41
  audio_file = open(audio_file, "rb")
42
  transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
 
47
  def predict(input, history=[]):
48
  compress(input)
49
  transcription = transcribe(input)
 
50
  answer = chat(transcription)
51
+
52
+ # upload the input/answer to google drive
53
+ doc_content = f"""
54
+ user:
55
+ {transcription}
56
+
57
+ summary:
58
+ {answer}
59
+ """
60
+ Fassade.upload_to_drive(doc_content)
61
+
62
  history.append((transcription, answer))
63
  response = history
64
  return response, history
credentials.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"web":{"client_id":"802510675416-0929ppk1vqugs5nnuiakamt1ugec6qa7.apps.googleusercontent.com","project_id":"gptsummary","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost:7860/"],"javascript_origins":["http://localhost:7860"]}}
google_manager/__init__.py ADDED
File without changes
google_manager/auth.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os.path
2
+ from google.auth.transport.requests import Request
3
+ from google.oauth2.credentials import Credentials
4
+ from google_auth_oauthlib.flow import InstalledAppFlow
5
+
6
+
7
+ def authenticate(SCOPES):
8
+ """
9
+ Request access for the google docs api
10
+ """
11
+ creds = None
12
+
13
+ if os.path.exists("token.json"):
14
+ creds = Credentials.from_authorized_user_file("token.json", SCOPES)
15
+
16
+ # If there are no (valid) credentials available, let the user log in.
17
+ if not creds or not creds.valid:
18
+ if creds and creds.expired and creds.refresh_token:
19
+ creds.refresh(Request())
20
+ else:
21
+ flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
22
+ creds = flow.run_local_server(port=7860)
23
+
24
+ # Save the credentials for the next run
25
+ with open("token.json", "w") as token:
26
+ token.write(creds.to_json())
27
+
28
+ return creds
google_manager/constants.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ SCOPES = [
2
+ "https://www.googleapis.com/auth/documents",
3
+ "https://www.googleapis.com/auth/drive.file",
4
+ ]
5
+
6
+
7
+ FOLDER_NAME = "GptSummary"
google_manager/docs.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from googleapiclient.discovery import build
3
+ from googleapiclient.errors import HttpError
4
+
5
+
6
+ def save_doc(creds, title, content):
7
+ try:
8
+ service = build("docs", "v1", credentials=creds)
9
+
10
+ # create a document
11
+ title = title
12
+ body = {"title": title}
13
+ doc = service.documents().create(body=body).execute()
14
+ print("Created document with title: {0}".format(doc.get("title")))
15
+
16
+ # Get the ID of the new file
17
+ doc_id = doc["documentId"]
18
+
19
+ # Write "Hello World" in the new file
20
+ doc_content = content
21
+ service.documents().get(documentId=doc_id).execute()
22
+ requests = [{"insertText": {"location": {"index": 1}, "text": doc_content}}]
23
+ result = (
24
+ service.documents()
25
+ .batchUpdate(documentId=doc_id, body={"requests": requests})
26
+ .execute()
27
+ )
28
+
29
+ print(f"A new Google Doc file has been created with ID: {doc_id}")
30
+ return result
31
+
32
+ except HttpError as err:
33
+ print(err)
34
+
35
+
36
+ def move_doc(creds, document_id, folder_id):
37
+ service = build("drive", "v3", credentials=creds)
38
+
39
+ try:
40
+ # Get the current parents of the document
41
+ file = service.files().get(fileId=document_id, fields="parents").execute()
42
+ current_parents = ",".join(file.get("parents"))
43
+
44
+ # Move the document to the new folder
45
+ file = (
46
+ service.files()
47
+ .update(
48
+ fileId=document_id,
49
+ addParents=folder_id,
50
+ removeParents=current_parents,
51
+ fields="id, parents",
52
+ )
53
+ .execute()
54
+ )
55
+
56
+ print(
57
+ f'The document with ID {file.get("id")} was moved to the folder with ID {folder_id}.'
58
+ )
59
+
60
+ except HttpError as error:
61
+ print(f"An error occurred: {error}")
62
+
63
+
64
+ def name_doc():
65
+ """
66
+ Gets and format the time to generate document name
67
+ """
68
+ now = datetime.datetime.now()
69
+ timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
70
+
71
+ return f"summary_{timestamp}.txt"
google_manager/docs_api.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import os.path
2
+
3
+ # from google.auth.transport.requests import Request
4
+ # from google.oauth2.credentials import Credentials
5
+ # from google_auth_oauthlib.flow import InstalledAppFlow
6
+ # from googleapiclient.discovery import build
7
+ # from googleapiclient.errors import HttpError
8
+
9
+ # # If modifying these scopes, delete the file token.json.
10
+ # SCOPES = [
11
+ # "https://www.googleapis.com/auth/documents",
12
+ # "https://www.googleapis.com/auth/drive.file",
13
+ # ]
14
+
15
+
16
+ # def get_auth():
17
+ # """
18
+ # Request access for the google docs api
19
+ # """
20
+ # creds = None
21
+ # # The file token.json stores the user's access and refresh tokens, and is
22
+ # # created automatically when the authorization flow completes for the first
23
+ # # time.
24
+ # if os.path.exists("token.json"):
25
+ # creds = Credentials.from_authorized_user_file("token.json", SCOPES)
26
+ # # If there are no (valid) credentials available, let the user log in.
27
+ # if not creds or not creds.valid:
28
+ # if creds and creds.expired and creds.refresh_token:
29
+ # creds.refresh(Request())
30
+ # else:
31
+ # flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
32
+ # creds = flow.run_local_server(port=7860)
33
+ # # Save the credentials for the next run
34
+ # with open("token.json", "w") as token:
35
+ # token.write(creds.to_json())
36
+
37
+
38
+ # def save_doc(creds, title):
39
+ # try:
40
+ # service = build("docs", "v1", credentials=creds)
41
+
42
+ # # create a document
43
+ # title = "My Document"
44
+ # body = {"title": title}
45
+ # doc = service.documents().create(body=body).execute()
46
+ # print("Created document with title: {0}".format(doc.get("title")))
47
+
48
+ # except HttpError as err:
49
+ # print(err)
50
+
51
+
52
+ # import datetime
53
+
54
+ # # Get the current date and time
55
+ # now = datetime.datetime.now()
56
+
57
+ # # Format the date and time as a string
58
+ # timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
59
+
60
+ # # Define the filename with the timestamp
61
+ # filename = f"file_{timestamp}.txt"
google_manager/drive.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from googleapiclient.discovery import build
2
+ from googleapiclient.errors import HttpError
3
+
4
+
5
+ def create_folder(creds, folder_name, parent_folder_id=None) -> str:
6
+ """ """
7
+ drive_service = build("drive", "v3", credentials=creds)
8
+
9
+ # Create a metadata dict with the folder name and parent folder ID (if any)
10
+ folder_metadata = {
11
+ "name": folder_name,
12
+ "mimeType": "application/vnd.google-apps.folder",
13
+ }
14
+ if parent_folder_id:
15
+ folder_metadata["parents"] = [parent_folder_id]
16
+
17
+ try:
18
+ folder = (
19
+ drive_service.files().create(body=folder_metadata, fields="id").execute()
20
+ )
21
+ print(f'Folder "{folder_name}" with ID "{folder["id"]}" created successfully')
22
+ return folder["id"]
23
+ except HttpError as error:
24
+ print(f"An error occurred while creating the Google Drive folder: {error}")
25
+ return None
26
+
27
+
28
+ def search_folder(creds, folder_name):
29
+ """ """
30
+ try:
31
+ # create drive api client
32
+ service = build("drive", "v3", credentials=creds)
33
+ files = []
34
+ page_token = None
35
+
36
+ while True:
37
+ response = (
38
+ service.files()
39
+ .list(
40
+ q="mimeType='application/vnd.google-apps.folder' and trashed=false",
41
+ spaces="drive",
42
+ fields="nextPageToken, files(id, name)",
43
+ pageToken=page_token,
44
+ )
45
+ .execute()
46
+ )
47
+ for file in response.get("files", []):
48
+ # Process change
49
+ print(f'Found file: {file.get("name")}, {file.get("id")}')
50
+ files.extend(response.get("files", []))
51
+ page_token = response.get("nextPageToken", None)
52
+ if page_token is None:
53
+ break
54
+
55
+ except HttpError as error:
56
+ print(f"An error occurred: {error}")
57
+ files = None
58
+
59
+ return files
google_manager/fassade.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from google_manager.auth import authenticate
2
+ from google_manager.drive import create_folder, search_folder
3
+ from google_manager.docs import save_doc, move_doc, name_doc
4
+ from google_manager.constants import SCOPES, FOLDER_NAME
5
+
6
+
7
+ class Fassade:
8
+ def upload_to_drive(content, FOLDER_NAME=FOLDER_NAME):
9
+ FOLDER_NAME = "GptSummary"
10
+ creds = authenticate(SCOPES)
11
+ files = search_folder(creds, FOLDER_NAME)
12
+
13
+ if not files:
14
+ folder_id = create_folder(creds, FOLDER_NAME)
15
+
16
+ else:
17
+ folder_id = files.pop()["id"]
18
+
19
+ doc_name = name_doc()
20
+ doc_response = save_doc(creds, doc_name, content)
21
+
22
+ move_doc(creds, doc_response["documentId"], folder_id)
google_manager/test.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from fassade import Fassade
2
+
3
+ CONTENT = """
4
+ Cars were invented in 1886, when German inventor Carl Benz patented his Benz Patent-Motorwagen.[3][4][5] Cars became widely available during the 20th century. One of the first cars affordable by the masses was the 1908 Model T, an American car manufactured by the Ford Motor Company. Cars were rapidly adopted in the US, where they replaced horse-drawn carriages.[6] In Europe and other parts of the world, demand for automobiles did not increase until after World War II.[7] The car is considered an essential part of the developed economy
5
+ """
6
+
7
+
8
+ if __name__ == "__main__":
9
+ Fassade.upload_to_drive(CONTENT)
token.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"token": "ya29.a0AVvZVsovFtXFOt_y0MPPJ3Ezf3Sgnps6rqvbNu2rM9xG31Hqgnd0vRATIxRBOAp2VHtKCfprshTzijyWLSjYcVDLkxdNxnfUvSSQNi9dAIb-CDXln-fkrG9rhOjIUR-1LtXjvs4X3uV2EuEhTfH8R7rgl_Pg-SkaCgYKAZMSARASFQGbdwaIJJ8fbDLgh97SY8g0zKL0hw0166", "refresh_token": "1//03P_ayTe4UkoXCgYIARAAGAMSNwF-L9IribM55gnIuAF39QKGPVCOLhzhyXBe_4znQdrZr5g6SnVcbk3h11WlxPlh5YR_QRDGmSg", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "802510675416-0929ppk1vqugs5nnuiakamt1ugec6qa7.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/documents", "https://www.googleapis.com/auth/drive.file"], "expiry": "2023-03-06T17:44:39.266964Z"}