garyd1 commited on
Commit
a246ded
·
verified ·
1 Parent(s): a503c00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -18,14 +18,16 @@ st.set_page_config(page_title="Enhanced Gemini Q&A App", layout="wide")
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
  # Function to get response from Gemini API
23
  def get_gemini_response(question):
24
  # Reformat the session chat history to match the API requirements
25
- formatted_history = [
26
- {"parts": [{"text": chat["user"]}], "role": "user"} if "user" in chat else {"parts": [{"text": chat["bot"]}], "role": "bot"}
27
- for chat in st.session_state.chat_history
28
- ]
 
 
 
29
  chat = model.start_chat(history=formatted_history) # Use the correctly formatted history
30
  try:
31
  response = chat.send_message(question, stream=True)
@@ -37,7 +39,6 @@ def get_gemini_response(question):
37
  except Exception as e:
38
  return f"Error: {str(e)}"
39
 
40
-
41
  # UI Layout
42
  st.title("🤖 Gemini AI - Interactive Q&A")
43
  st.write("Ask me anything and I'll try to answer!")
 
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
+ for chat in st.session_state.chat_history:
26
+ if "user" in chat:
27
+ formatted_history.append({"parts": [{"text": chat["user"]}], "role": "user"})
28
+ else:
29
+ formatted_history.append({"parts": [{"text": chat["bot"]}], "role": "bot"})
30
+
31
  chat = model.start_chat(history=formatted_history) # Use the correctly formatted history
32
  try:
33
  response = chat.send_message(question, stream=True)
 
39
  except Exception as e:
40
  return f"Error: {str(e)}"
41
 
 
42
  # UI Layout
43
  st.title("🤖 Gemini AI - Interactive Q&A")
44
  st.write("Ask me anything and I'll try to answer!")