Spaces:
Sleeping
Sleeping
File size: 1,914 Bytes
89dc8b2 1132b50 89dc8b2 e960d63 89dc8b2 6323bc8 89dc8b2 6323bc8 e960d63 1132b50 67bfb80 1132b50 4dc1d14 1132b50 6323bc8 e960d63 78aafcc e960d63 89dc8b2 6323bc8 e960d63 6323bc8 e960d63 89dc8b2 |
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 |
from fastapi import FastAPI
from pydantic import BaseModel
import faq as faq
import util as util
import uvicorn
import gradio as gr
app = FastAPI()
class AskRequest(BaseModel):
question: str
sheet_url: str
page_content_column: str
k: int
@app.post("/api/v1/ask")
async def ask_api(request: AskRequest):
return ask(
request.sheet_url, request.page_content_column, request.k, request.question
)
@app.post("/api/v2/ask")
async def ask_api(request: AskRequest):
util.SPLIT_PAGE_BREAKS = True
vectordb = faq.load_vectordb(request.sheet_url, request.page_content_column)
documents = faq.similarity_search(vectordb, request.question, k=request.k)
df_doc = util.transform_documents_to_dataframe(documents)
df_filter = util.remove_duplicates_by_column(df_doc, "ID")
return util.dataframe_to_dict(df_filter)
@app.delete("/api/v1/")
async def delete_vectordb_api():
return delete_vectordb()
def ask(sheet_url: str, page_content_column: str, k: int, question: str):
util.SPLIT_PAGE_BREAKS = False
vectordb = faq.load_vectordb(sheet_url, page_content_column)
result = faq.similarity_search(vectordb, question, k=k)
return result
def delete_vectordb():
faq.delete_vectordb()
with gr.Blocks() as block:
sheet_url = gr.Textbox(label="Google Sheet URL")
page_content_column = gr.Textbox(label="Question Column")
k = gr.Slider(2, 5, step=1, label="K")
question = gr.Textbox(label="Question")
ask_button = gr.Button("Ask")
answer_output = gr.JSON(label="Answer")
delete_button = gr.Button("Delete Vector DB")
ask_button.click(
ask,
inputs=[sheet_url, page_content_column, k, question],
outputs=answer_output,
)
delete_button.click(delete_vectordb)
app = gr.mount_gradio_app(app, block, path="/")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|