Spaces:
Sleeping
Sleeping
import io | |
from functions import * | |
from PyPDF2 import PdfReader | |
from fastapi import FastAPI, File, UploadFile | |
from fastapi.middleware.cors import CORSMiddleware | |
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): | |
try: | |
response = createUser(username = username, password = password) | |
output = { | |
"output": response | |
} | |
except Exception as e: | |
output = { | |
"error": e | |
} | |
return output | |
async def login(username: str, password: str): | |
try: | |
response = matchPassword(username = username, password = password) | |
output = { | |
"output": response | |
} | |
except Exception as e: | |
output = { | |
"error": e | |
} | |
return output | |
async def newChatbot(chatbotName: str): | |
createTable(tablename = chatbotName) | |
return { | |
"output": "SUCCESS" | |
} | |
async def addPDFData(vectorstore: str, pdf: UploadFile = File(...)): | |
try: | |
pdf = await pdf.read() | |
reader = PdfReader(io.BytesIO(pdf)) | |
text = "" | |
for page in reader.pages: | |
text += page.extract_text() | |
addDocuments(text = text, vectorstore = vectorstore) | |
output = { | |
"output": "SUCCESS" | |
} | |
except Exception as e: | |
output = { | |
"error": e | |
} | |
return output | |
async def addText(vectorstore: str, text: str): | |
addDocuments(text = text, vectorstore = vectorstore) | |
try: | |
output = { | |
"output": "SUCCESS" | |
} | |
return output | |
except Exception as e: | |
output = { | |
"error": e | |
} | |
return output | |
async def answerQuestion(query: str, vectorstore: str, llmModel: str = "llama3-70b-8192"): | |
try: | |
response = answerQuery(query=query, vectorstore=vectorstore, llmModel=llmModel) | |
output = { | |
"output": response | |
} | |
except Exception as e: | |
output = { | |
"error": e | |
} | |
return output | |
async def delete(chatbotName: str): | |
try: | |
deleteTable(tableName=chatbotName) | |
response = { | |
"output": "SUCCESS" | |
} | |
except Exception as e: | |
response = { | |
"output": e | |
} | |
return response | |
async def delete(username: str): | |
try: | |
chatbots = listTables(username=username) | |
response = { | |
"output": chatbots | |
} | |
except Exception as e: | |
response = { | |
"output": e | |
} | |
return response |