slimshadow commited on
Commit
5e5d24f
·
verified ·
1 Parent(s): 8839c6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -3,7 +3,7 @@ import requests
3
  import json
4
 
5
  # Function to call the API
6
- def call_chatgpt_api(message):
7
  url = "https://api.safone.dev/chatgpt"
8
  headers = {
9
  "accept": "application/json",
@@ -13,7 +13,7 @@ def call_chatgpt_api(message):
13
  "message": message,
14
  "version": 3,
15
  "chat_mode": "assistant",
16
- "dialog_messages": "[{\"bot\":\"\",\"user\":\"\"}]"
17
  }
18
 
19
  response = requests.post(url, headers=headers, data=json.dumps(payload))
@@ -22,21 +22,39 @@ def call_chatgpt_api(message):
22
  else:
23
  return None
24
 
 
 
 
 
 
 
 
25
  # Streamlit App
26
  st.title('SlimshadowGPT')
27
  st.write("This is a simple interface to interact with the GPT-3.5 API.")
28
 
29
- user_input = st.text_input("Enter your message:")
30
 
31
  if st.button('Send'):
32
  if user_input:
 
 
33
  with st.spinner('Generating response...'):
34
- response = call_chatgpt_api(user_input)
35
  if response:
36
- st.write("Assistant:", response['choices'][0]['message']['content'])
 
 
37
  else:
38
  st.write("Failed to get a response from the API.")
39
- else:
40
- st.write("Please enter a message to send.")
 
 
 
 
 
 
 
41
 
42
  # Run the app using `streamlit run app.py` in the terminal
 
3
  import json
4
 
5
  # Function to call the API
6
+ def call_chatgpt_api(message, dialog_messages):
7
  url = "https://api.safone.dev/chatgpt"
8
  headers = {
9
  "accept": "application/json",
 
13
  "message": message,
14
  "version": 3,
15
  "chat_mode": "assistant",
16
+ "dialog_messages": json.dumps(dialog_messages)
17
  }
18
 
19
  response = requests.post(url, headers=headers, data=json.dumps(payload))
 
22
  else:
23
  return None
24
 
25
+ # Initialize session state if not already done
26
+ if 'dialog_messages' not in st.session_state:
27
+ st.session_state.dialog_messages = []
28
+
29
+ if 'responses' not in st.session_state:
30
+ st.session_state.responses = []
31
+
32
  # Streamlit App
33
  st.title('SlimshadowGPT')
34
  st.write("This is a simple interface to interact with the GPT-3.5 API.")
35
 
36
+ user_input = st.text_input("Enter your message:", key='user_input')
37
 
38
  if st.button('Send'):
39
  if user_input:
40
+ st.session_state.dialog_messages.append({"bot": "", "user": user_input})
41
+
42
  with st.spinner('Generating response...'):
43
+ response = call_chatgpt_api(user_input, st.session_state.dialog_messages)
44
  if response:
45
+ assistant_message = response['choices'][0]['message']['content']
46
+ st.session_state.dialog_messages.append({"bot": assistant_message, "user": ""})
47
+ st.session_state.responses.append((user_input, assistant_message))
48
  else:
49
  st.write("Failed to get a response from the API.")
50
+
51
+ # Clear input box after sending
52
+ st.session_state.user_input = ""
53
+
54
+ # Display the conversation history
55
+ st.write("### Chat History")
56
+ for user_msg, bot_msg in st.session_state.responses:
57
+ st.write(f"**You:** {user_msg}")
58
+ st.write(f"**Assistant:** {bot_msg}")
59
 
60
  # Run the app using `streamlit run app.py` in the terminal