Spaces:
Runtime error
Runtime error
pushing for test
Browse files- app.py +80 -60
- assets/google_logo.svg +44 -0
- credentials.json +1 -1
- google_manager/auth.py +52 -0
- google_manager/fassade.py +6 -2
- gpt_summary.py +82 -0
- main.py +6 -0
- pages/auth_page.py +54 -0
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,77 +1,97 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
from
|
4 |
-
import
|
5 |
-
from
|
6 |
-
from google_manager.
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# configuring openai package
|
13 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
-
openai.api_key = OPENAI_API_KEY
|
15 |
|
|
|
|
|
16 |
|
17 |
-
def load_prompt(path):
|
18 |
-
with open(path) as f:
|
19 |
-
lines = f.readlines()
|
20 |
-
return "".join(lines)
|
21 |
|
|
|
|
|
|
|
22 |
|
23 |
-
def chat(passage, max_tokens=256, temprature=0, debug=False):
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"""
|
29 |
|
30 |
-
prompt = load_prompt("summary_prompt.txt").replace("<<SUMMARY>>", passage)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
user:
|
55 |
-
{transcription}
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
Fassade.upload_to_drive(doc_content)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
|
|
|
66 |
|
67 |
-
with gr.Blocks() as demo:
|
68 |
-
gr.Markdown(DESCRIPTION)
|
69 |
-
chatbot = gr.Chatbot()
|
70 |
-
state = gr.State([])
|
71 |
|
72 |
-
|
73 |
-
audio_file = gr.Audio(label="Audio", source="microphone", type="filepath")
|
74 |
|
75 |
-
audio_file.change(predict, [audio_file, state], [chatbot, state])
|
76 |
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, Depends
|
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 google_manager.auth import get_oauth_url
|
7 |
+
from pages.auth_page import html_content
|
8 |
+
import google_auth_oauthlib.flow
|
9 |
+
|
10 |
+
# from fastapi_sessions import SessionMiddleware, get_session
|
11 |
+
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
|
|
|
12 |
|
13 |
+
import gradio as gr
|
14 |
+
from gpt_summary import Ui
|
15 |
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
app = FastAPI()
|
18 |
+
app.add_middleware(SessionMiddleware, secret_key="mysecret")
|
19 |
+
app.mount("/assets", StaticFiles(directory="assets"), name="assets")
|
20 |
|
|
|
21 |
|
22 |
+
@app.get("/", response_class=HTMLResponse)
|
23 |
+
async def home():
|
24 |
+
return HTMLResponse(content=html_content, status_code=200)
|
|
|
25 |
|
|
|
26 |
|
27 |
+
@app.get("/auth")
|
28 |
+
async def integreate_google(request: Request):
|
29 |
+
# url = get_oauth_url(SCOPES, "http://127.0.0.1:8000/auth_callback")
|
30 |
+
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
31 |
+
"credentials.json", scopes=SCOPES
|
32 |
)
|
33 |
|
34 |
+
# flow.redirect_uri = "http://127.0.0.1:8000/gradio/?__theme=dark"
|
35 |
+
# flow.redirect_uri = app.url_path_for(redirect_url)
|
36 |
+
flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
|
37 |
+
|
38 |
+
# Generate URL for request to Google's OAuth 2.0 server.
|
39 |
+
# Use kwargs to set optional request parameters.
|
40 |
+
authorization_url, state = flow.authorization_url(
|
41 |
+
# Enable offline access so that you can refresh an access token without
|
42 |
+
# re-prompting the user for permission. Recommended for web server apps.
|
43 |
+
access_type="offline",
|
44 |
+
# Enable incremental authorization. Recommended as a best practice.
|
45 |
+
include_granted_scopes="true",
|
46 |
+
)
|
47 |
+
request.session["state"] = state
|
48 |
+
print("authorization_url")
|
49 |
+
print(authorization_url)
|
50 |
+
return RedirectResponse(url=authorization_url)
|
51 |
+
|
52 |
+
|
53 |
+
@app.get("/auth_callback")
|
54 |
+
async def auth_callback(request: Request):
|
55 |
+
# url = get_oauth_url(SCOPES)
|
56 |
+
print("request")
|
57 |
+
# print(request["code"])
|
58 |
+
# return {"callback": "here"}
|
59 |
+
|
60 |
+
# Specify the state when creating the flow in the callback so that it can
|
61 |
+
# verified in the authorization server response.
|
62 |
+
# if False:
|
63 |
+
state = request.session["state"]
|
64 |
+
|
65 |
+
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
66 |
+
"credentials.json", scopes=SCOPES, state=state
|
67 |
+
)
|
68 |
+
flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
|
69 |
|
70 |
+
# Use the authorization server's response to fetch the OAuth 2.0 tokens.
|
71 |
+
authorization_response = str(request.url)
|
|
|
|
|
72 |
|
73 |
+
flow.fetch_token(authorization_response=authorization_response)
|
74 |
+
if False:
|
75 |
+
# flow.fetch_token(authorization_response=authorization_response)
|
|
|
76 |
|
77 |
+
# Store credentials in the session.
|
78 |
+
# ACTION ITEM: In a production app, you likely want to save these
|
79 |
+
# credentials in a persistent database instead.
|
80 |
+
credentials = flow.credentials
|
81 |
+
request.session["credentials"] = credentials_to_dict(credentials)
|
82 |
|
83 |
+
return RedirectResponse("/gradio")
|
84 |
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
app = gr.mount_gradio_app(app, Ui, path="/gradio")
|
|
|
87 |
|
|
|
88 |
|
89 |
+
def credentials_to_dict(credentials):
|
90 |
+
return {
|
91 |
+
"token": credentials.token,
|
92 |
+
"refresh_token": credentials.refresh_token,
|
93 |
+
"token_uri": credentials.token_uri,
|
94 |
+
"client_id": credentials.client_id,
|
95 |
+
"client_secret": credentials.client_secret,
|
96 |
+
"scopes": credentials.scopes,
|
97 |
+
}
|
assets/google_logo.svg
ADDED
|
credentials.json
CHANGED
@@ -1 +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://
|
|
|
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":["https://zinoubm-clienttest.hf.space/?__theme=dark","http://127.0.0.1:8000/gradio/?__theme=dark","http://127.0.0.1:8000/auth_callback"],"javascript_origins":["https://zinoubm-clienttest.hf.space","http://127.0.0.1:8000"]}}
|
google_manager/auth.py
CHANGED
@@ -2,6 +2,13 @@ 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):
|
@@ -26,3 +33,48 @@ def authenticate(SCOPES):
|
|
26 |
token.write(creds.to_json())
|
27 |
|
28 |
return creds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
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
|
google_manager/fassade.py
CHANGED
@@ -1,13 +1,17 @@
|
|
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 |
-
|
|
|
11 |
files = search_folder(creds, FOLDER_NAME)
|
12 |
|
13 |
if not files:
|
|
|
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:
|
gpt_summary.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
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 |
+
|
9 |
+
from description import DESCRIPTION
|
10 |
+
|
11 |
+
# fastApi
|
12 |
+
|
13 |
+
import fastapi
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
|
17 |
+
load_dotenv()
|
18 |
+
|
19 |
+
# configuring openai package
|
20 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
21 |
+
openai.api_key = OPENAI_API_KEY
|
22 |
+
|
23 |
+
|
24 |
+
def load_prompt(path):
|
25 |
+
with open(path) as f:
|
26 |
+
lines = f.readlines()
|
27 |
+
return "".join(lines)
|
28 |
+
|
29 |
+
|
30 |
+
def chat(passage, max_tokens=256, temprature=0, debug=False):
|
31 |
+
|
32 |
+
if debug:
|
33 |
+
passage = """
|
34 |
+
A car or automobile is a motor vehicle with wheels. Most definitions of cars say that they run primarily on roads, seat one to eight people, have four wheels, and mainly transport people (rather than goods).
|
35 |
+
"""
|
36 |
+
|
37 |
+
prompt = load_prompt("summary_prompt.txt").replace("<<SUMMARY>>", passage)
|
38 |
+
|
39 |
+
summary = openai.ChatCompletion.create(
|
40 |
+
model="gpt-3.5-turbo",
|
41 |
+
messages=[{"role": "user", "content": prompt}],
|
42 |
+
)
|
43 |
+
|
44 |
+
return summary["choices"][0]["message"]["content"].strip()
|
45 |
+
|
46 |
+
|
47 |
+
def transcribe(audio_file):
|
48 |
+
audio_file = open(audio_file, "rb")
|
49 |
+
transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
|
50 |
+
transcription = transcription["text"]
|
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
|
71 |
+
return response, history
|
72 |
+
|
73 |
+
|
74 |
+
with gr.Blocks() as Ui:
|
75 |
+
gr.Markdown(DESCRIPTION)
|
76 |
+
chatbot = gr.Chatbot()
|
77 |
+
state = gr.State([])
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
audio_file = gr.Audio(label="Audio", source="microphone", type="filepath")
|
81 |
+
|
82 |
+
audio_file.change(predict, [audio_file, state], [chatbot, state])
|
main.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uvicorn
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
|
5 |
+
if __name__ == "__main__":
|
6 |
+
uvicorn.run(f"app:app", port=8000, reload=True)
|
pages/auth_page.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""
|
requirements.txt
CHANGED
@@ -6,3 +6,4 @@ openai==0.27.0
|
|
6 |
protobuf==4.22.0
|
7 |
python-dotenv==1.0.0
|
8 |
soundfile==0.12.1
|
|
|
|
6 |
protobuf==4.22.0
|
7 |
python-dotenv==1.0.0
|
8 |
soundfile==0.12.1
|
9 |
+
fastapi-sessions
|