|
import requests |
|
import streamlit as st |
|
import json |
|
from streamlit_chat import message |
|
from utils import upload_file |
|
|
|
st.title("GPTBase") |
|
|
|
def clear_submit(): |
|
st.session_state["submit"] = False |
|
|
|
def set_api_key(api_key: str): |
|
st.session_state["API_KEY"] = api_key |
|
|
|
def set_ai_id(ai_id: str): |
|
st.session_state["ai_id"] = ai_id |
|
|
|
|
|
with st.sidebar: |
|
user_secret = st.text_input( |
|
"API Key", |
|
type="password", |
|
placeholder="Paste your API key here (ak-...)", |
|
help="You can get your API key from https://gptbase.ai/api-keys.", |
|
value=st.session_state.get("API_KEY", ""), |
|
) |
|
if user_secret: |
|
set_api_key(user_secret) |
|
|
|
user_ai_id = st.text_input( |
|
"AI Id", |
|
placeholder="Paste your AI Id here", |
|
value=st.session_state.get("ai_id", ""), |
|
) |
|
if user_ai_id: |
|
set_ai_id(user_ai_id) |
|
|
|
uploaded_file = st.file_uploader( |
|
"Upload a pdf, docx, or txt file", |
|
type=["pdf", "docx", "txt"], |
|
help="Scanned documents are not supported yet!", |
|
on_change=clear_submit, |
|
) |
|
api_key = st.session_state.get("API_KEY") |
|
ai_id = st.session_state.get("ai_id") |
|
if not api_key or not ai_id: |
|
st.warning("Please enter your API key and AI Id to upload a file.") |
|
|
|
if uploaded_file is not None and "API_KEY" in st.session_state and "ai_id" in st.session_state: |
|
res = upload_file(uploaded_file, api_key, ai_id) |
|
if not res: |
|
st.text("Upload failed, please try again") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tab1, tab2 = st.tabs(["Introduce", "Ask AI"]) |
|
with tab1: |
|
st.markdown("### Use steps") |
|
st.write('1.Please fill in your API Key and AI Id.') |
|
st.write('2.Upload the file you want to get the answer.') |
|
st.write('3.You can ask questions about all the files you have uploaded.') |
|
st.markdown("""---""") |
|
st.write('If you have any questions or suggestions, please visit: https://twitter.com/GptBase') |
|
|
|
with tab2: |
|
if not api_key or not ai_id: |
|
st.write('Please fill in your API Key and AI Id.') |
|
st.header("Ask AI some questions about the file you uploaded:") |
|
|
|
def get_text(): |
|
input_text = st.text_area("You:", on_change=clear_submit) |
|
return input_text |
|
user_input = get_text() |
|
button = st.button("Submit") |
|
if button or st.session_state.get("submit"): |
|
if api_key and ai_id and user_input: |
|
st.session_state["submit"] = True |
|
question_url = f'https://gptbase.ai/api/v1/question/{ai_id}' |
|
headers = {"Authorization": f"Bearer {api_key}"} |
|
data = {"question": user_input} |
|
|
|
try: |
|
response = requests.post(question_url, headers=headers, data=json.dumps(data)) |
|
if response.status_code == 200: |
|
result = response.json() |
|
message(result['answer']) |
|
else: |
|
print(f"Failed to ask, please try again.") |
|
response.raise_for_status() |
|
except requests.exceptions.HTTPError as err: |
|
print(f"Failed to ask, please try again.:{err}") |
|
except requests.exceptions.RequestException as e: |
|
print(e) |
|
|