ziyadsuper2017 commited on
Commit
e4e897b
·
1 Parent(s): 96d0abe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -58,9 +58,12 @@ st.title("Gemini API Chatbot")
58
  # Get chat history from session state
59
  chat_history = st.session_state.get("chat_history", [])
60
 
 
 
 
61
  # Display previous messages
62
  for message in chat_history:
63
- role, text = message["role"], message["content"]
64
  st.markdown(f"**{role.title()}:** {text}")
65
 
66
  # Get user input from text box
@@ -69,12 +72,12 @@ user_input = st.text_input("You")
69
  # Check if user input is not empty
70
  if user_input:
71
  # Add user message to chat history
72
- chat_history.append({"role": "user", "content": user_input})
73
 
74
  # Display user message with markdown
75
  st.markdown(f"**You:** {user_input}")
76
 
77
- # Get model response with generate_content method
78
  with st.spinner("Thinking..."):
79
  response = model.generate_content(chat_history)
80
 
@@ -82,7 +85,7 @@ if user_input:
82
  response_text = response.result.text
83
 
84
  # Add response message to chat history
85
- chat_history.append({"role": "assistant", "content": response_text})
86
 
87
  # Display response message with markdown
88
  st.markdown(f"**Gemini Bot:** {response_text}")
@@ -108,7 +111,7 @@ if st.button("Clear History"):
108
 
109
  # Save chat history to database
110
  for message in chat_history:
111
- c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["content"]))
112
  conn.commit()
113
 
114
  # Close the connection
 
58
  # Get chat history from session state
59
  chat_history = st.session_state.get("chat_history", [])
60
 
61
+ # Convert chat history to the expected format
62
+ chat_history = [{"role": message["role"], "parts": [{"text": message["content"]}]} for message in chat_history]
63
+
64
  # Display previous messages
65
  for message in chat_history:
66
+ role, text = message["role"], message["parts"][0]["text"]
67
  st.markdown(f"**{role.title()}:** {text}")
68
 
69
  # Get user input from text box
 
72
  # Check if user input is not empty
73
  if user_input:
74
  # Add user message to chat history
75
+ chat_history.append({"role": "user", "parts": [{"text": user_input}]})
76
 
77
  # Display user message with markdown
78
  st.markdown(f"**You:** {user_input}")
79
 
80
+ # Get model response with generate_content method```
81
  with st.spinner("Thinking..."):
82
  response = model.generate_content(chat_history)
83
 
 
85
  response_text = response.result.text
86
 
87
  # Add response message to chat history
88
+ chat_history.append({"role": "assistant", "parts": [{"text": response_text}]})
89
 
90
  # Display response message with markdown
91
  st.markdown(f"**Gemini Bot:** {response_text}")
 
111
 
112
  # Save chat history to database
113
  for message in chat_history:
114
+ c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["parts"][0]["text"]))
115
  conn.commit()
116
 
117
  # Close the connection