Create api_utils.py
Browse files- api_utils.py +69 -0
api_utils.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
def get_api_response(question, session_id, model):
|
| 5 |
+
headers = {
|
| 6 |
+
'accept': 'application/json',
|
| 7 |
+
'Content-Type': 'application/json'
|
| 8 |
+
}
|
| 9 |
+
data = {
|
| 10 |
+
"question": question,
|
| 11 |
+
"model": model
|
| 12 |
+
}
|
| 13 |
+
if session_id:
|
| 14 |
+
data["session_id"] = session_id
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
response = requests.post("http://localhost:8000/chat", headers=headers, json=data)
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
return response.json()
|
| 20 |
+
else:
|
| 21 |
+
st.error(f"API request failed with status code {response.status_code}: {response.text}")
|
| 22 |
+
return None
|
| 23 |
+
except Exception as e:
|
| 24 |
+
st.error(f"An error occurred: {str(e)}")
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
def upload_document(file):
|
| 28 |
+
print("Uploading file...")
|
| 29 |
+
try:
|
| 30 |
+
files = {"file": (file.name, file, file.type)}
|
| 31 |
+
response = requests.post("http://localhost:8000/upload-doc", files=files)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
return response.json()
|
| 34 |
+
else:
|
| 35 |
+
st.error(f"Failed to upload file. Error: {response.status_code} - {response.text}")
|
| 36 |
+
return None
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"An error occurred while uploading the file: {str(e)}")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
def list_documents():
|
| 42 |
+
try:
|
| 43 |
+
response = requests.get("http://localhost:8000/list-docs")
|
| 44 |
+
if response.status_code == 200:
|
| 45 |
+
return response.json()
|
| 46 |
+
else:
|
| 47 |
+
st.error(f"Failed to fetch document list. Error: {response.status_code} - {response.text}")
|
| 48 |
+
return []
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"An error occurred while fetching the document list: {str(e)}")
|
| 51 |
+
return []
|
| 52 |
+
|
| 53 |
+
def delete_document(file_id):
|
| 54 |
+
headers = {
|
| 55 |
+
'accept': 'application/json',
|
| 56 |
+
'Content-Type': 'application/json'
|
| 57 |
+
}
|
| 58 |
+
data = {"file_id": file_id}
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
response = requests.post("http://localhost:8000/delete-doc", headers=headers, json=data)
|
| 62 |
+
if response.status_code == 200:
|
| 63 |
+
return response.json()
|
| 64 |
+
else:
|
| 65 |
+
st.error(f"Failed to delete document. Error: {response.status_code} - {response.text}")
|
| 66 |
+
return None
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"An error occurred while deleting the document: {str(e)}")
|
| 69 |
+
return None
|