File size: 2,378 Bytes
fa51ed6 7b5318b fa51ed6 7b5318b fa51ed6 7c2021a fa51ed6 7b5318b fa51ed6 |
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 |
import requests
import streamlit as st
def get_api_response(question, session_id, model):
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
data = {
"question": question,
"model": model
}
if session_id:
data["session_id"] = session_id
try:
response = requests.post("https://vishnuramdebyez-fastapi.hf.space/chat", headers=headers, json=data)
if response.status_code == 200:
return response.json()
else:
st.error(f"API request failed with status code {response.status_code}: {response.text}")
return None
except Exception as e:
st.error(f"An error occurred: {str(e)}")
return None
def upload_document(file):
print("Uploading file...")
try:
files = {"file": (file.name, file, file.type)}
response = requests.post("https://vishnuramdebyez-fastapi.hf.space/upload-doc", files=files)
if response.status_code == 200:
return response.json()
else:
st.error(f"Failed to upload file. Error: {response.status_code} - {response.text}")
return None
except Exception as e:
st.error(f"An error occurred while uploading the file: {str(e)}")
return None
def list_documents():
try:
response = requests.get("http://localhost:8501/list-docs")
if response.status_code == 200:
return response.json()
else:
st.error(f"Failed to fetch document list. Error: {response.status_code} - {response.text}")
return []
except Exception as e:
st.error(f"An error occurred while fetching the document list: {str(e)}")
return []
def delete_document(file_id):
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
data = {"file_id": file_id}
try:
response = requests.post("https://vishnuramdebyez-fastapi.hf.space/delete-doc", headers=headers, json=data)
if response.status_code == 200:
return response.json()
else:
st.error(f"Failed to delete document. Error: {response.status_code} - {response.text}")
return None
except Exception as e:
st.error(f"An error occurred while deleting the document: {str(e)}")
return None |