Spaces:
Sleeping
Sleeping
File size: 4,098 Bytes
6f45939 ff2fdee 31b96c8 a2cd209 d440c70 548732b d440c70 31b96c8 7f71865 c6f21e7 a2cd209 c6f21e7 a2cd209 7f71865 a2cd209 c6f21e7 a2cd209 31b96c8 ff2fdee a2cd209 d440c70 ff2fdee a2cd209 31b96c8 d440c70 31b96c8 ff2fdee d440c70 31b96c8 ff2fdee 31b96c8 ff2fdee 31b96c8 d440c70 |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import gradio as gr
import requests
import json
import logging
logging.basicConfig(level=logging.DEBUG)
API_URL = "https://sendthat.cc"
def list_indexes():
try:
url = f"{API_URL}/list_indexes"
response = requests.get(url)
response.raise_for_status()
indexes = response.json()
logging.debug(f"API response for indexes: {indexes}")
if isinstance(indexes, list):
if "indexes" in indexes:
return ["history", "medical"] # Hardcoded index names
elif indexes:
return indexes
else:
return ["No indexes available"]
elif isinstance(indexes, dict) and "indexes" in indexes:
return indexes["indexes"] if indexes["indexes"] else ["No indexes available"]
else:
logging.warning(f"Unexpected response format: {indexes}")
return ["history", "medical"] # Fallback to hardcoded index names
except requests.exceptions.RequestException as e:
logging.error(f"Error fetching indexes: {e}")
return ["Error fetching indexes"]
except json.JSONDecodeError as e:
logging.error(f"Error decoding JSON: {e}")
return ["Error decoding response"]
def search_document(index_name, query, k):
try:
url = f"{API_URL}/search/{index_name}"
payload = {"text": query, "k": k}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
return response.json(), "", k
except requests.exceptions.RequestException as e:
logging.error(f"Error in search: {e}")
return {"error": str(e)}, query, k
def qa_document(index_name, question, k):
try:
url = f"{API_URL}/qa/{index_name}"
payload = {"text": question, "k": k}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
return response.json(), "", k
except requests.exceptions.RequestException as e:
logging.error(f"Error in QA: {e}")
return {"error": str(e)}, question, k
def refresh_indexes():
indexes = list_indexes()
return gr.Dropdown(choices=indexes, label="Select Index")
# Custom CSS for modern appearance
custom_css = """
.gradio-container {
background-color: #f5f5f5;
}
.input-box, .output-box, .gr-box {
border-radius: 8px !important;
border-color: #d1d1d1 !important;
}
.input-box, .gr-dropdown {
background-color: #e6f2ff !important;
}
.gr-button {
background-color: #4a90e2 !important;
color: white !important;
}
.gr-button:hover {
background-color: #3a7bc8 !important;
}
.gr-form {
border-color: #d1d1d1 !important;
background-color: white !important;
}
"""
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("# Document Search and Question Answering System")
with gr.Row():
index_dropdown = gr.Dropdown(label="Select Index", choices=list_indexes())
refresh_button = gr.Button("Refresh Indexes")
with gr.Tab("Search"):
search_input = gr.Textbox(label="Search Query")
search_k = gr.Slider(1, 10, 5, step=1, label="Number of Results")
search_button = gr.Button("Search")
search_output = gr.JSON(label="Search Results")
with gr.Tab("Question Answering"):
qa_input = gr.Textbox(label="Question")
qa_k = gr.Slider(1, 10, 5, step=1, label="Number of Contexts to Consider")
qa_button = gr.Button("Ask Question")
qa_output = gr.JSON(label="Answer")
refresh_button.click(refresh_indexes, outputs=index_dropdown)
search_button.click(search_document,
inputs=[index_dropdown, search_input, search_k],
outputs=[search_output, search_input, search_k])
qa_button.click(qa_document,
inputs=[index_dropdown, qa_input, qa_k],
outputs=[qa_output, qa_input, qa_k])
demo.launch() |