ishworrsubedii commited on
Commit
4275b3b
1 Parent(s): 39cf044

Minor changes in the session check and oauth

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import io
2
  import tempfile
 
 
3
  from starlette import status
4
  from functions import *
5
  import pandas as pd
@@ -13,7 +15,6 @@ import nltk
13
  import time
14
  import uuid
15
 
16
-
17
  nltk.download('punkt_tab')
18
 
19
  app = FastAPI(title="ConversAI", root_path="/api/v1")
@@ -48,11 +49,24 @@ async def sign_up(email, username, password):
48
 
49
 
50
  @app.post("/session-check")
51
- async def check_session():
52
  res = supabase.auth.get_session()
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  return res
55
 
 
56
  @app.post("/get-user")
57
  async def get_user(access_token):
58
  res = supabase.auth.get_user(jwt=access_token)
@@ -65,7 +79,6 @@ async def refresh_token(refresh_token):
65
  return res
66
 
67
 
68
-
69
  @app.post("/login")
70
  async def sign_in(email, password):
71
  try:
@@ -184,7 +197,6 @@ async def login_with_token(token):
184
  )
185
 
186
 
187
-
188
  @app.post("/set-session-data")
189
  async def set_session_data(access_token, refresh_token):
190
  res = supabase.auth.set_session(access_token, refresh_token)
@@ -207,8 +219,9 @@ async def sign_out(user_id):
207
 
208
 
209
  @app.post("/oauth")
210
- async def oauth(provider):
211
- res = supabase.auth.sign_in_with_oauth({"provider": provider})
 
212
  return res
213
 
214
 
@@ -259,7 +272,8 @@ async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
259
  with open(f"{fileId}.txt", "w") as file:
260
  file.write(newText)
261
  with open(f"{fileId}.txt", "rb") as f:
262
- supabase.storage.from_("ConversAI").upload(file = f, path = os.path.join("/", f.name), file_options={"content-type": "text/plain"})
 
263
  os.remove(f"{fileId}.txt")
264
  output["supabaseFileName"] = f"{fileId}.txt"
265
  return output
@@ -280,7 +294,7 @@ async def returnText(pdf: UploadFile = File(...)):
280
  return {
281
  "source": source,
282
  "extractionTime": timeTaken,
283
- "output": text
284
  }
285
 
286
 
@@ -307,7 +321,8 @@ async def addText(vectorstore: str, text: str, source: str | None = None):
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
@@ -345,13 +360,14 @@ async def addQAPairData(addQaPair: AddQAPair):
345
  @app.post("/addWebsite")
346
  async def addWebsite(vectorstore: str, websiteUrls: list[str]):
347
  start = time.time()
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()
@@ -364,12 +380,14 @@ async def addWebsite(vectorstore: str, websiteUrls: list[str]):
364
  tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+")
365
  wordCount = f"WORD COUNT: {len(tokenizer.tokenize(text))}" + "\n"
366
  links = "LINKS:\n" + "\n".join(websiteUrls) + "\n"
367
- newText = ("=" * 75 + "\n").join([timeTaken, uploadTime, wordCount, tokenCount, links, "TEXT: \n" + text + "\n"])
 
368
  fileId = str(uuid.uuid4())
369
  with open(f"{fileId}.txt", "w") as file:
370
  file.write(newText)
371
  with open(f"{fileId}.txt", "rb") as f:
372
- supabase.storage.from_("ConversAI").upload(file = f, path = os.path.join("/", f.name), file_options={"content-type": "text/plain"})
 
373
  os.remove(f"{fileId}.txt")
374
  output["supabaseFileName"] = f"{fileId}.txt"
375
  return output
@@ -385,7 +403,8 @@ async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-7
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
@@ -450,5 +469,7 @@ async def analyzeAndAnswer(query: str, file: UploadFile = File(...)):
450
  @app.post("/getChatHistory")
451
  async def chatHistory(vectorstore: str):
452
  username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
453
- response = supabase.table("ConversAI_ChatHistory").select("timestamp", "question", "response").eq("username", username).eq("chatbotName", chatbotName).execute().data
454
- return response
 
 
 
1
  import io
2
  import tempfile
3
+
4
+ from click import option
5
  from starlette import status
6
  from functions import *
7
  import pandas as pd
 
15
  import time
16
  import uuid
17
 
 
18
  nltk.download('punkt_tab')
19
 
20
  app = FastAPI(title="ConversAI", root_path="/api/v1")
 
49
 
50
 
51
  @app.post("/session-check")
52
+ async def check_session(user_id: str):
53
  res = supabase.auth.get_session()
54
+ if res == None:
55
+ try:
56
+ supabase.table("Stores").delete().eq(
57
+ "StoreID", user_id
58
+ ).execute()
59
+ resp = supabase.auth.sign_out()
60
+
61
+ response = {"message": "success", "code": 200, "Session": res}
62
+
63
+ return response
64
+ except Exception as e:
65
+ raise HTTPException(status_code=400, detail=str(e))
66
 
67
  return res
68
 
69
+
70
  @app.post("/get-user")
71
  async def get_user(access_token):
72
  res = supabase.auth.get_user(jwt=access_token)
 
79
  return res
80
 
81
 
 
82
  @app.post("/login")
83
  async def sign_in(email, password):
84
  try:
 
197
  )
198
 
199
 
 
200
  @app.post("/set-session-data")
201
  async def set_session_data(access_token, refresh_token):
202
  res = supabase.auth.set_session(access_token, refresh_token)
 
219
 
220
 
221
  @app.post("/oauth")
222
+ async def oauth():
223
+ res = supabase.auth.sign_in_with_oauth(
224
+ {"provider": "google", "options": {"redirect_to": "https://convers-ai-lac.vercel.app"}})
225
  return res
226
 
227
 
 
272
  with open(f"{fileId}.txt", "w") as file:
273
  file.write(newText)
274
  with open(f"{fileId}.txt", "rb") as f:
275
+ supabase.storage.from_("ConversAI").upload(file=f, path=os.path.join("/", f.name),
276
+ file_options={"content-type": "text/plain"})
277
  os.remove(f"{fileId}.txt")
278
  output["supabaseFileName"] = f"{fileId}.txt"
279
  return output
 
294
  return {
295
  "source": source,
296
  "extractionTime": timeTaken,
297
+ "output": text
298
  }
299
 
300
 
 
321
  with open(f"{fileId}.txt", "w") as file:
322
  file.write(newText)
323
  with open(f"{fileId}.txt", "rb") as f:
324
+ supabase.storage.from_("ConversAI").upload(file=f, path=os.path.join("/", f.name),
325
+ file_options={"content-type": "text/plain"})
326
  os.remove(f"{fileId}.txt")
327
  output["supabaseFileName"] = f"{fileId}.txt"
328
  return output
 
360
  @app.post("/addWebsite")
361
  async def addWebsite(vectorstore: str, websiteUrls: list[str]):
362
  start = time.time()
363
+ text = extractTextFromUrlList(urls=websiteUrls)
364
  textExtraction = time.time()
365
  username, chatbotname = vectorstore.split("$")[1], vectorstore.split("$")[2]
366
  df = pd.DataFrame(supabase.table("ConversAI_ChatbotInfo").select("*").execute().data)
367
  currentCount = df[(df["user_id"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
368
  newCount = currentCount + len(text)
369
+ limit = supabase.table("ConversAI_UserConfig").select("tokenLimit").eq("user_id", username).execute().data[0][
370
+ "tokenLimit"]
371
  if newCount < int(limit):
372
  supabase.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("user_id", username).eq(
373
  "chatbotname", chatbotname).execute()
 
380
  tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+")
381
  wordCount = f"WORD COUNT: {len(tokenizer.tokenize(text))}" + "\n"
382
  links = "LINKS:\n" + "\n".join(websiteUrls) + "\n"
383
+ newText = ("=" * 75 + "\n").join(
384
+ [timeTaken, uploadTime, wordCount, tokenCount, links, "TEXT: \n" + text + "\n"])
385
  fileId = str(uuid.uuid4())
386
  with open(f"{fileId}.txt", "w") as file:
387
  file.write(newText)
388
  with open(f"{fileId}.txt", "rb") as f:
389
+ supabase.storage.from_("ConversAI").upload(file=f, path=os.path.join("/", f.name),
390
+ file_options={"content-type": "text/plain"})
391
  os.remove(f"{fileId}.txt")
392
  output["supabaseFileName"] = f"{fileId}.txt"
393
  return output
 
403
  output = answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
404
  response = (
405
  supabase.table("ConversAI_ChatHistory")
406
+ .insert({"username": username, "chatbotName": chatbotName, "llmModel": llmModel, "question": query,
407
+ "response": output["output"]})
408
  .execute()
409
  )
410
  return output
 
469
  @app.post("/getChatHistory")
470
  async def chatHistory(vectorstore: str):
471
  username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
472
+ response = supabase.table("ConversAI_ChatHistory").select("timestamp", "question", "response").eq("username",
473
+ username).eq(
474
+ "chatbotName", chatbotName).execute().data
475
+ return response