Spaces:
Sleeping
Sleeping
Commit
·
074d6fc
1
Parent(s):
af3f1e4
Fix bug: Prevent multiple user creation with the same email
Browse files- app.py +20 -11
- functions.py +26 -15
app.py
CHANGED
|
@@ -36,16 +36,23 @@ async def sign_up(email, username, password):
|
|
| 36 |
res, _ = supabase.auth.sign_up(
|
| 37 |
{"email": email, "password": password, "role": "user"}
|
| 38 |
)
|
| 39 |
-
user_id = res[1].id
|
| 40 |
-
r_ = createUser(user_id=user_id, username=username)
|
| 41 |
-
print(r_)
|
| 42 |
-
|
| 43 |
-
response = {
|
| 44 |
-
"status": "success",
|
| 45 |
-
"code": 200,
|
| 46 |
-
"message": "Please check you email address for email verification",
|
| 47 |
-
}
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return response
|
| 50 |
|
| 51 |
|
|
@@ -148,6 +155,8 @@ async def login_with_token(access_token: str, refresh_token: str):
|
|
| 148 |
user_id_oauth = decoded_token.get("sub")
|
| 149 |
try:
|
| 150 |
user_id = supabase.table("ConversAI_UserInfo").select("*").filter("user_id", "eq", user_id_oauth).execute()
|
|
|
|
|
|
|
| 151 |
user_name = user_id.data[0]["username"]
|
| 152 |
|
| 153 |
except:
|
|
@@ -171,8 +180,8 @@ async def login_with_token(access_token: str, refresh_token: str):
|
|
| 171 |
|
| 172 |
|
| 173 |
@app.post("/user_name")
|
| 174 |
-
async def user_name_(username: str, user_id: str):
|
| 175 |
-
r_ = createUser(user_id=user_id, username=username)
|
| 176 |
return r_
|
| 177 |
|
| 178 |
|
|
|
|
| 36 |
res, _ = supabase.auth.sign_up(
|
| 37 |
{"email": email, "password": password, "role": "user"}
|
| 38 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
user_id = res[1].id
|
| 41 |
+
r_ = createUser(user_id=user_id, username=username, email=email)
|
| 42 |
+
if r_.get('code') == 409:
|
| 43 |
+
return r_
|
| 44 |
+
elif r_.get('code') == 200:
|
| 45 |
+
response = {
|
| 46 |
+
"status": "success",
|
| 47 |
+
"code": 200,
|
| 48 |
+
"message": "Please check you email address for email verification",
|
| 49 |
+
}
|
| 50 |
+
else:
|
| 51 |
+
response = {
|
| 52 |
+
"status": "failed",
|
| 53 |
+
"code": 400,
|
| 54 |
+
"message": "Failed to sign up please try again later",
|
| 55 |
+
}
|
| 56 |
return response
|
| 57 |
|
| 58 |
|
|
|
|
| 155 |
user_id_oauth = decoded_token.get("sub")
|
| 156 |
try:
|
| 157 |
user_id = supabase.table("ConversAI_UserInfo").select("*").filter("user_id", "eq", user_id_oauth).execute()
|
| 158 |
+
user_id = supabase.table("ConversAI_UserInfo").select("*").filter("email", "eq", user_id_oauth).execute()
|
| 159 |
+
|
| 160 |
user_name = user_id.data[0]["username"]
|
| 161 |
|
| 162 |
except:
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
@app.post("/user_name")
|
| 183 |
+
async def user_name_(username: str, user_id: str, email: str):
|
| 184 |
+
r_ = createUser(user_id=user_id, username=username, email=email)
|
| 185 |
return r_
|
| 186 |
|
| 187 |
|
functions.py
CHANGED
|
@@ -73,23 +73,32 @@ prompt = ChatPromptTemplate.from_template(prompt)
|
|
| 73 |
chatHistoryStore = dict()
|
| 74 |
|
| 75 |
|
| 76 |
-
def createUser(user_id: str, username: str) -> dict:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
client.table("ConversAI_UserInfo").insert(
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
"
|
|
|
|
| 85 |
}
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
return {
|
| 92 |
-
"
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
|
|
@@ -182,10 +191,11 @@ def answerQuery(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192")
|
|
| 182 |
api_key=os.environ["QDRANT_API_KEY"],
|
| 183 |
retrieval_mode=RetrievalMode.HYBRID
|
| 184 |
)
|
| 185 |
-
retriever = vectorstore.as_retriever(search_type
|
| 186 |
baseChain = (
|
| 187 |
{"context": RunnableLambda(lambda x: x["question"]) | retriever | RunnableLambda(format_docs),
|
| 188 |
-
"question": RunnableLambda(lambda x: x["question"]),
|
|
|
|
| 189 |
| prompt
|
| 190 |
| ChatGroq(model=llmModel, temperature=0.75, max_tokens=512)
|
| 191 |
| StrOutputParser()
|
|
@@ -269,6 +279,7 @@ def getTextFromImagePDF(pdfBytes):
|
|
| 269 |
def getText(image):
|
| 270 |
global reader
|
| 271 |
return "\n".join([text[1] for text in reader.readtext(np.array(image), paragraph=True)])
|
|
|
|
| 272 |
allImages = convert_from_bytes(pdfBytes)
|
| 273 |
texts = [getText(image) for image in allImages]
|
| 274 |
return "\n\n\n".join(texts)
|
|
|
|
| 73 |
chatHistoryStore = dict()
|
| 74 |
|
| 75 |
|
| 76 |
+
def createUser(user_id: str, username: str, email: str) -> dict:
|
| 77 |
+
userData = client.table("ConversAI_UserInfo").select("*").execute().data
|
| 78 |
+
if user_id not in [userData[x]["user_id"] for x in range(len(userData))]:
|
| 79 |
+
try:
|
| 80 |
+
client.table("ConversAI_UserInfo").insert(
|
| 81 |
+
{"user_id": user_id, "username": username, "email": email}).execute()
|
| 82 |
+
|
| 83 |
+
client.table("ConversAI_UserConfig").insert({"user_id": user_id}).execute()
|
| 84 |
|
| 85 |
+
res = {
|
| 86 |
+
"code": 200,
|
| 87 |
+
"message": "User Setup Successful"
|
| 88 |
}
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
res = {
|
| 92 |
+
"code": 409,
|
| 93 |
+
"message": "Email already exists",
|
| 94 |
}
|
| 95 |
+
|
| 96 |
+
return res
|
| 97 |
+
|
| 98 |
+
else:
|
| 99 |
return {
|
| 100 |
+
"code": 409,
|
| 101 |
+
"message": "User already exists"
|
| 102 |
}
|
| 103 |
|
| 104 |
|
|
|
|
| 191 |
api_key=os.environ["QDRANT_API_KEY"],
|
| 192 |
retrieval_mode=RetrievalMode.HYBRID
|
| 193 |
)
|
| 194 |
+
retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 4, "score_threshold": None})
|
| 195 |
baseChain = (
|
| 196 |
{"context": RunnableLambda(lambda x: x["question"]) | retriever | RunnableLambda(format_docs),
|
| 197 |
+
"question": RunnableLambda(lambda x: x["question"]),
|
| 198 |
+
"chatHistory": RunnableLambda(lambda x: x["chatHistory"])}
|
| 199 |
| prompt
|
| 200 |
| ChatGroq(model=llmModel, temperature=0.75, max_tokens=512)
|
| 201 |
| StrOutputParser()
|
|
|
|
| 279 |
def getText(image):
|
| 280 |
global reader
|
| 281 |
return "\n".join([text[1] for text in reader.readtext(np.array(image), paragraph=True)])
|
| 282 |
+
|
| 283 |
allImages = convert_from_bytes(pdfBytes)
|
| 284 |
texts = [getText(image) for image in allImages]
|
| 285 |
return "\n\n\n".join(texts)
|