File size: 2,875 Bytes
b0abe84
2f9dcad
7fc3da4
027a338
 
2f9dcad
027a338
 
 
 
91f7830
60dc67b
2f9dcad
027a338
2f9dcad
 
027a338
2f9dcad
 
027a338
2ef4f3c
 
027a338
b0abe84
2f9dcad
 
7fc3da4
91f7830
b0abe84
91f7830
2f9dcad
 
2ef4f3c
60dc67b
2f9dcad
027a338
91f7830
027a338
 
60dc67b
91f7830
 
eafe297
91f7830
eafe297
 
 
 
 
 
 
91f7830
027a338
 
 
 
 
 
 
 
2f9dcad
027a338
 
 
 
 
 
91f7830
eafe297
91f7830
eafe297
 
 
 
 
 
 
3cd8a78
027a338
 
 
b0abe84
 
2f9dcad
60dc67b
 
2f9dcad
027a338
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse, HTMLResponse
from google.oauth2.credentials import Credentials
from fastapi.staticfiles import StaticFiles
from google_manager.constants import SCOPES
from pages.load_page import load_page
import google_auth_oauthlib.flow
from starlette.middleware.sessions import SessionMiddleware
import gradio as gr
from gpt_summary import Ui
from utils import credentials_to_dict

# Create an instance of the FastAPI app
app = FastAPI()

# Add Session middleware to the app
app.add_middleware(SessionMiddleware, secret_key="mysecret")

# Mount the static files directory
app.mount("/assets", StaticFiles(directory="assets"), name="assets")


@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
    # Load the html content of the auth page
    html_content = load_page("./pages/auth_page.html")

    if "credentials" not in request.session:
        return HTMLResponse(content=html_content, status_code=200)

    else:
        return RedirectResponse("/gradio/?__theme=dark")


# Define the endpoint for Google authentication
@app.get("/auth")
async def authorize(request: Request):
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        "credentials.json", scopes=SCOPES
    )

    # for dev
    # flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
    # for production
    flow.redirect_uri = (
        request.url.scheme
        + "://"
        + request.url.hostname
        + (":" + str(request.url.port) if request.url.port else "")
        + app.url_path_for("auth_callback")
    )

    authorization_url, state = flow.authorization_url(
        access_type="offline",
        include_granted_scopes="true",
    )
    request.session["state"] = state
    return RedirectResponse(url=authorization_url)


# Define the callback endpoint after successful authentication
@app.get("/auth_callback")
async def auth_callback(request: Request):
    state = request.session["state"]
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        "credentials.json", scopes=SCOPES, state=state
    )
    # for dev
    # flow.redirect_uri = "http://127.0.0.1:8000/auth_callback"
    # for production
    flow.redirect_uri = (
        request.url.scheme
        + "://"
        + request.url.hostname
        + (":" + str(request.url.port) if request.url.port else "")
        + app.url_path_for("auth_callback")
    )

    # Use the authorization server's response to fetch the OAuth 2.0 tokens.
    authorization_response = str(request.url)
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    request.session["credentials"] = credentials_to_dict(credentials)
    return RedirectResponse("/gradio?__theme=dark")


# Mount the Gradio UI
app = gr.mount_gradio_app(app, Ui, path="/gradio")