Spaces:
Sleeping
Sleeping
UPDATE: chatHistory
Browse files
app.py
CHANGED
|
@@ -81,7 +81,7 @@ async def sign_in(email, password):
|
|
| 81 |
if store_session_check and store_session_check.data:
|
| 82 |
store_id = store_session_check.data[0].get("StoreID")
|
| 83 |
|
| 84 |
-
userData =
|
| 85 |
username = userData[0]["username"]
|
| 86 |
|
| 87 |
if not store_id:
|
|
@@ -215,13 +215,13 @@ async def oauth(provider):
|
|
| 215 |
@app.post("/newChatbot")
|
| 216 |
async def newChatbot(chatbotName: str, username: str):
|
| 217 |
currentBotCount = len(listTables(username=username)["output"])
|
| 218 |
-
limit =
|
| 219 |
"chatbotLimit"]
|
| 220 |
if currentBotCount >= int(limit):
|
| 221 |
return {
|
| 222 |
"output": "CHATBOT LIMIT EXCEEDED"
|
| 223 |
}
|
| 224 |
-
|
| 225 |
chatbotName = f"convai${username}${chatbotName}"
|
| 226 |
return createTable(tablename=chatbotName)
|
| 227 |
|
|
@@ -238,13 +238,13 @@ async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
|
|
| 238 |
textExtraction = time.time()
|
| 239 |
os.remove(temp_file_path)
|
| 240 |
username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 241 |
-
df = pd.DataFrame(
|
| 242 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 243 |
-
limit =
|
| 244 |
"tokenLimit"]
|
| 245 |
newCount = currentCount + len(text)
|
| 246 |
if newCount < int(limit):
|
| 247 |
-
|
| 248 |
"chatbotname", chatbotname).execute()
|
| 249 |
uploadStart = time.time()
|
| 250 |
output = addDocuments(text=text, source=source, vectorstore=vectorstore)
|
|
@@ -271,29 +271,46 @@ async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
|
|
| 271 |
|
| 272 |
@app.post("/scanAndReturnText")
|
| 273 |
async def returnText(pdf: UploadFile = File(...)):
|
|
|
|
| 274 |
pdf = await pdf.read()
|
| 275 |
start = time.time()
|
| 276 |
text = getTextFromImagePDF(pdfBytes=pdf)
|
| 277 |
end = time.time()
|
| 278 |
timeTaken = f"{end - start}s"
|
| 279 |
return {
|
|
|
|
| 280 |
"extractionTime": timeTaken,
|
| 281 |
"output": text
|
| 282 |
}
|
| 283 |
|
| 284 |
|
| 285 |
@app.post("/addText")
|
| 286 |
-
async def addText(vectorstore: str, text: str):
|
| 287 |
username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 288 |
-
df = pd.DataFrame(
|
| 289 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 290 |
newCount = currentCount + len(text)
|
| 291 |
-
limit =
|
| 292 |
"tokenLimit"]
|
| 293 |
if newCount < int(limit):
|
| 294 |
-
|
| 295 |
"chatbotname", chatbotname).execute()
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
else:
|
| 298 |
return {
|
| 299 |
"output": "WEBSITE EXCEEDING LIMITS, PLEASE TRY WITH A SMALLER DOCUMENT."
|
|
@@ -309,14 +326,14 @@ class AddQAPair(BaseModel):
|
|
| 309 |
@app.post("/addQAPair")
|
| 310 |
async def addText(addQaPair: AddQAPair):
|
| 311 |
username, chatbotname = addQaPair.vectorstore.split("$")[1], addQaPair.vectorstore.split("$")[2]
|
| 312 |
-
df = pd.DataFrame(
|
| 313 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 314 |
qa = f"QUESTION: {addQaPair.question}\tANSWER: {addQaPair.answer}"
|
| 315 |
newCount = currentCount + len(qa)
|
| 316 |
-
limit =
|
| 317 |
"tokenLimit"]
|
| 318 |
if newCount < int(limit):
|
| 319 |
-
|
| 320 |
"chatbotname", chatbotname).execute()
|
| 321 |
return addDocuments(text=qa, source="Q&A Pairs", vectorstore=addQaPair.vectorstore)
|
| 322 |
else:
|
|
@@ -331,12 +348,12 @@ async def addWebsite(vectorstore: str, websiteUrls: list[str]):
|
|
| 331 |
text = extractTextFromUrlList(urls = websiteUrls)
|
| 332 |
textExtraction = time.time()
|
| 333 |
username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 334 |
-
df = pd.DataFrame(
|
| 335 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 336 |
newCount = currentCount + len(text)
|
| 337 |
-
limit =
|
| 338 |
if newCount < int(limit):
|
| 339 |
-
|
| 340 |
"chatbotname", chatbotname).execute()
|
| 341 |
uploadStart = time.time()
|
| 342 |
output = addDocuments(text=text, source=urlparse(websiteUrls[0]).netloc, vectorstore=vectorstore)
|
|
@@ -364,13 +381,20 @@ async def addWebsite(vectorstore: str, websiteUrls: list[str]):
|
|
| 364 |
|
| 365 |
@app.post("/answerQuery")
|
| 366 |
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
|
| 370 |
@app.post("/deleteChatbot")
|
| 371 |
async def delete(chatbotName: str):
|
| 372 |
username, chatbotName = chatbotName.split("$")[1], chatbotName.split("$")[2]
|
| 373 |
-
|
| 374 |
return deleteTable(tableName=chatbotName)
|
| 375 |
|
| 376 |
|
|
@@ -389,7 +413,7 @@ async def crawlUrl(baseUrl: str):
|
|
| 389 |
@app.post("/getCurrentCount")
|
| 390 |
async def getCount(vectorstore: str):
|
| 391 |
username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 392 |
-
df = pd.DataFrame(
|
| 393 |
return {
|
| 394 |
"currentCount": df[(df['user_id'] == username) & (df['chatbotname'] == chatbotName)]['charactercount'].iloc[0]
|
| 395 |
}
|
|
|
|
| 81 |
if store_session_check and store_session_check.data:
|
| 82 |
store_id = store_session_check.data[0].get("StoreID")
|
| 83 |
|
| 84 |
+
userData = supabase.table("ConversAI_UserInfo").select("*").filter("user_id", "eq", user_id).execute().data
|
| 85 |
username = userData[0]["username"]
|
| 86 |
|
| 87 |
if not store_id:
|
|
|
|
| 215 |
@app.post("/newChatbot")
|
| 216 |
async def newChatbot(chatbotName: str, username: str):
|
| 217 |
currentBotCount = len(listTables(username=username)["output"])
|
| 218 |
+
limit = supabase.table("ConversAI_UserConfig").select("chatbotLimit").eq("user_id", username).execute().data[0][
|
| 219 |
"chatbotLimit"]
|
| 220 |
if currentBotCount >= int(limit):
|
| 221 |
return {
|
| 222 |
"output": "CHATBOT LIMIT EXCEEDED"
|
| 223 |
}
|
| 224 |
+
supabase.table("ConversAI_ChatbotInfo").insert({"user_id": username, "chatbotname": chatbotName}).execute()
|
| 225 |
chatbotName = f"convai${username}${chatbotName}"
|
| 226 |
return createTable(tablename=chatbotName)
|
| 227 |
|
|
|
|
| 238 |
textExtraction = time.time()
|
| 239 |
os.remove(temp_file_path)
|
| 240 |
username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 241 |
+
df = pd.DataFrame(supabase.table("ConversAI_ChatbotInfo").select("*").execute().data)
|
| 242 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 243 |
+
limit = supabase.table("ConversAI_UserConfig").select("tokenLimit").eq("user_id", username).execute().data[0][
|
| 244 |
"tokenLimit"]
|
| 245 |
newCount = currentCount + len(text)
|
| 246 |
if newCount < int(limit):
|
| 247 |
+
supabase.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("user_id", username).eq(
|
| 248 |
"chatbotname", chatbotname).execute()
|
| 249 |
uploadStart = time.time()
|
| 250 |
output = addDocuments(text=text, source=source, vectorstore=vectorstore)
|
|
|
|
| 271 |
|
| 272 |
@app.post("/scanAndReturnText")
|
| 273 |
async def returnText(pdf: UploadFile = File(...)):
|
| 274 |
+
source = pdf.filename
|
| 275 |
pdf = await pdf.read()
|
| 276 |
start = time.time()
|
| 277 |
text = getTextFromImagePDF(pdfBytes=pdf)
|
| 278 |
end = time.time()
|
| 279 |
timeTaken = f"{end - start}s"
|
| 280 |
return {
|
| 281 |
+
"source": source,
|
| 282 |
"extractionTime": timeTaken,
|
| 283 |
"output": text
|
| 284 |
}
|
| 285 |
|
| 286 |
|
| 287 |
@app.post("/addText")
|
| 288 |
+
async def addText(vectorstore: str, text: str, source: str | None = None):
|
| 289 |
username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 290 |
+
df = pd.DataFrame(supabase.table("ConversAI_ChatbotInfo").select("*").execute().data)
|
| 291 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 292 |
newCount = currentCount + len(text)
|
| 293 |
+
limit = supabase.table("ConversAI_UserConfig").select("tokenLimit").eq("user_id", username).execute().data[0][
|
| 294 |
"tokenLimit"]
|
| 295 |
if newCount < int(limit):
|
| 296 |
+
supabase.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("user_id", username).eq(
|
| 297 |
"chatbotname", chatbotname).execute()
|
| 298 |
+
uploadStart = time.time()
|
| 299 |
+
output = addDocuments(text=text, source=source, vectorstore=vectorstore)
|
| 300 |
+
uploadEnd = time.time()
|
| 301 |
+
uploadTime = f"VECTOR UPLOAD TIME: {uploadEnd - uploadStart}s" + "\n"
|
| 302 |
+
tokenCount = f"TOKEN COUNT: {len(text)}" + "\n"
|
| 303 |
+
tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+")
|
| 304 |
+
wordCount = f"WORD COUNT: {len(tokenizer.tokenize(text))}" + "\n"
|
| 305 |
+
newText = ("=" * 75 + "\n").join([uploadTime, wordCount, tokenCount, "TEXT: \n" + text + "\n"])
|
| 306 |
+
fileId = str(uuid.uuid4())
|
| 307 |
+
with open(f"{fileId}.txt", "w") as file:
|
| 308 |
+
file.write(newText)
|
| 309 |
+
with open(f"{fileId}.txt", "rb") as f:
|
| 310 |
+
supabase.storage.from_("ConversAI").upload(file = f, path = os.path.join("/", f.name), file_options={"content-type": "text/plain"})
|
| 311 |
+
os.remove(f"{fileId}.txt")
|
| 312 |
+
output["supabaseFileName"] = f"{fileId}.txt"
|
| 313 |
+
return output
|
| 314 |
else:
|
| 315 |
return {
|
| 316 |
"output": "WEBSITE EXCEEDING LIMITS, PLEASE TRY WITH A SMALLER DOCUMENT."
|
|
|
|
| 326 |
@app.post("/addQAPair")
|
| 327 |
async def addText(addQaPair: AddQAPair):
|
| 328 |
username, chatbotname = addQaPair.vectorstore.split("$")[1], addQaPair.vectorstore.split("$")[2]
|
| 329 |
+
df = pd.DataFrame(supabase.table("ConversAI_ChatbotInfo").select("*").execute().data)
|
| 330 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 331 |
qa = f"QUESTION: {addQaPair.question}\tANSWER: {addQaPair.answer}"
|
| 332 |
newCount = currentCount + len(qa)
|
| 333 |
+
limit = supabase.table("ConversAI_UserConfig").select("tokenLimit").eq("user_id", username).execute().data[0][
|
| 334 |
"tokenLimit"]
|
| 335 |
if newCount < int(limit):
|
| 336 |
+
supabase.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("user_id", username).eq(
|
| 337 |
"chatbotname", chatbotname).execute()
|
| 338 |
return addDocuments(text=qa, source="Q&A Pairs", vectorstore=addQaPair.vectorstore)
|
| 339 |
else:
|
|
|
|
| 348 |
text = extractTextFromUrlList(urls = websiteUrls)
|
| 349 |
textExtraction = time.time()
|
| 350 |
username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 351 |
+
df = pd.DataFrame(supabase.table("ConversAI_ChatbotInfo").select("*").execute().data)
|
| 352 |
currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
|
| 353 |
newCount = currentCount + len(text)
|
| 354 |
+
limit = supabase.table("ConversAI_UserConfig").select("tokenLimit").eq("user_id", username).execute().data[0]["tokenLimit"]
|
| 355 |
if newCount < int(limit):
|
| 356 |
+
supabase.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("user_id", username).eq(
|
| 357 |
"chatbotname", chatbotname).execute()
|
| 358 |
uploadStart = time.time()
|
| 359 |
output = addDocuments(text=text, source=urlparse(websiteUrls[0]).netloc, vectorstore=vectorstore)
|
|
|
|
| 381 |
|
| 382 |
@app.post("/answerQuery")
|
| 383 |
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
|
| 384 |
+
username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 385 |
+
output = answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
|
| 386 |
+
response = (
|
| 387 |
+
supabase.table("ConversAI_ChatHistory")
|
| 388 |
+
.insert({"username": username, "chatbotName": chatbotName, "llmModel": llmModel, "question": query, "response": output["output"]})
|
| 389 |
+
.execute()
|
| 390 |
+
)
|
| 391 |
+
return output
|
| 392 |
|
| 393 |
|
| 394 |
@app.post("/deleteChatbot")
|
| 395 |
async def delete(chatbotName: str):
|
| 396 |
username, chatbotName = chatbotName.split("$")[1], chatbotName.split("$")[2]
|
| 397 |
+
supabase.table('ConversAI_ChatbotInfo').delete().eq('user_id', username).eq('chatbotname', chatbotName).execute()
|
| 398 |
return deleteTable(tableName=chatbotName)
|
| 399 |
|
| 400 |
|
|
|
|
| 413 |
@app.post("/getCurrentCount")
|
| 414 |
async def getCount(vectorstore: str):
|
| 415 |
username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
| 416 |
+
df = pd.DataFrame(supabase.table("ConversAI_ChatbotInfo").select("*").execute().data)
|
| 417 |
return {
|
| 418 |
"currentCount": df[(df['user_id'] == username) & (df['chatbotname'] == chatbotName)]['charactercount'].iloc[0]
|
| 419 |
}
|