Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
API_URL = "https://sendthat.cc"
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
def search_document(index_name, query, k):
|
7 |
url = f"{API_URL}/search/{index_name}"
|
8 |
payload = {"text": query, "k": k}
|
9 |
headers = {"Content-Type": "application/json"}
|
10 |
response = requests.post(url, json=payload, headers=headers)
|
11 |
-
|
12 |
-
|
13 |
-
formatted_results = []
|
14 |
-
for result in results.get('results', []):
|
15 |
-
metadata = result.get('metadata', {})
|
16 |
-
formatted_result = f"Source: {metadata.get('source', 'Unknown')}\n"
|
17 |
-
formatted_result += f"Page: {metadata.get('page', 'Unknown')}\n"
|
18 |
-
formatted_result += f"Content: {metadata.get('content', 'No content available')}\n"
|
19 |
-
formatted_result += f"Distance: {result.get('distance', 'Unknown')}\n"
|
20 |
-
formatted_results.append(formatted_result)
|
21 |
-
|
22 |
-
return "\n\n".join(formatted_results)
|
23 |
|
24 |
def qa_document(index_name, question, k):
|
25 |
url = f"{API_URL}/qa/{index_name}"
|
26 |
payload = {"text": question, "k": k}
|
27 |
headers = {"Content-Type": "application/json"}
|
28 |
response = requests.post(url, json=payload, headers=headers)
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
formatted_sources = []
|
35 |
-
for source in sources:
|
36 |
-
formatted_source = f"Source: {source.get('source', 'Unknown')}\n"
|
37 |
-
formatted_source += f"Relevance Score: {source.get('relevance_score', 'Unknown')}"
|
38 |
-
formatted_sources.append(formatted_source)
|
39 |
-
|
40 |
-
formatted_result = f"Answer: {answer}\n\nSources:\n" + "\n\n".join(formatted_sources)
|
41 |
-
return formatted_result
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
gr.Markdown("# Document Search and Question Answering System")
|
45 |
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
with gr.Tab("Search"):
|
49 |
search_input = gr.Textbox(label="Search Query")
|
50 |
search_k = gr.Slider(1, 10, 5, step=1, label="Number of Results")
|
51 |
search_button = gr.Button("Search")
|
52 |
-
search_output = gr.
|
53 |
|
54 |
-
search_button.click(search_document, inputs=[index_name, search_input, search_k], outputs=search_output)
|
55 |
-
|
56 |
with gr.Tab("Question Answering"):
|
57 |
qa_input = gr.Textbox(label="Question")
|
58 |
qa_k = gr.Slider(1, 10, 5, step=1, label="Number of Contexts to Consider")
|
59 |
qa_button = gr.Button("Ask Question")
|
60 |
-
qa_output = gr.
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import json
|
4 |
|
5 |
API_URL = "https://sendthat.cc"
|
6 |
|
7 |
+
def list_indexes():
|
8 |
+
url = f"{API_URL}/list_indexes"
|
9 |
+
response = requests.get(url)
|
10 |
+
return response.json()
|
11 |
+
|
12 |
def search_document(index_name, query, k):
|
13 |
url = f"{API_URL}/search/{index_name}"
|
14 |
payload = {"text": query, "k": k}
|
15 |
headers = {"Content-Type": "application/json"}
|
16 |
response = requests.post(url, json=payload, headers=headers)
|
17 |
+
return response.json(), "", k
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def qa_document(index_name, question, k):
|
20 |
url = f"{API_URL}/qa/{index_name}"
|
21 |
payload = {"text": question, "k": k}
|
22 |
headers = {"Content-Type": "application/json"}
|
23 |
response = requests.post(url, json=payload, headers=headers)
|
24 |
+
return response.json(), "", k
|
25 |
+
|
26 |
+
def refresh_indexes():
|
27 |
+
indexes = list_indexes()
|
28 |
+
return gr.Dropdown(choices=indexes, label="Select Index")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Custom CSS for modern appearance
|
31 |
+
custom_css = """
|
32 |
+
.gradio-container {
|
33 |
+
background-color: #f5f5f5;
|
34 |
+
}
|
35 |
+
.input-box, .output-box, .gr-box {
|
36 |
+
border-radius: 8px !important;
|
37 |
+
border-color: #d1d1d1 !important;
|
38 |
+
}
|
39 |
+
.input-box, .gr-dropdown {
|
40 |
+
background-color: #e6f2ff !important;
|
41 |
+
}
|
42 |
+
.gr-button {
|
43 |
+
background-color: #4a90e2 !important;
|
44 |
+
color: white !important;
|
45 |
+
}
|
46 |
+
.gr-button:hover {
|
47 |
+
background-color: #3a7bc8 !important;
|
48 |
+
}
|
49 |
+
.gr-form {
|
50 |
+
border-color: #d1d1d1 !important;
|
51 |
+
background-color: white !important;
|
52 |
+
}
|
53 |
+
"""
|
54 |
+
|
55 |
+
with gr.Blocks(css=custom_css) as demo:
|
56 |
gr.Markdown("# Document Search and Question Answering System")
|
57 |
|
58 |
+
with gr.Row():
|
59 |
+
index_dropdown = gr.Dropdown(label="Select Index", choices=list_indexes())
|
60 |
+
refresh_button = gr.Button("Refresh Indexes")
|
61 |
+
|
62 |
with gr.Tab("Search"):
|
63 |
search_input = gr.Textbox(label="Search Query")
|
64 |
search_k = gr.Slider(1, 10, 5, step=1, label="Number of Results")
|
65 |
search_button = gr.Button("Search")
|
66 |
+
search_output = gr.JSON(label="Search Results")
|
67 |
|
|
|
|
|
68 |
with gr.Tab("Question Answering"):
|
69 |
qa_input = gr.Textbox(label="Question")
|
70 |
qa_k = gr.Slider(1, 10, 5, step=1, label="Number of Contexts to Consider")
|
71 |
qa_button = gr.Button("Ask Question")
|
72 |
+
qa_output = gr.JSON(label="Answer")
|
73 |
+
|
74 |
+
refresh_button.click(refresh_indexes, outputs=index_dropdown)
|
75 |
+
search_button.click(search_document,
|
76 |
+
inputs=[index_dropdown, search_input, search_k],
|
77 |
+
outputs=[search_output, search_input, search_k])
|
78 |
+
qa_button.click(qa_document,
|
79 |
+
inputs=[index_dropdown, qa_input, qa_k],
|
80 |
+
outputs=[qa_output, qa_input, qa_k])
|
81 |
|
82 |
demo.launch()
|