Spaces:
Sleeping
Sleeping
import io | |
from functions import * | |
from PyPDF2 import PdfReader | |
from fastapi import FastAPI, File, UploadFile | |
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=["*"], | |
) | |
async def signup(username: str, password: str): | |
response = createUser(username = username, password = password) | |
return response | |
async def login(username: str, password: str): | |
response = matchPassword(username = username, password = password) | |
return response | |
async def newChatbot(chatbotName: str, username: str): | |
chatbotName = f"convai-{username}-{chatbotName}" | |
return createTable(tablename = chatbotName) | |
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() | |
return addDocuments(text = text, vectorstore = vectorstore) | |
async def addText(vectorstore: str, text: str): | |
return addDocuments(text = text, vectorstore = vectorstore) | |
async def addWebsite(vectorstore: str, websiteUrl: str): | |
urls = getLinks(websiteUrl) | |
loader = UnstructuredURLLoader(urls=urls) | |
docs = loader.load() | |
text = "\n\n\n\n".join([f"Metadata:\n{docs[doc].metadata} \nPage Content:\n {docs[doc].page_content}" for doc in range(len(docs))]) | |
return addDocuments(text = text, vectorstore = vectorstore) | |
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"): | |
return answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel) | |
async def delete(chatbotName: str): | |
return deleteTable(tableName=chatbotName) | |
async def delete(username: str): | |
return listTables(username=username) |