Spaces:
Runtime error
Runtime error
prepairing for production
Browse files- app.py +26 -56
- google_manager/fassade.py +2 -2
- gpt_summary.py +43 -18
- main.py +8 -7
- utils.py +12 -0
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import google_auth_oauthlib.flow
|
|
| 8 |
from starlette.middleware.sessions import SessionMiddleware
|
| 9 |
import gradio as gr
|
| 10 |
from gpt_summary import Ui
|
|
|
|
| 11 |
|
| 12 |
# Create an instance of the FastAPI app
|
| 13 |
app = FastAPI()
|
|
@@ -23,42 +24,32 @@ app.mount("/assets", StaticFiles(directory="assets"), name="assets")
|
|
| 23 |
async def home(request: Request):
|
| 24 |
# Load the html content of the auth page
|
| 25 |
html_content = load_page("./pages/auth_page.html")
|
| 26 |
-
# print("request session")
|
| 27 |
-
# print(request.session)
|
| 28 |
-
# creds = request.session.get("credentials", None)
|
| 29 |
-
creds = request.session.get("credentials", {})
|
| 30 |
|
| 31 |
-
if
|
| 32 |
-
try:
|
| 33 |
-
creds = Credentials.from_authorized_user_info(info=creds)
|
| 34 |
-
|
| 35 |
-
except Exception as e:
|
| 36 |
-
raise Exception(
|
| 37 |
-
f"Invalid credentials in session with the following error{e}"
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
if not creds or not creds.valid:
|
| 41 |
return HTMLResponse(content=html_content, status_code=200)
|
|
|
|
| 42 |
else:
|
| 43 |
return RedirectResponse("/gradio/?__theme=dark")
|
| 44 |
|
| 45 |
|
| 46 |
# Define the endpoint for Google authentication
|
| 47 |
@app.get("/auth")
|
| 48 |
-
async def
|
| 49 |
-
# print(SCOPES)
|
| 50 |
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
| 51 |
"credentials.json", scopes=SCOPES
|
| 52 |
)
|
| 53 |
-
|
| 54 |
-
#
|
| 55 |
-
flow.redirect_uri =
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
|
|
|
|
|
|
|
|
|
| 62 |
authorization_url, state = flow.authorization_url(
|
| 63 |
access_type="offline",
|
| 64 |
include_granted_scopes="true",
|
|
@@ -74,26 +65,16 @@ async def auth_callback(request: Request):
|
|
| 74 |
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
| 75 |
"credentials.json", scopes=SCOPES, state=state
|
| 76 |
)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
#
|
| 81 |
-
#
|
| 82 |
-
#
|
| 83 |
-
#
|
| 84 |
-
#
|
| 85 |
-
#
|
| 86 |
-
#
|
| 87 |
-
# )
|
| 88 |
-
# flow.redirect_uri = "https://gpt-summary-u8pr.onrender.com/auth_callback"
|
| 89 |
-
# flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
|
| 90 |
-
flow.redirect_uri = (
|
| 91 |
-
request.url.scheme
|
| 92 |
-
+ "://"
|
| 93 |
-
+ request.url.hostname
|
| 94 |
-
+ (":" + str(request.url.port) if request.url.port else "")
|
| 95 |
-
+ app.url_path_for("auth_callback")
|
| 96 |
-
)
|
| 97 |
|
| 98 |
# Use the authorization server's response to fetch the OAuth 2.0 tokens.
|
| 99 |
authorization_response = str(request.url)
|
|
@@ -105,14 +86,3 @@ async def auth_callback(request: Request):
|
|
| 105 |
|
| 106 |
# Mount the Gradio UI
|
| 107 |
app = gr.mount_gradio_app(app, Ui, path="/gradio")
|
| 108 |
-
|
| 109 |
-
# Define a helper function to convert credentials to dictionary
|
| 110 |
-
def credentials_to_dict(credentials):
|
| 111 |
-
return {
|
| 112 |
-
"token": credentials.token,
|
| 113 |
-
"refresh_token": credentials.refresh_token,
|
| 114 |
-
"token_uri": credentials.token_uri,
|
| 115 |
-
"client_id": credentials.client_id,
|
| 116 |
-
"client_secret": credentials.client_secret,
|
| 117 |
-
"scopes": credentials.scopes,
|
| 118 |
-
}
|
|
|
|
| 8 |
from starlette.middleware.sessions import SessionMiddleware
|
| 9 |
import gradio as gr
|
| 10 |
from gpt_summary import Ui
|
| 11 |
+
from utils import credentials_to_dict
|
| 12 |
|
| 13 |
# Create an instance of the FastAPI app
|
| 14 |
app = FastAPI()
|
|
|
|
| 24 |
async def home(request: Request):
|
| 25 |
# Load the html content of the auth page
|
| 26 |
html_content = load_page("./pages/auth_page.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
if "credentials" not in request.session:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return HTMLResponse(content=html_content, status_code=200)
|
| 30 |
+
|
| 31 |
else:
|
| 32 |
return RedirectResponse("/gradio/?__theme=dark")
|
| 33 |
|
| 34 |
|
| 35 |
# Define the endpoint for Google authentication
|
| 36 |
@app.get("/auth")
|
| 37 |
+
async def authorize(request: Request):
|
|
|
|
| 38 |
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
| 39 |
"credentials.json", scopes=SCOPES
|
| 40 |
)
|
| 41 |
+
|
| 42 |
+
# for dev
|
| 43 |
+
flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
|
| 44 |
+
# for production
|
| 45 |
+
# flow.redirect_uri = (
|
| 46 |
+
# request.url.scheme
|
| 47 |
+
# + "://"
|
| 48 |
+
# + request.url.hostname
|
| 49 |
+
# + (":" + str(request.url.port) if request.url.port else "")
|
| 50 |
+
# + app.url_path_for("auth_callback")
|
| 51 |
+
# )
|
| 52 |
+
|
| 53 |
authorization_url, state = flow.authorization_url(
|
| 54 |
access_type="offline",
|
| 55 |
include_granted_scopes="true",
|
|
|
|
| 65 |
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
| 66 |
"credentials.json", scopes=SCOPES, state=state
|
| 67 |
)
|
| 68 |
+
# for dev
|
| 69 |
+
flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
|
| 70 |
+
# for production
|
| 71 |
+
# flow.redirect_uri = (
|
| 72 |
+
# request.url.scheme
|
| 73 |
+
# + "://"
|
| 74 |
+
# + request.url.hostname
|
| 75 |
+
# + (":" + str(request.url.port) if request.url.port else "")
|
| 76 |
+
# + app.url_path_for("auth_callback")
|
| 77 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Use the authorization server's response to fetch the OAuth 2.0 tokens.
|
| 80 |
authorization_response = str(request.url)
|
|
|
|
| 86 |
|
| 87 |
# Mount the Gradio UI
|
| 88 |
app = gr.mount_gradio_app(app, Ui, path="/gradio")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
google_manager/fassade.py
CHANGED
|
@@ -8,8 +8,6 @@ class Fassade:
|
|
| 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:
|
|
@@ -22,3 +20,5 @@ class Fassade:
|
|
| 22 |
doc_response = save_doc(creds, doc_name, content)
|
| 23 |
|
| 24 |
move_doc(creds, doc_response["documentId"], folder_id)
|
|
|
|
|
|
|
|
|
| 8 |
self.creds = None
|
| 9 |
|
| 10 |
def upload_to_drive(creds, content, FOLDER_NAME=FOLDER_NAME):
|
|
|
|
|
|
|
| 11 |
files = search_folder(creds, FOLDER_NAME)
|
| 12 |
|
| 13 |
if not files:
|
|
|
|
| 20 |
doc_response = save_doc(creds, doc_name, content)
|
| 21 |
|
| 22 |
move_doc(creds, doc_response["documentId"], folder_id)
|
| 23 |
+
|
| 24 |
+
return {"status": "200"}
|
gpt_summary.py
CHANGED
|
@@ -7,6 +7,8 @@ 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 |
|
| 12 |
load_dotenv()
|
|
@@ -46,31 +48,54 @@ def transcribe(audio_file):
|
|
| 46 |
return transcription
|
| 47 |
|
| 48 |
|
| 49 |
-
def predict(input, request: gr.Request, history=[]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
compress(input)
|
| 51 |
print("whisper starts")
|
| 52 |
-
transcription = transcribe
|
| 53 |
print("whisper ends")
|
| 54 |
print("gpt starts")
|
| 55 |
-
answer = chat
|
| 56 |
print("gpt ends")
|
| 57 |
|
|
|
|
| 58 |
# upload the input/answer to google drive
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
Fassade.upload_to_drive(creds, doc_content)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
history.append((transcription, answer))
|
| 76 |
response = history
|
|
|
|
| 7 |
from google.oauth2.credentials import Credentials
|
| 8 |
from description import DESCRIPTION
|
| 9 |
import gradio as gr
|
| 10 |
+
from utils import credentials_to_dict
|
| 11 |
+
import asyncio
|
| 12 |
|
| 13 |
|
| 14 |
load_dotenv()
|
|
|
|
| 48 |
return transcription
|
| 49 |
|
| 50 |
|
| 51 |
+
# def predict(input, request: gr.Request, history=[]):
|
| 52 |
+
# compress(input)
|
| 53 |
+
# print("whisper starts")
|
| 54 |
+
# transcription = transcribe(input)
|
| 55 |
+
# print("whisper ends")
|
| 56 |
+
# print("gpt starts")
|
| 57 |
+
# answer = chat(transcription)
|
| 58 |
+
# print("gpt ends")
|
| 59 |
+
|
| 60 |
+
# # upload the input/answer to google drive
|
| 61 |
+
# session_dict = vars(request.session)
|
| 62 |
+
# if "credentials" in session_dict:
|
| 63 |
+
# creds = Credentials(**vars(session_dict["credentials"]))
|
| 64 |
+
|
| 65 |
+
# doc_content = "user:\n" f"{transcription}\n" "\n" "summary:\n" f"{answer}\n"
|
| 66 |
+
# Fassade.upload_to_drive(creds, doc_content)
|
| 67 |
+
# # request.session["credentials"] = credentials_to_dict(creds)
|
| 68 |
+
# setattr(request.session, "credentials", credentials_to_dict(creds))
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
async def predict(input, request: gr.Request, history=[]):
|
| 72 |
compress(input)
|
| 73 |
print("whisper starts")
|
| 74 |
+
transcription = await asyncio.to_thread(transcribe, input)
|
| 75 |
print("whisper ends")
|
| 76 |
print("gpt starts")
|
| 77 |
+
answer = await asyncio.to_thread(chat, transcription)
|
| 78 |
print("gpt ends")
|
| 79 |
|
| 80 |
+
loop = asyncio.get_event_loop()
|
| 81 |
# upload the input/answer to google drive
|
| 82 |
+
session_dict = vars(request.session)
|
| 83 |
+
if "credentials" in session_dict:
|
| 84 |
+
creds = Credentials(**vars(session_dict["credentials"]))
|
| 85 |
+
|
| 86 |
+
doc_content = "user:\n" f"{transcription}\n" "\n" "summary:\n" f"{answer}\n"
|
| 87 |
+
# await asyncio.to_thread(Fassade.upload_to_drive, creds, doc_content)
|
| 88 |
+
loop.run_in_executor(None, Fassade.upload_to_drive, creds, doc_content)
|
| 89 |
+
setattr(request.session, "credentials", credentials_to_dict(creds))
|
| 90 |
+
|
| 91 |
+
# session_data = request.session.get("credentials", None)
|
| 92 |
+
# if session_data:
|
| 93 |
+
# creds = Credentials(**vars(session_data["credentials"]))
|
| 94 |
+
|
| 95 |
+
# doc_content = "user:\n" f"{transcription}\n" "\n" "summary:\n" f"{answer}\n"
|
| 96 |
+
# Fassade.upload_to_drive(creds, doc_content)
|
| 97 |
+
# session_data.update(credentials_to_dict(creds))
|
| 98 |
+
# request.session["credentials"] = session_data
|
| 99 |
|
| 100 |
history.append((transcription, answer))
|
| 101 |
response = history
|
main.py
CHANGED
|
@@ -2,15 +2,16 @@ 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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
from app import app
|
| 4 |
|
|
|
|
| 5 |
os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "1"
|
| 6 |
|
| 7 |
app = app
|
| 8 |
|
| 9 |
# use this for local development
|
| 10 |
+
if __name__ == "__main__":
|
| 11 |
+
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
|
| 12 |
+
|
| 13 |
+
uvicorn.run(
|
| 14 |
+
f"app:app",
|
| 15 |
+
port=8000,
|
| 16 |
+
reload=True,
|
| 17 |
+
)
|
utils.py
CHANGED
|
@@ -6,3 +6,15 @@ 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")
|
| 8 |
return 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")
|
| 8 |
return audio_file
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Define a helper function to convert credentials to dictionary
|
| 12 |
+
def credentials_to_dict(credentials):
|
| 13 |
+
return {
|
| 14 |
+
"token": credentials.token,
|
| 15 |
+
"refresh_token": credentials.refresh_token,
|
| 16 |
+
"token_uri": credentials.token_uri,
|
| 17 |
+
"client_id": credentials.client_id,
|
| 18 |
+
"client_secret": credentials.client_secret,
|
| 19 |
+
"scopes": credentials.scopes,
|
| 20 |
+
}
|