Update api.py
Browse files
api.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import os
|
2 |
import aiohttp
|
3 |
from dotenv import load_dotenv
|
|
|
4 |
|
5 |
load_dotenv()
|
6 |
|
7 |
async def query_rag_api(question):
|
8 |
"""
|
9 |
-
Asynchronously query the RAG
|
10 |
|
11 |
Args:
|
12 |
question (str): The user's question.
|
@@ -15,27 +16,17 @@ async def query_rag_api(question):
|
|
15 |
dict: Answer, contexts, or error message.
|
16 |
|
17 |
Note:
|
18 |
-
Replace API_ENDPOINT with
|
19 |
-
|
20 |
"""
|
21 |
-
# Placeholder - REPLACE with your Space's
|
22 |
-
API_ENDPOINT = "https://huggingface.co/spaces/samim2024/testing
|
23 |
-
|
24 |
-
|
25 |
-
if not api_key:
|
26 |
-
return {"error": "HUGGINGFACEHUB_API_TOKEN not set", "answer": "", "contexts": []}
|
27 |
-
|
28 |
-
headers = {"Content-Type": "application/json"}
|
29 |
-
payload = {"question": question}
|
30 |
|
31 |
try:
|
32 |
async with aiohttp.ClientSession() as session:
|
33 |
-
async with session.
|
34 |
-
API_ENDPOINT,
|
35 |
-
json=payload,
|
36 |
-
headers=headers,
|
37 |
-
timeout=10
|
38 |
-
) as response:
|
39 |
response_dict = await response.json()
|
40 |
if response_dict.get("error"):
|
41 |
return {"error": response_dict["error"], "answer": "", "contexts": []}
|
@@ -44,4 +35,4 @@ async def query_rag_api(question):
|
|
44 |
"contexts": response_dict.get("contexts", [])
|
45 |
}
|
46 |
except Exception as e:
|
47 |
-
return {"error": f"
|
|
|
1 |
import os
|
2 |
import aiohttp
|
3 |
from dotenv import load_dotenv
|
4 |
+
from urllib.parse import urlencode
|
5 |
|
6 |
load_dotenv()
|
7 |
|
8 |
async def query_rag_api(question):
|
9 |
"""
|
10 |
+
Asynchronously query the RAG model via Streamlit endpoint.
|
11 |
|
12 |
Args:
|
13 |
question (str): The user's question.
|
|
|
16 |
dict: Answer, contexts, or error message.
|
17 |
|
18 |
Note:
|
19 |
+
Replace API_ENDPOINT with your Space's URL (e.g., https://<your-space-id>.hf.space)
|
20 |
+
after deployment.
|
21 |
"""
|
22 |
+
# Placeholder - REPLACE with your Space's URL
|
23 |
+
API_ENDPOINT = "https://huggingface.co/spaces/samim2024/testing"
|
24 |
+
params = {"query": question}
|
25 |
+
url = f"{API_ENDPOINT}?{urlencode(params)}"
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
try:
|
28 |
async with aiohttp.ClientSession() as session:
|
29 |
+
async with session.get(url, timeout=10) as response:
|
|
|
|
|
|
|
|
|
|
|
30 |
response_dict = await response.json()
|
31 |
if response_dict.get("error"):
|
32 |
return {"error": response_dict["error"], "answer": "", "contexts": []}
|
|
|
35 |
"contexts": response_dict.get("contexts", [])
|
36 |
}
|
37 |
except Exception as e:
|
38 |
+
return {"error": f"Query failed: {str(e)}", "answer": "", "contexts": []}
|