Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
|
|
|
|
4 |
|
5 |
API_URL = "https://sendthat.cc"
|
6 |
|
@@ -11,30 +14,50 @@ def list_indexes():
|
|
11 |
response.raise_for_status()
|
12 |
indexes = response.json()
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
else:
|
20 |
-
|
|
|
21 |
except requests.exceptions.RequestException as e:
|
22 |
-
|
23 |
return ["Error fetching indexes"]
|
|
|
|
|
|
|
24 |
|
25 |
def search_document(index_name, query, k):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def qa_document(index_name, question, k):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def refresh_indexes():
|
40 |
indexes = list_indexes()
|
|
|
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 |
|
|
|
14 |
response.raise_for_status()
|
15 |
indexes = response.json()
|
16 |
|
17 |
+
logging.debug(f"API response for indexes: {indexes}")
|
18 |
+
|
19 |
+
if isinstance(indexes, list):
|
20 |
+
if "indexes" in indexes:
|
21 |
+
return ["history", "medical"] # Hardcoded index names
|
22 |
+
elif indexes:
|
23 |
+
return indexes
|
24 |
+
else:
|
25 |
+
return ["No indexes available"]
|
26 |
+
elif isinstance(indexes, dict) and "indexes" in indexes:
|
27 |
+
return indexes["indexes"] if indexes["indexes"] else ["No indexes available"]
|
28 |
else:
|
29 |
+
logging.warning(f"Unexpected response format: {indexes}")
|
30 |
+
return ["history", "medical"] # Fallback to hardcoded index names
|
31 |
except requests.exceptions.RequestException as e:
|
32 |
+
logging.error(f"Error fetching indexes: {e}")
|
33 |
return ["Error fetching indexes"]
|
34 |
+
except json.JSONDecodeError as e:
|
35 |
+
logging.error(f"Error decoding JSON: {e}")
|
36 |
+
return ["Error decoding response"]
|
37 |
|
38 |
def search_document(index_name, query, k):
|
39 |
+
try:
|
40 |
+
url = f"{API_URL}/search/{index_name}"
|
41 |
+
payload = {"text": query, "k": k}
|
42 |
+
headers = {"Content-Type": "application/json"}
|
43 |
+
response = requests.post(url, json=payload, headers=headers)
|
44 |
+
response.raise_for_status()
|
45 |
+
return response.json(), "", k
|
46 |
+
except requests.exceptions.RequestException as e:
|
47 |
+
logging.error(f"Error in search: {e}")
|
48 |
+
return {"error": str(e)}, query, k
|
49 |
|
50 |
def qa_document(index_name, question, k):
|
51 |
+
try:
|
52 |
+
url = f"{API_URL}/qa/{index_name}"
|
53 |
+
payload = {"text": question, "k": k}
|
54 |
+
headers = {"Content-Type": "application/json"}
|
55 |
+
response = requests.post(url, json=payload, headers=headers)
|
56 |
+
response.raise_for_status()
|
57 |
+
return response.json(), "", k
|
58 |
+
except requests.exceptions.RequestException as e:
|
59 |
+
logging.error(f"Error in QA: {e}")
|
60 |
+
return {"error": str(e)}, question, k
|
61 |
|
62 |
def refresh_indexes():
|
63 |
indexes = list_indexes()
|