Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 7 |
+
|
| 8 |
+
API_URL = "https://sendthat.cc"
|
| 9 |
+
HISTORY_INDEX = "history"
|
| 10 |
+
|
| 11 |
+
def search_document(query, k):
|
| 12 |
+
try:
|
| 13 |
+
url = f"{API_URL}/search/{HISTORY_INDEX}"
|
| 14 |
+
payload = {"text": query, "k": k}
|
| 15 |
+
headers = {"Content-Type": "application/json"}
|
| 16 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 17 |
+
response.raise_for_status()
|
| 18 |
+
return response.json(), "", k
|
| 19 |
+
except requests.exceptions.RequestException as e:
|
| 20 |
+
logging.error(f"Error in search: {e}")
|
| 21 |
+
return {"error": str(e)}, query, k
|
| 22 |
+
|
| 23 |
+
def qa_document(question, k):
|
| 24 |
+
try:
|
| 25 |
+
url = f"{API_URL}/qa/{HISTORY_INDEX}"
|
| 26 |
+
payload = {"text": question, "k": k}
|
| 27 |
+
headers = {"Content-Type": "application/json"}
|
| 28 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 29 |
+
response.raise_for_status()
|
| 30 |
+
return response.json(), "", k
|
| 31 |
+
except requests.exceptions.RequestException as e:
|
| 32 |
+
logging.error(f"Error in QA: {e}")
|
| 33 |
+
return {"error": str(e)}, question, k
|
| 34 |
+
|
| 35 |
+
# Custom CSS for modern appearance
|
| 36 |
+
custom_css = """
|
| 37 |
+
.gradio-container {
|
| 38 |
+
background-color: #f5f5f5;
|
| 39 |
+
}
|
| 40 |
+
.input-box, .output-box, .gr-box {
|
| 41 |
+
border-radius: 8px !important;
|
| 42 |
+
border-color: #d1d1d1 !important;
|
| 43 |
+
}
|
| 44 |
+
.input-box {
|
| 45 |
+
background-color: #e6f2ff !important;
|
| 46 |
+
}
|
| 47 |
+
.gr-button {
|
| 48 |
+
background-color: #4a90e2 !important;
|
| 49 |
+
color: white !important;
|
| 50 |
+
}
|
| 51 |
+
.gr-button:hover {
|
| 52 |
+
background-color: #3a7bc8 !important;
|
| 53 |
+
}
|
| 54 |
+
.gr-form {
|
| 55 |
+
border-color: #d1d1d1 !important;
|
| 56 |
+
background-color: white !important;
|
| 57 |
+
}
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 61 |
+
gr.Markdown("# History Document Search and Question Answering System")
|
| 62 |
+
|
| 63 |
+
with gr.Tab("Search"):
|
| 64 |
+
search_input = gr.Textbox(label="Search Query")
|
| 65 |
+
search_k = gr.Slider(1, 10, 5, step=1, label="Number of Results")
|
| 66 |
+
search_button = gr.Button("Search")
|
| 67 |
+
search_output = gr.JSON(label="Search Results")
|
| 68 |
+
|
| 69 |
+
with gr.Tab("Question Answering"):
|
| 70 |
+
qa_input = gr.Textbox(label="Question")
|
| 71 |
+
qa_k = gr.Slider(1, 10, 5, step=1, label="Number of Contexts to Consider")
|
| 72 |
+
qa_button = gr.Button("Ask Question")
|
| 73 |
+
qa_output = gr.JSON(label="Answer")
|
| 74 |
+
|
| 75 |
+
search_button.click(search_document,
|
| 76 |
+
inputs=[search_input, search_k],
|
| 77 |
+
outputs=[search_output, search_input, search_k])
|
| 78 |
+
qa_button.click(qa_document,
|
| 79 |
+
inputs=[qa_input, qa_k],
|
| 80 |
+
outputs=[qa_output, qa_input, qa_k])
|
| 81 |
+
|
| 82 |
+
demo.launch()
|