Spaces:
Runtime error
Runtime error
Commit
·
9257db8
1
Parent(s):
88cfe62
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
import google.generativeai as genai
|
3 |
import sqlite3
|
4 |
|
5 |
conn = sqlite3.connect('chat_history.db')
|
@@ -10,71 +10,55 @@ c.execute('''
|
|
10 |
(role TEXT, message TEXT)
|
11 |
''')
|
12 |
|
13 |
-
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
14 |
genai.configure(api_key=api_key)
|
15 |
|
16 |
generation_config = {
|
17 |
-
"temperature": 0.9,
|
18 |
-
"max_output_tokens":
|
19 |
}
|
20 |
|
21 |
-
safety_settings = []
|
22 |
|
23 |
model = genai.GenerativeModel(
|
24 |
model_name="gemini-pro",
|
25 |
-
generation_config=generation_config,
|
26 |
-
safety_settings=safety_settings
|
27 |
)
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
for message in chat_history:
|
35 |
r, t = message["role"], message["parts"][0]["text"]
|
36 |
st.markdown(f"**{r.title()}:** {t}")
|
37 |
-
displayed_msgs.append(message)
|
38 |
|
39 |
-
user_input = st.text_input("")
|
40 |
-
|
41 |
-
if user_input:
|
42 |
-
|
43 |
-
if len(chat_history) % 2 == 0:
|
44 |
-
role = "user"
|
45 |
-
else:
|
46 |
-
role = "model"
|
47 |
-
|
48 |
-
chat_history.append({"role": role, "parts": [{"text": user_input}]})
|
49 |
|
50 |
-
|
|
|
|
|
|
|
51 |
response = model.generate_content(chat_history)
|
52 |
-
response_text = response.text
|
|
|
53 |
chat_history.append({"role": "model", "parts": [{"text": response_text}]})
|
54 |
-
|
55 |
st.session_state["chat_history"] = chat_history
|
|
|
|
|
|
|
56 |
|
57 |
-
|
|
|
58 |
for message in chat_history:
|
59 |
-
|
60 |
-
|
61 |
-
st.markdown(f"**{r.title()}:** {t}")
|
62 |
-
displayed_msgs.append(message)
|
63 |
-
|
64 |
-
if st.button("Display History"):
|
65 |
-
displayed_msgs = []
|
66 |
-
c.execute("SELECT * FROM history")
|
67 |
-
rows = c.fetchall()
|
68 |
|
69 |
-
|
70 |
-
if {"role": row[0], "parts": [{"text": row[1]}]} not in displayed_msgs:
|
71 |
-
st.markdown(f"**{row[0].title()}:** {row[1]}")
|
72 |
-
displayed_msgs.append({"role": row[0], "parts": [{"text": row[1]}]})
|
73 |
-
|
74 |
-
for message in chat_history:
|
75 |
-
if {"role": message["role"], "parts": [message["parts"][0]]} not in rows:
|
76 |
-
c.execute("INSERT INTO history VALUES (?, ?)",
|
77 |
-
(message["role"], message["parts"][0]["text"]))
|
78 |
-
conn.commit()
|
79 |
-
|
80 |
-
conn.close()
|
|
|
1 |
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
import sqlite3
|
4 |
|
5 |
conn = sqlite3.connect('chat_history.db')
|
|
|
10 |
(role TEXT, message TEXT)
|
11 |
''')
|
12 |
|
13 |
+
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
14 |
genai.configure(api_key=api_key)
|
15 |
|
16 |
generation_config = {
|
17 |
+
"temperature": 0.9,
|
18 |
+
"max_output_tokens": 100,
|
19 |
}
|
20 |
|
21 |
+
safety_settings = []
|
22 |
|
23 |
model = genai.GenerativeModel(
|
24 |
model_name="gemini-pro",
|
25 |
+
generation_config=generation_config,
|
26 |
+
safety_settings=safety_settings
|
27 |
)
|
28 |
+
|
29 |
+
st.title("Chatbot")
|
30 |
+
|
31 |
+
chat_history = st.session_state.get("chat_history", [])
|
32 |
|
33 |
+
if len(chat_history) % 2 == 0:
|
34 |
+
role = "user"
|
35 |
+
else:
|
36 |
+
role = "model"
|
37 |
+
|
38 |
for message in chat_history:
|
39 |
r, t = message["role"], message["parts"][0]["text"]
|
40 |
st.markdown(f"**{r.title()}:** {t}")
|
|
|
41 |
|
42 |
+
user_input = st.text_input("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
if user_input:
|
45 |
+
chat_history.append({"role": role, "parts": [{"text": user_input}]})
|
46 |
+
|
47 |
+
if role == "user":
|
48 |
response = model.generate_content(chat_history)
|
49 |
+
response_text = response.text
|
50 |
+
|
51 |
chat_history.append({"role": "model", "parts": [{"text": response_text}]})
|
52 |
+
|
53 |
st.session_state["chat_history"] = chat_history
|
54 |
+
if st.button("Display History"):
|
55 |
+
c.execute("SELECT * FROM history")
|
56 |
+
rows = c.fetchall()
|
57 |
|
58 |
+
for row in rows:
|
59 |
+
st.markdown(f"**{row[0].title()}:** {row[1]}")
|
60 |
for message in chat_history:
|
61 |
+
r, t = message["role"], message["parts"][0]["text"]
|
62 |
+
st.markdown(f"**{r.title()}:** {t}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# Save and display chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|