Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://huggingface.co/spaces/techconspartners/ConversAI
Browse files- app.py +50 -43
- functions.py +2 -19
app.py
CHANGED
|
@@ -21,17 +21,17 @@ app.add_middleware(
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
app.include_router(speech_translator_router, prefix="/speech")
|
| 26 |
|
| 27 |
|
| 28 |
@app.post("/signup")
|
| 29 |
-
async def sign_up(email, password):
|
| 30 |
res, _ = supabase.auth.sign_up(
|
| 31 |
{"email": email, "password": password, "role": "user"}
|
| 32 |
)
|
| 33 |
user_id = res[1].id
|
| 34 |
-
r_ = createUser(username=
|
|
|
|
| 35 |
response = {
|
| 36 |
"status": "success",
|
| 37 |
"code": 200,
|
|
@@ -47,6 +47,7 @@ async def check_session():
|
|
| 47 |
|
| 48 |
return res
|
| 49 |
|
|
|
|
| 50 |
@app.post("/login")
|
| 51 |
async def sign_in(email, password):
|
| 52 |
try:
|
|
@@ -56,54 +57,53 @@ async def sign_in(email, password):
|
|
| 56 |
user_id = res.user.id
|
| 57 |
access_token = res.session.access_token
|
| 58 |
refresh_token = res.session.refresh_token
|
|
|
|
| 59 |
store_session_check = supabase.table("Stores").select("*").filter("StoreID", "eq", user_id).execute()
|
| 60 |
-
|
| 61 |
-
store_id = store_session_check[1][0]["StoreID"]
|
| 62 |
-
except:
|
| 63 |
-
store_id = None
|
| 64 |
|
| 65 |
-
if
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
supabase.table("Stores").insert(
|
| 69 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
message = {
|
| 72 |
-
"message": "
|
| 73 |
-
"code":
|
| 74 |
-
"
|
| 75 |
"access_token": access_token,
|
| 76 |
-
"refresh_token": refresh_token
|
| 77 |
-
|
| 78 |
return message
|
| 79 |
|
| 80 |
-
|
| 81 |
elif store_id == user_id:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"
|
| 85 |
-
|
| 86 |
-
}
|
| 87 |
-
return message
|
| 88 |
|
| 89 |
else:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
except:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
"
|
| 102 |
-
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
return message
|
| 106 |
-
|
| 107 |
|
| 108 |
|
| 109 |
@app.post("/set-session-data")
|
|
@@ -114,10 +114,17 @@ async def set_session_data(access_token, refresh_token):
|
|
| 114 |
|
| 115 |
|
| 116 |
@app.post("/logout")
|
| 117 |
-
async def sign_out():
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
@app.post("/oauth")
|
|
@@ -137,7 +144,7 @@ async def newChatbot(chatbotName: str, username: str):
|
|
| 137 |
"output": "CHATBOT LIMIT EXCEEDED"
|
| 138 |
}
|
| 139 |
client.table("ConversAI_ChatbotInfo").insert({"user_id": username, "chatbotname": chatbotName}).execute()
|
| 140 |
-
chatbotName = f"convai
|
| 141 |
return createTable(tablename=chatbotName)
|
| 142 |
|
| 143 |
|
|
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
app.include_router(speech_translator_router, prefix="/speech")
|
| 25 |
|
| 26 |
|
| 27 |
@app.post("/signup")
|
| 28 |
+
async def sign_up(email, username, password):
|
| 29 |
res, _ = supabase.auth.sign_up(
|
| 30 |
{"email": email, "password": password, "role": "user"}
|
| 31 |
)
|
| 32 |
user_id = res[1].id
|
| 33 |
+
r_ = createUser(user_id=user_id, username=username)
|
| 34 |
+
|
| 35 |
response = {
|
| 36 |
"status": "success",
|
| 37 |
"code": 200,
|
|
|
|
| 47 |
|
| 48 |
return res
|
| 49 |
|
| 50 |
+
|
| 51 |
@app.post("/login")
|
| 52 |
async def sign_in(email, password):
|
| 53 |
try:
|
|
|
|
| 57 |
user_id = res.user.id
|
| 58 |
access_token = res.session.access_token
|
| 59 |
refresh_token = res.session.refresh_token
|
| 60 |
+
|
| 61 |
store_session_check = supabase.table("Stores").select("*").filter("StoreID", "eq", user_id).execute()
|
| 62 |
+
store_id = None
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
if store_session_check and store_session_check.data:
|
| 65 |
+
store_id = store_session_check.data[0].get("StoreID")
|
| 66 |
|
| 67 |
+
if not store_id:
|
| 68 |
+
response = (
|
| 69 |
supabase.table("Stores").insert(
|
| 70 |
+
{
|
| 71 |
+
"AccessToken": access_token,
|
| 72 |
+
"StoreID": user_id,
|
| 73 |
+
"RefreshToken": refresh_token,
|
| 74 |
+
}
|
| 75 |
+
).execute()
|
| 76 |
+
)
|
| 77 |
|
| 78 |
message = {
|
| 79 |
+
"message": "Success",
|
| 80 |
+
"code": status.HTTP_200_OK,
|
| 81 |
+
"user_id": user_id,
|
| 82 |
"access_token": access_token,
|
| 83 |
+
"refresh_token": refresh_token
|
| 84 |
+
}
|
| 85 |
return message
|
| 86 |
|
|
|
|
| 87 |
elif store_id == user_id:
|
| 88 |
+
raise HTTPException(
|
| 89 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 90 |
+
detail="You are already signed in. Please sign out first to sign in again."
|
| 91 |
+
)
|
|
|
|
|
|
|
| 92 |
|
| 93 |
else:
|
| 94 |
+
raise HTTPException(
|
| 95 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 96 |
+
detail="Failed to sign in. Please check your credentials."
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
except HTTPException as http_exc:
|
| 100 |
+
raise http_exc
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
raise HTTPException(
|
| 104 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 105 |
+
detail=f"An unexpected error occurred during sign-in: {str(e)}"
|
| 106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
@app.post("/set-session-data")
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
@app.post("/logout")
|
| 117 |
+
async def sign_out(user_id):
|
| 118 |
+
try:
|
| 119 |
+
supabase.table("Stores").delete().eq(
|
| 120 |
+
"StoreID", user_id
|
| 121 |
+
).execute()
|
| 122 |
+
res = supabase.auth.sign_out()
|
| 123 |
+
response = {"message": "success"}
|
| 124 |
|
| 125 |
+
return response
|
| 126 |
+
except Exception as e:
|
| 127 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 128 |
|
| 129 |
|
| 130 |
@app.post("/oauth")
|
|
|
|
| 144 |
"output": "CHATBOT LIMIT EXCEEDED"
|
| 145 |
}
|
| 146 |
client.table("ConversAI_ChatbotInfo").insert({"user_id": username, "chatbotname": chatbotName}).execute()
|
| 147 |
+
chatbotName = f"convai${username}${chatbotName}"
|
| 148 |
return createTable(tablename=chatbotName)
|
| 149 |
|
| 150 |
|
functions.py
CHANGED
|
@@ -80,11 +80,11 @@ store = InMemoryStore()
|
|
| 80 |
chatHistoryStore = dict()
|
| 81 |
|
| 82 |
|
| 83 |
-
def createUser(username: str) -> dict:
|
| 84 |
try:
|
| 85 |
userData = client.table("ConversAI_UserInfo").select("*").execute().data
|
| 86 |
if username not in [userData[x]["user_id"] for x in range(len(userData))]:
|
| 87 |
-
client.table("ConversAI_UserInfo").insert({"user_id":
|
| 88 |
client.table("ConversAI_UserConfig").insert({"user_id": username}).execute()
|
| 89 |
|
| 90 |
return {
|
|
@@ -100,23 +100,6 @@ def createUser(username: str) -> dict:
|
|
| 100 |
}
|
| 101 |
|
| 102 |
|
| 103 |
-
# def matchPassword(username: str, password: str) -> str:
|
| 104 |
-
# response = (
|
| 105 |
-
# client.table("ConversAI_UserInfo")
|
| 106 |
-
# .select("*")
|
| 107 |
-
# .eq("user_id", username)
|
| 108 |
-
# .execute()
|
| 109 |
-
# )
|
| 110 |
-
# try:
|
| 111 |
-
# return {
|
| 112 |
-
# "output": password == response.data[0]["password"]
|
| 113 |
-
# }
|
| 114 |
-
# except:
|
| 115 |
-
# return {
|
| 116 |
-
# "output": "USER DOESN'T EXIST"
|
| 117 |
-
# }
|
| 118 |
-
|
| 119 |
-
|
| 120 |
def createTable(tablename: str):
|
| 121 |
global vectorEmbeddings
|
| 122 |
global sparseEmbeddings
|
|
|
|
| 80 |
chatHistoryStore = dict()
|
| 81 |
|
| 82 |
|
| 83 |
+
def createUser(user_id: str, username: str) -> dict:
|
| 84 |
try:
|
| 85 |
userData = client.table("ConversAI_UserInfo").select("*").execute().data
|
| 86 |
if username not in [userData[x]["user_id"] for x in range(len(userData))]:
|
| 87 |
+
client.table("ConversAI_UserInfo").insert({"user_id": user_id, "username": username}).execute()
|
| 88 |
client.table("ConversAI_UserConfig").insert({"user_id": username}).execute()
|
| 89 |
|
| 90 |
return {
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def createTable(tablename: str):
|
| 104 |
global vectorEmbeddings
|
| 105 |
global sparseEmbeddings
|