Spaces:
Sleeping
Sleeping
Update
Browse files
app.py
CHANGED
@@ -10,30 +10,26 @@ from fastapi.responses import JSONResponse
|
|
10 |
app = FastAPI()
|
11 |
|
12 |
|
13 |
-
class
|
14 |
-
question: str
|
15 |
sheet_url: str
|
16 |
page_content_column: str
|
17 |
-
k: int = 20
|
18 |
-
reload_collection: Optional[bool] =
|
19 |
id_column: Optional[str] = None
|
20 |
synonyms: Optional[List[List[str]]] = None
|
21 |
|
22 |
|
23 |
-
@app.post("/api
|
24 |
-
async def
|
25 |
-
return ask(
|
26 |
-
request.sheet_url, request.page_content_column, request.k, request.question
|
27 |
-
)
|
28 |
-
|
29 |
-
|
30 |
-
@app.post("/api/v2/ask")
|
31 |
-
async def ask_api(request: AskRequest):
|
32 |
if request.id_column is not None:
|
33 |
util.SPLIT_PAGE_BREAKS = True
|
34 |
if request.synonyms is not None:
|
35 |
util.SYNONYMS = request.synonyms
|
36 |
vectordb = faq.load_vectordb(request.sheet_url, request.page_content_column)
|
|
|
|
|
|
|
37 |
documents = faq.similarity_search(vectordb, request.question, k=request.k)
|
38 |
df_doc = util.transform_documents_to_dataframe(documents)
|
39 |
if request.id_column is not None:
|
@@ -41,37 +37,50 @@ async def ask_api(request: AskRequest):
|
|
41 |
return JSONResponse(util.dataframe_to_dict(df_doc))
|
42 |
|
43 |
|
44 |
-
@app.
|
45 |
-
async def
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
|
49 |
-
def ask(sheet_url: str, page_content_column: str, k: int, question: str):
|
50 |
util.SPLIT_PAGE_BREAKS = False
|
51 |
vectordb = faq.load_vectordb(sheet_url, page_content_column)
|
|
|
|
|
|
|
52 |
documents = faq.similarity_search(vectordb, question, k=k)
|
53 |
df_doc = util.transform_documents_to_dataframe(documents)
|
54 |
-
return util.dataframe_to_dict(df_doc)
|
55 |
-
|
56 |
-
|
57 |
-
def delete_vectordb():
|
58 |
-
faq.delete_vectordb()
|
59 |
|
60 |
|
61 |
with gr.Blocks() as block:
|
62 |
sheet_url = gr.Textbox(label="Google Sheet URL")
|
63 |
page_content_column = gr.Textbox(label="Question Column")
|
64 |
k = gr.Slider(1, 30, step=1, label="K")
|
|
|
65 |
question = gr.Textbox(label="Question")
|
66 |
ask_button = gr.Button("Ask")
|
67 |
answer_output = gr.JSON(label="Answer")
|
68 |
-
delete_button = gr.Button("Delete Vector DB")
|
69 |
ask_button.click(
|
70 |
ask,
|
71 |
-
inputs=[sheet_url, page_content_column, k, question],
|
72 |
-
outputs=answer_output,
|
73 |
)
|
74 |
-
delete_button.click(delete_vectordb)
|
75 |
|
76 |
app = gr.mount_gradio_app(app, block, path="/")
|
77 |
|
|
|
10 |
app = FastAPI()
|
11 |
|
12 |
|
13 |
+
class Request(BaseModel):
|
14 |
+
Optional[question]: str = "?"
|
15 |
sheet_url: str
|
16 |
page_content_column: str
|
17 |
+
k: Optional[int] = 20
|
18 |
+
reload_collection: Optional[bool] = False
|
19 |
id_column: Optional[str] = None
|
20 |
synonyms: Optional[List[List[str]]] = None
|
21 |
|
22 |
|
23 |
+
@app.post("/api")
|
24 |
+
async def post_api(request: Request) -> JSONResponse:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
if request.id_column is not None:
|
26 |
util.SPLIT_PAGE_BREAKS = True
|
27 |
if request.synonyms is not None:
|
28 |
util.SYNONYMS = request.synonyms
|
29 |
vectordb = faq.load_vectordb(request.sheet_url, request.page_content_column)
|
30 |
+
if request.reload_collection:
|
31 |
+
faq.delete_vectordb_current_collection(vectordb)
|
32 |
+
vectordb = faq.load_vectordb(request.sheet_url, request.page_content_column)
|
33 |
documents = faq.similarity_search(vectordb, request.question, k=request.k)
|
34 |
df_doc = util.transform_documents_to_dataframe(documents)
|
35 |
if request.id_column is not None:
|
|
|
37 |
return JSONResponse(util.dataframe_to_dict(df_doc))
|
38 |
|
39 |
|
40 |
+
@app.put("/api")
|
41 |
+
async def put_api(request: Request) -> bool:
|
42 |
+
success = False
|
43 |
+
if request.id_column is not None:
|
44 |
+
util.SPLIT_PAGE_BREAKS = True
|
45 |
+
if request.synonyms is not None:
|
46 |
+
util.SYNONYMS = request.synonyms
|
47 |
+
vectordb = faq.load_vectordb(request.sheet_url, request.page_content_column)
|
48 |
+
if request.reload_collection:
|
49 |
+
faq.delete_vectordb_current_collection(vectordb)
|
50 |
+
vectordb = faq.load_vectordb(request.sheet_url, request.page_content_column)
|
51 |
+
success = True
|
52 |
+
return success
|
53 |
+
|
54 |
+
|
55 |
+
@app.delete("/api")
|
56 |
+
async def delete_vectordb_api() -> None:
|
57 |
+
faq.delete_vectordb()
|
58 |
|
59 |
|
60 |
+
def ask(sheet_url: str, page_content_column: str, k: int, reload_collection: bool, question: str):
|
61 |
util.SPLIT_PAGE_BREAKS = False
|
62 |
vectordb = faq.load_vectordb(sheet_url, page_content_column)
|
63 |
+
if reload_collection:
|
64 |
+
faq.delete_vectordb_current_collection(vectordb)
|
65 |
+
vectordb = faq.load_vectordb(sheet_url, page_content_column)
|
66 |
documents = faq.similarity_search(vectordb, question, k=k)
|
67 |
df_doc = util.transform_documents_to_dataframe(documents)
|
68 |
+
return util.dataframe_to_dict(df_doc), gr.Checkbox.update(False)
|
|
|
|
|
|
|
|
|
69 |
|
70 |
|
71 |
with gr.Blocks() as block:
|
72 |
sheet_url = gr.Textbox(label="Google Sheet URL")
|
73 |
page_content_column = gr.Textbox(label="Question Column")
|
74 |
k = gr.Slider(1, 30, step=1, label="K")
|
75 |
+
reload_collection = gr.Checkbox(label="Reload Collection?")
|
76 |
question = gr.Textbox(label="Question")
|
77 |
ask_button = gr.Button("Ask")
|
78 |
answer_output = gr.JSON(label="Answer")
|
|
|
79 |
ask_button.click(
|
80 |
ask,
|
81 |
+
inputs=[sheet_url, page_content_column, k, reload_collection, question],
|
82 |
+
outputs=[answer_output, reload_collection]
|
83 |
)
|
|
|
84 |
|
85 |
app = gr.mount_gradio_app(app, block, path="/")
|
86 |
|