Upload 3 files
Browse files- .streamlit/config.toml +6 -0
- app.py +91 -0
- requirements.txt +2 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
primaryColor="#F63366"
|
3 |
+
backgroundColor="#24243E"
|
4 |
+
secondaryBackgroundColor="#0F0C29"
|
5 |
+
textColor="#FFFFFF"
|
6 |
+
font="sans serif"
|
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import streamlit as st
|
3 |
+
import json
|
4 |
+
from streamlit_chat import message
|
5 |
+
|
6 |
+
st.title("GPTBase")
|
7 |
+
|
8 |
+
def clear_submit():
|
9 |
+
st.session_state["submit"] = False
|
10 |
+
|
11 |
+
def set_api_key(api_key: str):
|
12 |
+
st.session_state["API_KEY"] = api_key
|
13 |
+
|
14 |
+
def set_ai_id(ai_id: str):
|
15 |
+
st.session_state["ai_id"] = ai_id
|
16 |
+
|
17 |
+
# Sidebar
|
18 |
+
index = None
|
19 |
+
doc = None
|
20 |
+
with st.sidebar:
|
21 |
+
user_secret = st.text_input(
|
22 |
+
"API Key",
|
23 |
+
type="password",
|
24 |
+
placeholder="Paste your API key here (ak-...)",
|
25 |
+
help="You can get your API key from https://mygpt.felo.me/api-keys.",
|
26 |
+
value=st.session_state.get("API_KEY", ""),
|
27 |
+
)
|
28 |
+
if user_secret:
|
29 |
+
set_api_key(user_secret)
|
30 |
+
|
31 |
+
user_ai_id = st.text_input(
|
32 |
+
"AI Id",
|
33 |
+
placeholder="Paste your AI Id here",
|
34 |
+
value=st.session_state.get("ai_id", ""),
|
35 |
+
)
|
36 |
+
if user_ai_id:
|
37 |
+
set_ai_id(user_ai_id)
|
38 |
+
|
39 |
+
uploaded_file = st.file_uploader(
|
40 |
+
"Upload a pdf, docx, or txt file",
|
41 |
+
type=["pdf", "docx", "txt"],
|
42 |
+
help="Scanned documents are not supported yet!",
|
43 |
+
on_change=clear_submit,
|
44 |
+
)
|
45 |
+
api_key = st.session_state.get("API_KEY")
|
46 |
+
ai_id = st.session_state.get("ai_id")
|
47 |
+
if not api_key or not ai_id:
|
48 |
+
st.warning("Please enter your API key and AI Id to upload a file.")
|
49 |
+
|
50 |
+
if uploaded_file is not None and "API_KEY" in st.session_state and "ai_id" in st.session_state:
|
51 |
+
|
52 |
+
file = {"file": uploaded_file}
|
53 |
+
url = f'https://mygpt.felo.me/api/v1/ais/{ai_id}/files'
|
54 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
55 |
+
response = requests.post(url, headers=headers, files=file)
|
56 |
+
result = response.json()
|
57 |
+
if response.status_code != 200:
|
58 |
+
st.text(result['detail'])
|
59 |
+
|
60 |
+
tab1, tab2 = st.tabs(["Introduce", "Ask AI"])
|
61 |
+
with tab1:
|
62 |
+
st.markdown("### Use steps")
|
63 |
+
st.write('1.Please fill in your API Key and AI Id.')
|
64 |
+
st.write('2.Upload the file you want to get the answer.')
|
65 |
+
st.write('3.You can ask AI with the file.')
|
66 |
+
# st.markdown("""---""")
|
67 |
+
# st.write('If you have any questions or suggestions, please visit: ')
|
68 |
+
|
69 |
+
with tab2:
|
70 |
+
if not api_key or not ai_id:
|
71 |
+
st.write('Please fill in your API Key and AI Id.')
|
72 |
+
st.header("Ask AI some questions about the file you uploaded:")
|
73 |
+
|
74 |
+
def get_text():
|
75 |
+
input_text = st.text_area("You:", on_change=clear_submit)
|
76 |
+
return input_text
|
77 |
+
user_input = get_text()
|
78 |
+
button = st.button("Submit")
|
79 |
+
|
80 |
+
if button or st.session_state.get("submit"):
|
81 |
+
if api_key and ai_id and user_input:
|
82 |
+
url = f'https://mygpt.felo.me/api/v1/question/{ai_id}'
|
83 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
84 |
+
data = {"question": user_input}
|
85 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
86 |
+
result = response.json()
|
87 |
+
if response.status_code != 200:
|
88 |
+
st.text(result['detail'])
|
89 |
+
else:
|
90 |
+
message(result['answer'])
|
91 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
streamlit_chat
|