gptbase commited on
Commit
066960c
·
1 Parent(s): 8bd6cdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -2,6 +2,7 @@ import requests
2
  import streamlit as st
3
  import json
4
  from streamlit_chat import message
 
5
 
6
  st.title("GPTBase")
7
 
@@ -14,10 +15,6 @@ def set_api_key(api_key: str):
14
  def set_ai_id(ai_id: str):
15
  st.session_state["ai_id"] = ai_id
16
 
17
- def clear_upload():
18
- st.session_state["submit"] = False
19
- st.session_state['is_upload'] = False
20
-
21
  # Sidebar
22
  with st.sidebar:
23
  user_secret = st.text_input(
@@ -42,7 +39,7 @@ with st.sidebar:
42
  "Upload a pdf, docx, or txt file",
43
  type=["pdf", "docx", "txt"],
44
  help="Scanned documents are not supported yet!",
45
- on_change=clear_upload,
46
  )
47
  api_key = st.session_state.get("API_KEY")
48
  ai_id = st.session_state.get("ai_id")
@@ -50,16 +47,20 @@ with st.sidebar:
50
  st.warning("Please enter your API key and AI Id to upload a file.")
51
 
52
  if uploaded_file is not None and "API_KEY" in st.session_state and "ai_id" in st.session_state:
53
- if not st.session_state.get("is_upload"):
54
- file = {"file": uploaded_file}
55
- url = f'https://mygpt.felo.me/api/v1/ais/{ai_id}/files'
56
- headers = {"Authorization": f"Bearer {api_key}"}
57
- try:
58
- response = requests.post(url, headers=headers, files=file)
59
- print("upload")
60
- except requests.exceptions.HTTPError as err:
61
- st.text("Upload failed, please try again")
62
- st.session_state['is_upload'] = True
 
 
 
 
63
 
64
  tab1, tab2 = st.tabs(["Introduce", "Ask AI"])
65
  with tab1:
@@ -83,24 +84,19 @@ with tab2:
83
  if button or st.session_state.get("submit"):
84
  if api_key and ai_id and user_input:
85
  st.session_state["submit"] = True
86
- st.session_state['is_upload'] = False
87
- uploaded_file = None
88
  question_url = f'https://mygpt.felo.me/api/v1/question/{ai_id}'
89
  headers = {"Authorization": f"Bearer {api_key}"}
90
  data = {"question": user_input}
91
  # response = requests.post(question_url, headers=headers, data=json.dumps(data))
92
  try:
93
  response = requests.post(question_url, headers=headers, data=json.dumps(data))
94
- result = response.json()
95
- message(result['answer'])
96
- # message(result['answer'])
 
 
97
  response.raise_for_status() # Raises an HTTPError if the response contains an error status code.
98
  except requests.exceptions.HTTPError as err:
99
- print(f"Failed to ask, please try again.")
100
-
101
- # result = response.json()
102
- # if response.status_code != 200:
103
- # st.text(result['detail'])
104
- # else:
105
- # message(result['answer'])
106
-
 
2
  import streamlit as st
3
  import json
4
  from streamlit_chat import message
5
+ from utils import upload_file
6
 
7
  st.title("GPTBase")
8
 
 
15
  def set_ai_id(ai_id: str):
16
  st.session_state["ai_id"] = ai_id
17
 
 
 
 
 
18
  # Sidebar
19
  with st.sidebar:
20
  user_secret = st.text_input(
 
39
  "Upload a pdf, docx, or txt file",
40
  type=["pdf", "docx", "txt"],
41
  help="Scanned documents are not supported yet!",
42
+ on_change=clear_submit,
43
  )
44
  api_key = st.session_state.get("API_KEY")
45
  ai_id = st.session_state.get("ai_id")
 
47
  st.warning("Please enter your API key and AI Id to upload a file.")
48
 
49
  if uploaded_file is not None and "API_KEY" in st.session_state and "ai_id" in st.session_state:
50
+ res = upload_file(uploaded_file, api_key, ai_id)
51
+ if not res:
52
+ st.text("Upload failed, please try again")
53
+ # if not st.session_state.get("is_upload"):
54
+ # file = {"file": uploaded_file}
55
+ # url = f'https://mygpt.felo.me/api/v1/ais/{ai_id}/files'
56
+ # headers = {"Authorization": f"Bearer {api_key}"}
57
+ # try:
58
+ # response = requests.post(url, headers=headers, files=file)
59
+ # # print("upload")
60
+ # except requests.exceptions.HTTPError as err:
61
+ # st.text("Upload failed, please try again")
62
+
63
+ # st.session_state['is_upload'] = True
64
 
65
  tab1, tab2 = st.tabs(["Introduce", "Ask AI"])
66
  with tab1:
 
84
  if button or st.session_state.get("submit"):
85
  if api_key and ai_id and user_input:
86
  st.session_state["submit"] = True
 
 
87
  question_url = f'https://mygpt.felo.me/api/v1/question/{ai_id}'
88
  headers = {"Authorization": f"Bearer {api_key}"}
89
  data = {"question": user_input}
90
  # response = requests.post(question_url, headers=headers, data=json.dumps(data))
91
  try:
92
  response = requests.post(question_url, headers=headers, data=json.dumps(data))
93
+ if response.status_code == 200:
94
+ result = response.json()
95
+ message(result['answer'])
96
+ else:
97
+ print(f"Failed to ask, please try again.")
98
  response.raise_for_status() # Raises an HTTPError if the response contains an error status code.
99
  except requests.exceptions.HTTPError as err:
100
+ print(f"Failed to ask, please try again.:{err}")
101
+ except requests.exceptions.RequestException as e:
102
+ print(e)