Spaces:
Sleeping
Sleeping
File size: 6,695 Bytes
0dda2a1 6c7d766 0dda2a1 7d859ca 0dda2a1 c4a2d1f 0dda2a1 6c7d766 0dda2a1 cfd2b5e 064943c 0dda2a1 cfd2b5e 064943c 0dda2a1 011040f 6187b6c 6c7d766 011040f 064943c 0dda2a1 6c7d766 40d15f0 cfd2b5e 6c7d766 e6ccf57 6187b6c 6c7d766 6187b6c 1a55708 6c7d766 40d15f0 6c7d766 e6ccf57 6c7d766 6187b6c 1a55708 6c7d766 7d859ca 6187b6c 7d859ca 6c7d766 a383d87 6c7d766 a383d87 6c7d766 e6ccf57 6c7d766 6187b6c 1a55708 6c7d766 0dda2a1 011040f 8f8a88e 681223f cfd2b5e 40d15f0 011040f 40d15f0 6c7d766 064943c 40d15f0 011040f 40d15f0 5ebc71d 6c7d766 5ebc71d 6c7d766 e6ccf57 6c7d766 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import io
from functions import *
from PyPDF2 import PdfReader
import pandas as pd
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from langchain_community.document_loaders import UnstructuredURLLoader
app = FastAPI(title = "ConversAI", root_path = "/api/v1")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/signup")
async def signup(username: str, password: str):
response = createUser(username = username, password = password)
return response
@app.post("/login")
async def login(username: str, password: str):
response = matchPassword(username = username, password = password)
return response
@app.post("/newChatbot")
async def newChatbot(chatbotName: str, username: str):
currentBotCount = listTables(username = username)
limit = client.table("ConversAI_UserConfig").select("chatbotLimit").eq("username", "rauhan").execute().data[0]["chatbotLimit"]
if currentBotCount < limit:
return {
"output": "CHATBOT LIMIT EXCEEDED"
}
client.table("ConversAI_ChatbotInfo").insert({"username": username, "chatbotname": chatbotName}).execute()
chatbotName = f"convai-{username}-{chatbotName}"
return createTable(tablename = chatbotName)
@app.post("/addPDF")
async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)):
pdf = await pdf.read()
reader = PdfReader(io.BytesIO(pdf))
text = ""
for page in reader.pages:
text += page.extract_text()
username, chatbotname = vectorstore.split("-")[1], vectorstore.split("-")[2]
df = pd.DataFrame(client.table("ConversAI_ChatbotInfo").select("*").execute().data)
currentCount = df[(df["username"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
limit = client.table("ConversAI_UserConfig").select("tokenLimit").eq("username", username).execute().data[0]["tokenLimit"]
newCount = currentCount + len(text)
if newCount < int(limit):
client.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("username", username).eq("chatbotname", chatbotname).execute()
return addDocuments(text = text, vectorstore = vectorstore)
else:
return {
"output": "DOCUMENT EXCEEDING LIMITS, PLEASE TRY WITH A SMALLER DOCUMENT."
}
@app.post("/addText")
async def addText(vectorstore: str, text: str):
username, chatbotname = vectorstore.split("-")[1], vectorstore.split("-")[2]
df = pd.DataFrame(client.table("ConversAI_ChatbotInfo").select("*").execute().data)
currentCount = df[(df["username"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
newCount = currentCount + len(text)
limit = client.table("ConversAI_UserConfig").select("tokenLimit").eq("username", username).execute().data[0]["tokenLimit"]
if newCount < int(limit):
client.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("username", username).eq("chatbotname", chatbotname).execute()
return addDocuments(text = text, vectorstore = vectorstore)
else:
return {
"output": "WEBSITE EXCEEDING LIMITS, PLEASE TRY WITH A SMALLER DOCUMENT."
}
class AddQAPair(BaseModel):
vectorstore: str
question: str
answer: str
@app.post("/addQAPair")
async def addText(addQaPair: AddQAPair):
username, chatbotname = addQaPair.vectorstore.split("-")[1], addQaPair.vectorstore.split("-")[2]
df = pd.DataFrame(client.table("ConversAI_ChatbotInfo").select("*").execute().data)
currentCount = df[(df["username"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
qa = f"QUESTION: {addQaPair.question}\tANSWER: {addQaPair.answer}"
newCount = currentCount + len(qa)
limit = client.table("ConversAI_UserConfig").select("tokenLimit").eq("username", username).execute().data[0]["tokenLimit"]
if newCount < int(limit):
client.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("username", username).eq("chatbotname", chatbotname).execute()
return addDocuments(text = qa, vectorstore = addQaPair.vectorstore)
else:
return {
"output": "WEBSITE EXCEEDING LIMITS, PLEASE TRY WITH A SMALLER DOCUMENT."
}
@app.post("/addWebsite")
async def addWebsite(vectorstore: str, websiteUrls: list[str]):
urls = websiteUrls
loader = UnstructuredURLLoader(urls=urls)
docs = loader.load()
text = "\n\n".join([f"Metadata:\n{docs[doc].metadata} \nPage Content:\n {docs[doc].page_content}" for doc in range(len(docs))])
username, chatbotname = vectorstore.split("-")[1], vectorstore.split("-")[2]
df = pd.DataFrame(client.table("ConversAI_ChatbotInfo").select("*").execute().data)
currentCount = df[(df["username"] == username) & (df["chatbotname"] == chatbotname)]["charactercount"].iloc[0]
newCount = currentCount + len(text)
limit = client.table("ConversAI_UserConfig").select("tokenLimit").eq("username", username).execute().data[0]["tokenLimit"]
if newCount < int(limit):
client.table("ConversAI_ChatbotInfo").update({"charactercount": str(newCount)}).eq("username", username).eq("chatbotname", chatbotname).execute()
return addDocuments(text = text, vectorstore = vectorstore)
else:
return {
"output": "WEBSITE EXCEEDING LIMITS, PLEASE TRY WITH A SMALLER DOCUMENT."
}
@app.post("/answerQuery")
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"):
return answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel)
@app.post("/deleteChatbot")
async def delete(chatbotName: str):
username, chatbotName = chatbotName.split("-")[1], chatbotName.split("-")[2]
client.table('ConversAI_ChatbotInfo').delete().eq('username', username).eq('chatbotname', chatbotName).execute()
return deleteTable(tableName=chatbotName)
@app.post("/listChatbots")
async def delete(username: str):
return listTables(username=username)
@app.post("/getLinks")
async def crawlUrl(baseUrl: str):
return {
"urls": getLinks(url=baseUrl, timeout=30)
}
@app.post("/getCurrentCount")
async def getCount(vectorstore: str):
username, chatbotName = chatbotName.split("-")[1], chatbotName.split("-")[2]
df = pd.DataFrame(client.table("ConversAI_ChatbotInfo").select("*").execute().data)
return {
"currentCount": df[(df['username'] == username) & (df['chatbotname'] == chatbotName)]['charactercount'].iloc[0]
} |