Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import io
|
| 2 |
-
import
|
| 3 |
-
import tempfile
|
| 4 |
from functions import *
|
| 5 |
-
from
|
| 6 |
import pandas as pd
|
| 7 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 8 |
from pydantic import BaseModel
|
|
@@ -13,6 +12,7 @@ from functions import client as supabase
|
|
| 13 |
from urllib.parse import urlparse
|
| 14 |
import nltk
|
| 15 |
|
|
|
|
| 16 |
nltk.download('punkt_tab')
|
| 17 |
|
| 18 |
app = FastAPI(title="ConversAI", root_path="/api/v1")
|
|
@@ -35,6 +35,7 @@ async def sign_up(email, username, password):
|
|
| 35 |
)
|
| 36 |
user_id = res[1].id
|
| 37 |
r_ = createUser(user_id=user_id, username=username)
|
|
|
|
| 38 |
|
| 39 |
response = {
|
| 40 |
"status": "success",
|
|
@@ -51,6 +52,18 @@ async def check_session():
|
|
| 51 |
|
| 52 |
return res
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
@app.post("/login")
|
| 56 |
async def sign_in(email, password):
|
|
@@ -110,6 +123,64 @@ async def sign_in(email, password):
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
@app.post("/set-session-data")
|
| 114 |
async def set_session_data(access_token, refresh_token):
|
| 115 |
res = supabase.auth.set_session(access_token, refresh_token)
|
|
|
|
| 1 |
import io
|
| 2 |
+
from starlette import status
|
|
|
|
| 3 |
from functions import *
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
import pandas as pd
|
| 6 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 7 |
from pydantic import BaseModel
|
|
|
|
| 12 |
from urllib.parse import urlparse
|
| 13 |
import nltk
|
| 14 |
|
| 15 |
+
|
| 16 |
nltk.download('punkt_tab')
|
| 17 |
|
| 18 |
app = FastAPI(title="ConversAI", root_path="/api/v1")
|
|
|
|
| 35 |
)
|
| 36 |
user_id = res[1].id
|
| 37 |
r_ = createUser(user_id=user_id, username=username)
|
| 38 |
+
print(r_)
|
| 39 |
|
| 40 |
response = {
|
| 41 |
"status": "success",
|
|
|
|
| 52 |
|
| 53 |
return res
|
| 54 |
|
| 55 |
+
@app.post("/get-user")
|
| 56 |
+
async def get_user(access_token):
|
| 57 |
+
res = supabase.auth.get_user(jwt=access_token)
|
| 58 |
+
return res
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@app.post("/referesh-token")
|
| 62 |
+
async def refresh_token(refresh_token):
|
| 63 |
+
res = supabase.auth.refresh_token(refresh_token)
|
| 64 |
+
return res
|
| 65 |
+
|
| 66 |
+
|
| 67 |
|
| 68 |
@app.post("/login")
|
| 69 |
async def sign_in(email, password):
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
|
| 126 |
+
@app.post('login_with_token')
|
| 127 |
+
async def login_with_token(token):
|
| 128 |
+
try:
|
| 129 |
+
res = supabase.auth.sign_in_with_id_token(token)
|
| 130 |
+
print(res)
|
| 131 |
+
user_id = res.user.id
|
| 132 |
+
access_token = res.session.access_token
|
| 133 |
+
refresh_token = res.session.refresh_token
|
| 134 |
+
|
| 135 |
+
store_session_check = supabase.table("Stores").select("*").filter("StoreID", "eq", user_id).execute()
|
| 136 |
+
store_id = None
|
| 137 |
+
|
| 138 |
+
if store_session_check and store_session_check.data:
|
| 139 |
+
store_id = store_session_check.data[0].get("StoreID")
|
| 140 |
+
|
| 141 |
+
if not store_id:
|
| 142 |
+
response = (
|
| 143 |
+
supabase.table("Stores").insert(
|
| 144 |
+
{
|
| 145 |
+
"AccessToken": access_token,
|
| 146 |
+
"StoreID": user_id,
|
| 147 |
+
"RefreshToken": refresh_token,
|
| 148 |
+
}
|
| 149 |
+
).execute()
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
message = {
|
| 153 |
+
"message": "Success",
|
| 154 |
+
"code": status.HTTP_200_OK,
|
| 155 |
+
"user_id": user_id,
|
| 156 |
+
"access_token": access_token,
|
| 157 |
+
"refresh_token": refresh_token
|
| 158 |
+
}
|
| 159 |
+
return message
|
| 160 |
+
|
| 161 |
+
elif store_id == user_id:
|
| 162 |
+
raise HTTPException(
|
| 163 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 164 |
+
detail="You are already signed in. Please sign out first to sign in again."
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
else:
|
| 168 |
+
raise HTTPException(
|
| 169 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 170 |
+
detail="Failed to sign in. Please check your credentials."
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
except HTTPException as http_exc:
|
| 174 |
+
raise http_exc
|
| 175 |
+
|
| 176 |
+
except Exception as e:
|
| 177 |
+
raise HTTPException(
|
| 178 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 179 |
+
detail=f"An unexpected error occurred during sign-in: {str(e)}"
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
@app.post("/set-session-data")
|
| 185 |
async def set_session_data(access_token, refresh_token):
|
| 186 |
res = supabase.auth.set_session(access_token, refresh_token)
|