muzammil-eds commited on
Commit
3cf11b7
·
1 Parent(s): 48a1a29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -49
app.py CHANGED
@@ -2,12 +2,9 @@ import streamlit as st
2
  import os
3
  import pickle
4
  import time
5
- import tempfile
6
- import os
7
-
8
- import requests
9
-
10
 
 
11
 
12
  st.markdown(
13
  """
@@ -18,21 +15,13 @@ st.markdown(
18
  font-weight: bold;
19
  }
20
  </style>
21
- <div class="title">🧠 Phsycadalleics Chatbot</div>
22
- """,
23
  unsafe_allow_html=True
24
  )
25
- if 'chat_history' not in st.session_state:
26
- st.session_state.chat_history = [] # Define chat_history in the session state
27
-
28
  # Load and Save Conversations
29
  conversations_file = "conversations.pkl"
30
 
31
- if not os.path.exists(conversations_file):
32
- # Create the file
33
- with open(conversations_file, 'w') as f:
34
- pass # The file is created with this line, no need to write anything to it
35
-
36
 
37
  @st.cache_data
38
  def load_conversations():
@@ -42,8 +31,9 @@ def load_conversations():
42
  except (FileNotFoundError, EOFError):
43
  return []
44
 
 
45
  def save_conversations(conversations):
46
- temp_conversations_file = "temp_" + conversations_file
47
  with open(temp_conversations_file, "wb") as f:
48
  pickle.dump(conversations, f)
49
  os.replace(temp_conversations_file, conversations_file)
@@ -69,7 +59,6 @@ def display_chats_sidebar():
69
  if col1.button('Start New Chat', key="new_chat"):
70
  st.session_state.current_conversation = []
71
  st.session_state.conversations.append(st.session_state.current_conversation)
72
- st.session_state.chat_history = []
73
 
74
  with col2:
75
  if col2.button('Clear All Chats', key="clear_all"):
@@ -84,58 +73,44 @@ def display_chats_sidebar():
84
  chat_title = truncate_string(chat_title_raw)
85
  if st.sidebar.button(f"{chat_title}", key=f"chat_button_{idx}"):
86
  st.session_state.current_conversation = st.session_state.conversations[idx]
87
-
88
-
89
- def dummy_response(prompt_input, chat_history):
90
- api_url = 'http://b5c9-34-125-138-225.ngrok.io/api/chat'
91
-
92
- data_payload = {
93
- 'query': prompt_input,
94
- 'chat_history': chat_history
95
- }
96
-
97
- response = requests.post(api_url, json=data_payload)
98
 
99
- if response.status_code == 200:
100
- response_data = response.json()
101
- result = response_data.get('response')
102
- st.session_state.chat_history = response_data.get('chat_history')
103
- else:
104
- # Handle the case where the API does not return a successful response
105
- result = f"An error occurred: {response.text}"
106
-
107
- return result, chat_history
108
-
109
-
110
 
111
  def main_app():
112
  for message in st.session_state.current_conversation:
113
  with st.chat_message(message["role"]):
114
  st.write(message["content"])
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  if prompt := st.chat_input('Send a Message'):
117
  st.session_state.current_conversation.append({"role": "user", "content": prompt})
118
- print(' PROMPT :', prompt)
119
  with st.chat_message("user"):
120
  st.write(prompt)
121
 
122
  with st.chat_message("assistant"):
123
  with st.spinner("Thinking..."):
124
-
125
-
126
- response, st.session_state.chat_history = dummy_response(prompt, st.session_state.chat_history)
127
- full_response = ''
128
  placeholder = st.empty()
 
129
  for item in response:
130
  full_response += item
131
  time.sleep(0.003)
132
  placeholder.markdown(full_response)
133
  placeholder.markdown(full_response)
134
- print(' RESPONSE :', full_response)
135
-
136
- st.session_state.current_conversation.append({"role": "assistant", "content": response})
137
  save_conversations(st.session_state.conversations)
138
-
139
-
140
  display_chats_sidebar()
141
  main_app()
 
2
  import os
3
  import pickle
4
  import time
5
+ import g4f
 
 
 
 
6
 
7
+ st.set_page_config(page_title="MEDICAL ASSISTANT")
8
 
9
  st.markdown(
10
  """
 
15
  font-weight: bold;
16
  }
17
  </style>
18
+ <div class="title">🧠 MEDICAL ASSISTANT</div>
19
+ """,
20
  unsafe_allow_html=True
21
  )
 
 
 
22
  # Load and Save Conversations
23
  conversations_file = "conversations.pkl"
24
 
 
 
 
 
 
25
 
26
  @st.cache_data
27
  def load_conversations():
 
31
  except (FileNotFoundError, EOFError):
32
  return []
33
 
34
+
35
  def save_conversations(conversations):
36
+ temp_conversations_file = conversations_file
37
  with open(temp_conversations_file, "wb") as f:
38
  pickle.dump(conversations, f)
39
  os.replace(temp_conversations_file, conversations_file)
 
59
  if col1.button('Start New Chat', key="new_chat"):
60
  st.session_state.current_conversation = []
61
  st.session_state.conversations.append(st.session_state.current_conversation)
 
62
 
63
  with col2:
64
  if col2.button('Clear All Chats', key="clear_all"):
 
73
  chat_title = truncate_string(chat_title_raw)
74
  if st.sidebar.button(f"{chat_title}", key=f"chat_button_{idx}"):
75
  st.session_state.current_conversation = st.session_state.conversations[idx]
 
 
 
 
 
 
 
 
 
 
 
76
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  def main_app():
79
  for message in st.session_state.current_conversation:
80
  with st.chat_message(message["role"]):
81
  st.write(message["content"])
82
 
83
+ def generate_response(prompt_input):
84
+ string_dialogue = "You are a helpful Medical Assistant. You will Only respond to Medical related Queries. Say Sorry to any other Type of Queries."
85
+ for dict_message in st.session_state.current_conversation:
86
+ string_dialogue += dict_message["role"].capitalize() + ": " + dict_message["content"] + "\\n\\n"
87
+
88
+ prompt = f"{string_dialogue}\n {prompt_input} Assistant: "
89
+ response_generator = g4f.ChatCompletion.create(
90
+ model="gpt-3.5-turbo",
91
+ messages=[{"role": "user", "content": prompt}],
92
+ stream=True,
93
+ )
94
+ return response_generator
95
+
96
  if prompt := st.chat_input('Send a Message'):
97
  st.session_state.current_conversation.append({"role": "user", "content": prompt})
 
98
  with st.chat_message("user"):
99
  st.write(prompt)
100
 
101
  with st.chat_message("assistant"):
102
  with st.spinner("Thinking..."):
103
+ response = generate_response(prompt)
 
 
 
104
  placeholder = st.empty()
105
+ full_response = ''
106
  for item in response:
107
  full_response += item
108
  time.sleep(0.003)
109
  placeholder.markdown(full_response)
110
  placeholder.markdown(full_response)
111
+ st.session_state.current_conversation.append({"role": "assistant", "content": full_response})
 
 
112
  save_conversations(st.session_state.conversations)
113
+
114
+
115
  display_chats_sidebar()
116
  main_app()