ariankhalfani commited on
Commit
feca159
·
verified ·
1 Parent(s): b2a97bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -21,21 +21,14 @@ def add_message_to_conversation(user_message, bot_message, model_name):
21
  st.session_state.conversation = []
22
  st.session_state.conversation.append((user_message, bot_message, model_name))
23
 
24
- def initialize_memory():
25
- if "memory" not in st.session_state:
26
- st.session_state.memory = {"user_inputs": [], "bot_responses": []}
27
-
28
- def update_memory(user_message, bot_message):
29
- st.session_state.memory["user_inputs"].append(user_message)
30
- st.session_state.memory["bot_responses"].append(bot_message)
31
-
32
  # Streamlit app
33
  st.set_page_config(page_title="Gemma 27B-it Chatbot Interface", layout="wide")
34
  st.title("Gemma 27B-it Chatbot Interface")
35
  st.write("Gemma 27B-it Chatbot Interface")
36
 
37
- # Initialize session state for conversation and memory
38
- initialize_memory()
 
39
 
40
  # User input for question
41
  question = st.text_input("Question", placeholder="Enter your question here...")
@@ -44,12 +37,18 @@ question = st.text_input("Question", placeholder="Enter your question here...")
44
  if st.button("Send") and question:
45
  try:
46
  with st.spinner("Waiting for the model to respond..."):
47
- # Construct the chat history including memory
48
- chat_history = " ".join(st.session_state.memory["bot_responses"][-5:]) + f"User: {question}\n"
49
  response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
50
- answer = response.get("generated_text", "No response")
 
 
 
 
 
 
 
51
  add_message_to_conversation(question, answer, "Gemma-2-27B-it")
52
- update_memory(question, answer)
53
  except ValueError as e:
54
  st.error(str(e))
55
 
@@ -85,9 +84,8 @@ st.markdown(
85
  )
86
 
87
  # Display the conversation
88
- if "conversation" in st.session_state:
89
- st.write('<div class="chat-container">', unsafe_allow_html=True)
90
- for user_message, bot_message, model_name in st.session_state.conversation:
91
- st.write(f'<div class="chat-bubble user">You: {user_message}</div>', unsafe_allow_html=True)
92
- st.write(f'<div class="chat-bubble bot">{model_name}: {bot_message}</div>', unsafe_allow_html=True)
93
- st.write('</div>', unsafe_allow_html=True)
 
21
  st.session_state.conversation = []
22
  st.session_state.conversation.append((user_message, bot_message, model_name))
23
 
 
 
 
 
 
 
 
 
24
  # Streamlit app
25
  st.set_page_config(page_title="Gemma 27B-it Chatbot Interface", layout="wide")
26
  st.title("Gemma 27B-it Chatbot Interface")
27
  st.write("Gemma 27B-it Chatbot Interface")
28
 
29
+ # Initialize session state for conversation
30
+ if "conversation" not in st.session_state:
31
+ st.session_state.conversation = []
32
 
33
  # User input for question
34
  question = st.text_input("Question", placeholder="Enter your question here...")
 
37
  if st.button("Send") and question:
38
  try:
39
  with st.spinner("Waiting for the model to respond..."):
40
+ # Construct the chat history
41
+ chat_history = " ".join([msg[1] for msg in st.session_state.conversation[-5:]]) + f"User: {question}\n"
42
  response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
43
+
44
+ if isinstance(response, list):
45
+ answer = response[0].get("generated_text", "No response")
46
+ elif isinstance(response, dict):
47
+ answer = response.get("generated_text", "No response")
48
+ else:
49
+ answer = "No response"
50
+
51
  add_message_to_conversation(question, answer, "Gemma-2-27B-it")
 
52
  except ValueError as e:
53
  st.error(str(e))
54
 
 
84
  )
85
 
86
  # Display the conversation
87
+ st.write('<div class="chat-container">', unsafe_allow_html=True)
88
+ for user_message, bot_message, model_name in st.session_state.conversation:
89
+ st.write(f'<div class="chat-bubble user">You: {user_message}</div>', unsafe_allow_html=True)
90
+ st.write(f'<div class="chat-bubble bot">{model_name}: {bot_message}</div>', unsafe_allow_html=True)
91
+ st.write('</div>', unsafe_allow_html=True)