gptbase commited on
Commit
9fdcad1
·
1 Parent(s): a8bba1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -14,9 +14,11 @@ 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
  # Sidebar
18
- index = None
19
- doc = None
20
  with st.sidebar:
21
  user_secret = st.text_input(
22
  "API Key",
@@ -40,7 +42,7 @@ with st.sidebar:
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")
@@ -48,23 +50,25 @@ with st.sidebar:
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:
@@ -76,16 +80,27 @@ with tab2:
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
 
 
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(
24
  "API Key",
 
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
  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:
66
  st.markdown("### Use steps")
67
  st.write('1.Please fill in your API Key and AI Id.')
68
  st.write('2.Upload the file you want to get the answer.')
69
+ st.write('3.You can ask questions about all the files you have uploaded.')
70
+ st.markdown("""---""")
71
+ st.write('If you have any questions or suggestions, please visit: https://twitter.com/GptBase')
72
 
73
  with tab2:
74
  if not api_key or not ai_id:
 
80
  return input_text
81
  user_input = get_text()
82
  button = st.button("Submit")
 
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