File size: 3,912 Bytes
a8bba1e
 
 
 
066960c
a8bba1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21653ff
a8bba1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
066960c
a8bba1e
 
 
 
 
 
 
066960c
 
 
 
 
 
 
 
 
 
 
 
 
 
a8bba1e
 
 
 
 
 
9fdcad1
 
 
a8bba1e
 
 
 
 
 
 
 
 
 
 
 
 
9fdcad1
21653ff
a8bba1e
 
9fdcad1
 
 
066960c
 
 
 
 
9fdcad1
 
066960c
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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

# Sidebar
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")
        # if not st.session_state.get("is_upload"):
        #     file = {"file": uploaded_file}
        #     url = f'https://mygpt.felo.me/api/v1/ais/{ai_id}/files'
        #     headers = {"Authorization": f"Bearer {api_key}"}
        #     try:
        #         response = requests.post(url, headers=headers, files=file)
        #         # print("upload")
        #     except requests.exceptions.HTTPError as err:
        #         st.text("Upload failed, please try again")
            
        #     st.session_state['is_upload'] = True

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}
            # response = requests.post(question_url, headers=headers, data=json.dumps(data))
            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()  # Raises an HTTPError if the response contains an error status code.
            except requests.exceptions.HTTPError as err:
                print(f"Failed to ask, please try again.:{err}")
            except requests.exceptions.RequestException as e:
                print(e)