garyd1 commited on
Commit
efe4ee0
·
verified ·
1 Parent(s): ff2871e

Updated v2

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -12,21 +12,26 @@ genai.configure(api_key=API_KEY)
12
  model = genai.GenerativeModel('gemini-pro')
13
 
14
  # Streamlit Page Config
15
- st.set_page_config(page_title="Gemini Q&A App", layout="wide")
16
 
17
- # Initialize Chat History in Session State
18
  if "chat_history" not in st.session_state:
19
- st.session_state.chat_history = []
20
 
21
  # Function to get response from Gemini API
22
  def get_gemini_response(question):
23
- chat = model.start_chat(history=st.session_state.chat_history)
 
 
 
 
 
24
  try:
25
  response = chat.send_message(question, stream=True)
26
  full_response = ""
27
  for chunk in response:
28
  full_response += chunk.text + " "
29
- st.session_state.chat_history.append({"user": question, "bot": full_response})
30
  return full_response
31
  except Exception as e:
32
  return f"Error: {str(e)}"
 
12
  model = genai.GenerativeModel('gemini-pro')
13
 
14
  # Streamlit Page Config
15
+ st.set_page_config(page_title="Enhanced Gemini Q&A App", layout="wide")
16
 
17
+ # Initialize Chat History in Session State with the Correct Format
18
  if "chat_history" not in st.session_state:
19
+ st.session_state.chat_history = [] # Empty list for chat history
20
 
21
  # Function to get response from Gemini API
22
  def get_gemini_response(question):
23
+ # Reformat the session chat history to match the API requirements
24
+ formatted_history = [
25
+ {"content": chat["user"], "role": "user"} if "user" in chat else {"content": chat["bot"], "role": "bot"}
26
+ for chat in st.session_state.chat_history
27
+ ]
28
+ chat = model.start_chat(history=formatted_history) # Use the formatted history
29
  try:
30
  response = chat.send_message(question, stream=True)
31
  full_response = ""
32
  for chunk in response:
33
  full_response += chunk.text + " "
34
+ st.session_state.chat_history.append({"user": question, "bot": full_response}) # Append to chat history
35
  return full_response
36
  except Exception as e:
37
  return f"Error: {str(e)}"