zinoubm commited on
Commit
2f9dcad
·
1 Parent(s): c4b08e9

final touches

Browse files
Procfile CHANGED
@@ -1 +0,0 @@
1
- web: uvicorn main:app --host=0.0.0.0 --port={PORT:-8000}
 
 
app.py CHANGED
@@ -1,42 +1,46 @@
1
  from fastapi import FastAPI, Request
2
- from fastapi.responses import RedirectResponse
3
- from fastapi.responses import HTMLResponse
4
  from fastapi.staticfiles import StaticFiles
5
  from google_manager.constants import SCOPES
6
- from pages.auth_page import html_content
7
  import google_auth_oauthlib.flow
8
  from starlette.middleware.sessions import SessionMiddleware
9
-
10
  import gradio as gr
11
  from gpt_summary import Ui
12
 
13
-
14
  app = FastAPI()
 
 
15
  app.add_middleware(SessionMiddleware, secret_key="mysecret")
 
 
16
  app.mount("/assets", StaticFiles(directory="assets"), name="assets")
17
 
18
 
19
  @app.get("/", response_class=HTMLResponse)
20
  async def home(request: Request):
21
- print("request session")
22
- print(request.session)
23
- state = request.session.get("state", None)
24
- print("state")
25
- print(state)
26
- if state:
27
- return RedirectResponse("/gradio")
28
- else:
29
  return HTMLResponse(content=html_content, status_code=200)
 
 
30
 
31
 
 
32
  @app.get("/auth")
33
- async def integreate_google(request: Request):
 
34
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
35
  "credentials.json", scopes=SCOPES
36
  )
37
-
38
  flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
39
-
40
  authorization_url, state = flow.authorization_url(
41
  access_type="offline",
42
  include_granted_scopes="true",
@@ -45,10 +49,10 @@ async def integreate_google(request: Request):
45
  return RedirectResponse(url=authorization_url)
46
 
47
 
 
48
  @app.get("/auth_callback")
49
  async def auth_callback(request: Request):
50
  state = request.session["state"]
51
-
52
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
53
  "credentials.json", scopes=SCOPES, state=state
54
  )
@@ -56,17 +60,16 @@ async def auth_callback(request: Request):
56
 
57
  # Use the authorization server's response to fetch the OAuth 2.0 tokens.
58
  authorization_response = str(request.url)
59
-
60
  flow.fetch_token(authorization_response=authorization_response)
61
-
62
  credentials = flow.credentials
63
  request.session["credentials"] = credentials_to_dict(credentials)
64
- return RedirectResponse("/gradio")
65
 
66
 
 
67
  app = gr.mount_gradio_app(app, Ui, path="/gradio")
68
 
69
-
70
  def credentials_to_dict(credentials):
71
  return {
72
  "token": credentials.token,
 
1
  from fastapi import FastAPI, Request
2
+ from fastapi.responses import RedirectResponse, HTMLResponse
 
3
  from fastapi.staticfiles import StaticFiles
4
  from google_manager.constants import SCOPES
5
+ from pages.load_page import load_page
6
  import google_auth_oauthlib.flow
7
  from starlette.middleware.sessions import SessionMiddleware
 
8
  import gradio as gr
9
  from gpt_summary import Ui
10
 
11
+ # Create an instance of the FastAPI app
12
  app = FastAPI()
13
+
14
+ # Add Session middleware to the app
15
  app.add_middleware(SessionMiddleware, secret_key="mysecret")
16
+
17
+ # Mount the static files directory
18
  app.mount("/assets", StaticFiles(directory="assets"), name="assets")
19
 
20
 
21
  @app.get("/", response_class=HTMLResponse)
22
  async def home(request: Request):
23
+ # Load the html content of the auth page
24
+ html_content = load_page("./pages/auth_page.html")
25
+ # print("request session")
26
+ # print(request.session)
27
+ creds = request.session.get("credentials", None)
28
+ # print("state")
29
+ # print(creds)
30
+ if not creds or not creds.valid:
31
  return HTMLResponse(content=html_content, status_code=200)
32
+ else:
33
+ return RedirectResponse("/gradio/?__theme=dark")
34
 
35
 
36
+ # Define the endpoint for Google authentication
37
  @app.get("/auth")
38
+ async def integrate_google(request: Request):
39
+ # print(SCOPES)
40
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
41
  "credentials.json", scopes=SCOPES
42
  )
 
43
  flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
 
44
  authorization_url, state = flow.authorization_url(
45
  access_type="offline",
46
  include_granted_scopes="true",
 
49
  return RedirectResponse(url=authorization_url)
50
 
51
 
52
+ # Define the callback endpoint after successful authentication
53
  @app.get("/auth_callback")
54
  async def auth_callback(request: Request):
55
  state = request.session["state"]
 
56
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
57
  "credentials.json", scopes=SCOPES, state=state
58
  )
 
60
 
61
  # Use the authorization server's response to fetch the OAuth 2.0 tokens.
62
  authorization_response = str(request.url)
 
63
  flow.fetch_token(authorization_response=authorization_response)
 
64
  credentials = flow.credentials
65
  request.session["credentials"] = credentials_to_dict(credentials)
66
+ return RedirectResponse("/gradio?__theme=dark")
67
 
68
 
69
+ # Mount the Gradio UI
70
  app = gr.mount_gradio_app(app, Ui, path="/gradio")
71
 
72
+ # Define a helper function to convert credentials to dictionary
73
  def credentials_to_dict(credentials):
74
  return {
75
  "token": credentials.token,
google_manager/auth.py CHANGED
@@ -1,80 +1,16 @@
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
- from google_manager.constants import SCOPES
6
-
7
- from starlette.responses import RedirectResponse
8
- from gradio.routes import App
9
-
10
- import google.oauth2.credentials
11
  import google_auth_oauthlib.flow
12
 
13
 
14
- def authenticate(SCOPES):
15
- """
16
- Request access for the google docs api
17
- """
18
- creds = None
19
-
20
- if os.path.exists("token.json"):
21
- creds = Credentials.from_authorized_user_file("token.json", SCOPES)
22
-
23
- # If there are no (valid) credentials available, let the user log in.
24
- if not creds or not creds.valid:
25
- if creds and creds.expired and creds.refresh_token:
26
- creds.refresh(Request())
27
- else:
28
- flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
29
- creds = flow.run_local_server(port=7200)
30
-
31
- # Save the credentials for the next run
32
- with open("token.json", "w") as token:
33
- token.write(creds.to_json())
34
-
35
- return creds
36
-
37
-
38
  def get_oauth_url(SCOPES, redirect_url):
39
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
40
  "credentials.json", scopes=SCOPES
41
  )
42
 
43
- # flow.redirect_uri = "http://127.0.0.1:8000/gradio/?__theme=dark"
44
- # flow.redirect_uri = app.url_path_for(redirect_url)
45
  flow.redirect_uri = redirect_url
46
 
47
- # Generate URL for request to Google's OAuth 2.0 server.
48
- # Use kwargs to set optional request parameters.
49
  authorization_url, state = flow.authorization_url(
50
- # Enable offline access so that you can refresh an access token without
51
- # re-prompting the user for permission. Recommended for web server apps.
52
  access_type="offline",
53
- # Enable incremental authorization. Recommended as a best practice.
54
  include_granted_scopes="true",
55
  )
56
- print("state")
57
- print(state)
58
 
59
  return authorization_url
60
-
61
-
62
- def authenticate_production(SCOPES):
63
- creds = None
64
-
65
- if os.path.exists("token.json"):
66
- creds = Credentials.from_authorized_user_file("token.json", SCOPES)
67
-
68
- # If there are no (valid) credentials available, let the user log in.
69
- if not creds or not creds.valid:
70
- if creds and creds.expired and creds.refresh_token:
71
- creds.refresh(Request())
72
- else:
73
- flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
74
- creds = flow.run_local_server(port=7200)
75
-
76
- # Save the credentials for the next run
77
- with open("token.json", "w") as token:
78
- token.write(creds.to_json())
79
-
80
- return creds
 
 
 
 
 
 
 
 
 
 
 
1
  import google_auth_oauthlib.flow
2
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def get_oauth_url(SCOPES, redirect_url):
5
  flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
6
  "credentials.json", scopes=SCOPES
7
  )
8
 
 
 
9
  flow.redirect_uri = redirect_url
10
 
 
 
11
  authorization_url, state = flow.authorization_url(
 
 
12
  access_type="offline",
 
13
  include_granted_scopes="true",
14
  )
 
 
15
 
16
  return authorization_url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
google_manager/constants.py CHANGED
@@ -1,7 +1,10 @@
1
- SCOPES = [
2
- "https://www.googleapis.com/auth/documents",
3
- "https://www.googleapis.com/auth/drive.file",
4
- ]
5
 
 
6
 
7
- FOLDER_NAME = "GptSummary"
 
 
 
 
1
+ import os
2
+ import json
3
+ from dotenv import load_dotenv
 
4
 
5
+ load_dotenv()
6
 
7
+
8
+ SCOPES = os.getenv("SCOPES")
9
+ SCOPES = json.loads(SCOPES)
10
+ FOLDER_NAME = os.getenv("FOLDER_NAME")
google_manager/docs_api.py DELETED
@@ -1,61 +0,0 @@
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/fassade.py CHANGED
@@ -1,17 +1,15 @@
1
- from google_manager.auth import authenticate, authenticate_production
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 __init__(self):
9
  self.creds = None
10
 
11
- def upload_to_drive(content, FOLDER_NAME=FOLDER_NAME):
12
  FOLDER_NAME = "GptSummary"
13
 
14
- creds = authenticate_production(SCOPES)
15
  files = search_folder(creds, FOLDER_NAME)
16
 
17
  if not files:
 
 
1
  from google_manager.drive import create_folder, search_folder
2
  from google_manager.docs import save_doc, move_doc, name_doc
3
+ from google_manager.constants import FOLDER_NAME
4
 
5
 
6
  class Fassade:
7
  def __init__(self):
8
  self.creds = None
9
 
10
+ def upload_to_drive(creds, content, FOLDER_NAME=FOLDER_NAME):
11
  FOLDER_NAME = "GptSummary"
12
 
 
13
  files = search_folder(creds, FOLDER_NAME)
14
 
15
  if not files:
google_manager/test.py DELETED
@@ -1,9 +0,0 @@
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)
 
 
 
 
 
 
 
 
 
 
gpt_summary.py CHANGED
@@ -4,13 +4,8 @@ from dotenv import load_dotenv
4
  import openai
5
  from utils import compress
6
  from google_manager.fassade import Fassade
7
-
8
-
9
  from description import DESCRIPTION
10
-
11
- # fastApi
12
-
13
- import fastapi
14
  import gradio as gr
15
 
16
 
@@ -51,20 +46,31 @@ def transcribe(audio_file):
51
  return transcription
52
 
53
 
54
- def predict(input, history=[]):
55
  compress(input)
 
56
  transcription = transcribe(input)
 
 
57
  answer = chat(transcription)
 
58
 
59
  # upload the input/answer to google drive
60
- doc_content = f"""
61
- user:
62
- {transcription}
63
-
64
- summary:
65
- {answer}
66
- """
67
- # Fassade.upload_to_drive(doc_content)
 
 
 
 
 
 
 
68
 
69
  history.append((transcription, answer))
70
  response = history
 
4
  import openai
5
  from utils import compress
6
  from google_manager.fassade import Fassade
7
+ from google.oauth2.credentials import Credentials
 
8
  from description import DESCRIPTION
 
 
 
 
9
  import gradio as gr
10
 
11
 
 
46
  return transcription
47
 
48
 
49
+ def predict(input, request: gr.Request, history=[]):
50
  compress(input)
51
+ print("whisper starts")
52
  transcription = transcribe(input)
53
+ print("whisper ends")
54
+ print("gpt starts")
55
  answer = chat(transcription)
56
+ print("gpt ends")
57
 
58
  # upload the input/answer to google drive
59
+
60
+ doc_content = "user:\n" f"{transcription}\n" "\n" "summary:\n" f"{answer}\n"
61
+
62
+ creds_dict = vars(request.session).get("credentials", {})
63
+ if not creds_dict:
64
+ raise Exception("Credentials not found in session")
65
+
66
+ # Create credentials object from dictionary
67
+ try:
68
+ creds = Credentials.from_authorized_user_info(info=vars(creds_dict))
69
+
70
+ except Exception as e:
71
+ raise Exception(f"Invalid credentials in session with the following error{e}")
72
+
73
+ Fassade.upload_to_drive(creds, doc_content)
74
 
75
  history.append((transcription, answer))
76
  response = history
main.py CHANGED
@@ -1,17 +1,16 @@
1
  import uvicorn
2
- from pathlib import Path
3
  import os
4
  from app import app
5
 
6
  os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
7
  os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "1"
8
 
9
-
10
- # if __name__ == "__main__":
11
- # uvicorn.run(
12
- # f"app:app",
13
- # port=8000,
14
- # reload=True,
15
- # )
16
-
17
  app = app
 
 
 
 
 
 
 
 
 
1
  import uvicorn
 
2
  import os
3
  from app import app
4
 
5
  os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
6
  os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "1"
7
 
 
 
 
 
 
 
 
 
8
  app = app
9
+
10
+ # use this for local development
11
+ if __name__ == "__main__":
12
+ uvicorn.run(
13
+ f"app:app",
14
+ port=8000,
15
+ reload=True,
16
+ )
pages/auth_page.html CHANGED
@@ -1,7 +1,7 @@
1
  <html>
2
 
3
  <head>
4
- <title>Button Page</title>
5
  <link rel="preconnect" href="https://fonts.googleapis.com">
6
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
7
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap" rel="stylesheet">
@@ -12,8 +12,13 @@
12
 
13
  }
14
 
 
 
 
 
 
15
  .button-wrapper {
16
- height: 100vh;
17
  display: flex;
18
  justify-content: center;
19
  align-items: center;
@@ -46,6 +51,10 @@
46
  </head>
47
 
48
  <body>
 
 
 
 
49
  <div class="button-wrapper">
50
  <div>
51
  <button onclick="location.href='/auth'" class="integrate">
 
1
  <html>
2
 
3
  <head>
4
+ <title>Google Cloud Integration</title>
5
  <link rel="preconnect" href="https://fonts.googleapis.com">
6
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
7
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap" rel="stylesheet">
 
12
 
13
  }
14
 
15
+ h1 {
16
+ padding-top: 20vh;
17
+ color: white;
18
+ }
19
+
20
  .button-wrapper {
21
+ height: 60vh;
22
  display: flex;
23
  justify-content: center;
24
  align-items: center;
 
51
  </head>
52
 
53
  <body>
54
+ <!-- <h1>Want to save your work in Google Drive</h1> -->
55
+ <center>
56
+ <h1>Do you want to save your work in Google Drive?</h1>
57
+ </center>
58
  <div class="button-wrapper">
59
  <div>
60
  <button onclick="location.href='/auth'" class="integrate">
pages/auth_page.py DELETED
@@ -1,54 +0,0 @@
1
- html_content = """
2
- <html>
3
- <head>
4
- <title>Button Page</title>
5
- <link rel="preconnect" href="https://fonts.googleapis.com">
6
- <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
7
- <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap" rel="stylesheet">
8
- <style>
9
- body {
10
- background-color: #090F1E;
11
- font-family: 'Poppins', sans-serif;
12
-
13
- }
14
- .button-wrapper {
15
- height: 100vh;
16
- display: flex;
17
- justify-content: center;
18
- align-items: center;
19
- }
20
- button {
21
- border: none;
22
- margin-right: 10px;
23
- border-radius: 5px;
24
- color: #090F1E;
25
- font-weight: 400;
26
- font-size: 1rem;
27
- }
28
- .integrate {
29
- display: flex;
30
-
31
- align-items: center;
32
- }
33
- .skip {
34
- color: #95BDFF;
35
- text-decoration: none;
36
- }
37
-
38
- img {
39
- width: 2rem;
40
- }
41
- </style>
42
- </head>
43
- <body>
44
- <div class="button-wrapper">
45
- <div>
46
- <button onclick="location.href='/auth'" class="integrate">
47
- <img src="assets/google_logo.svg"> Integrate Google Cloud
48
- </button>
49
- </div>
50
- <a class="skip" href="/gradio?__theme=dark">skip</a>
51
- </div>
52
- </body>
53
- </html>
54
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pages/load_page.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ def load_page(path):
2
+ with open(path, "r") as file:
3
+ html_text = file.read()
4
+
5
+ return html_text
utils.py CHANGED
@@ -1,26 +1,7 @@
1
  import librosa
2
- from pathlib import Path, PurePath
3
  import soundfile as sf
4
 
5
 
6
- # def serialize(messages):
7
- # """
8
- # Converts a list of tuples where each element of the list represents a message to dictionary of
9
- # messages
10
- # """
11
-
12
- # serialized_messages = []
13
- # for message in messages:
14
- # serialized_message = [
15
- # {"role": "system", "content": message[0]},
16
- # {"role": "user", "content": message[1]},
17
- # ]
18
-
19
- # serialized_messages.extend(serialized_message)
20
-
21
- # return serialized_messages
22
-
23
-
24
  def compress(audio_file):
25
  y, s = librosa.load(audio_file, sr=8000) # Downsample 44.1kHz to 8kHz
26
  sf.write(audio_file, y, s, "PCM_24")
 
1
  import librosa
 
2
  import soundfile as sf
3
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def compress(audio_file):
6
  y, s = librosa.load(audio_file, sr=8000) # Downsample 44.1kHz to 8kHz
7
  sf.write(audio_file, y, s, "PCM_24")