Spaces:
Runtime error
Runtime error
ziyadsuper2017
commited on
Commit
•
8f02c87
1
Parent(s):
2d04232
modification
Browse files
app.py
CHANGED
@@ -30,7 +30,7 @@ chat_history = st.session_state.get("chat_history", [])
|
|
30 |
# Display previous messages
|
31 |
for message in chat_history:
|
32 |
role, text = message
|
33 |
-
st.markdown(f"**{role}:** {text}")
|
34 |
|
35 |
# Get user input from text box
|
36 |
user_input = st.text_input("You")
|
@@ -38,23 +38,26 @@ user_input = st.text_input("You")
|
|
38 |
# Check if user input is not empty
|
39 |
if user_input:
|
40 |
# Add user message to chat history
|
41 |
-
chat_history.append(
|
|
|
|
|
|
|
42 |
|
43 |
# Create model object
|
44 |
model = genai.GenerativeModel(model_name="gemini-pro")
|
45 |
|
46 |
-
# Prepare chat history for generate_content method
|
47 |
-
genai_chat_history = [{"role": role, "content": text} for role, text in chat_history]
|
48 |
-
|
49 |
# Get model response with generate_content method
|
50 |
with st.spinner("Thinking..."):
|
51 |
-
response = model.generate_content(
|
52 |
|
53 |
# Get response text from response object
|
54 |
response_text = response.text
|
55 |
|
56 |
# Add response message to chat history
|
57 |
-
chat_history.append(
|
|
|
|
|
|
|
58 |
|
59 |
# Update session state with chat history
|
60 |
st.session_state["chat_history"] = chat_history
|
@@ -68,7 +71,7 @@ if st.button("Display History"):
|
|
68 |
c.execute("SELECT * FROM history")
|
69 |
rows = c.fetchall()
|
70 |
for row in rows:
|
71 |
-
st.markdown(f"**{row[0]}:** {row[1]}")
|
72 |
|
73 |
# Add a button to clear chat history from database
|
74 |
if st.button("Clear History"):
|
@@ -77,7 +80,7 @@ if st.button("Clear History"):
|
|
77 |
|
78 |
# Save chat history to database
|
79 |
for message in chat_history:
|
80 |
-
c.execute("INSERT INTO history VALUES (?, ?)", message)
|
81 |
conn.commit()
|
82 |
|
83 |
# Close the connection
|
|
|
30 |
# Display previous messages
|
31 |
for message in chat_history:
|
32 |
role, text = message
|
33 |
+
st.markdown(f"**{role.title()}:** {text}")
|
34 |
|
35 |
# Get user input from text box
|
36 |
user_input = st.text_input("You")
|
|
|
38 |
# Check if user input is not empty
|
39 |
if user_input:
|
40 |
# Add user message to chat history
|
41 |
+
chat_history.append({"role": "user", "content": user_input})
|
42 |
+
|
43 |
+
# Display user message with markdown
|
44 |
+
st.markdown(f"**You:** {user_input}")
|
45 |
|
46 |
# Create model object
|
47 |
model = genai.GenerativeModel(model_name="gemini-pro")
|
48 |
|
|
|
|
|
|
|
49 |
# Get model response with generate_content method
|
50 |
with st.spinner("Thinking..."):
|
51 |
+
response = model.generate_content(chat_history)
|
52 |
|
53 |
# Get response text from response object
|
54 |
response_text = response.text
|
55 |
|
56 |
# Add response message to chat history
|
57 |
+
chat_history.append({"role": "assistant", "content": response_text})
|
58 |
+
|
59 |
+
# Display response message with markdown
|
60 |
+
st.markdown(f"**Gemini Bot:** {response_text}")
|
61 |
|
62 |
# Update session state with chat history
|
63 |
st.session_state["chat_history"] = chat_history
|
|
|
71 |
c.execute("SELECT * FROM history")
|
72 |
rows = c.fetchall()
|
73 |
for row in rows:
|
74 |
+
st.markdown(f"**{row[0].title()}:** {row[1]}")
|
75 |
|
76 |
# Add a button to clear chat history from database
|
77 |
if st.button("Clear History"):
|
|
|
80 |
|
81 |
# Save chat history to database
|
82 |
for message in chat_history:
|
83 |
+
c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["content"]))
|
84 |
conn.commit()
|
85 |
|
86 |
# Close the connection
|