Spaces:
Runtime error
Runtime error
Commit
·
7a4fa7e
1
Parent(s):
9257db8
Update app.py
Browse files
app.py
CHANGED
@@ -2,63 +2,62 @@ import streamlit as st
|
|
2 |
import google.generativeai as genai
|
3 |
import sqlite3
|
4 |
|
5 |
-
|
|
|
6 |
c = conn.cursor()
|
7 |
|
8 |
c.execute('''
|
9 |
-
CREATE TABLE IF NOT EXISTS history
|
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 |
-
|
30 |
-
|
31 |
-
|
|
|
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 |
-
|
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 |
-
#
|
|
|
|
|
|
2 |
import google.generativeai as genai
|
3 |
import sqlite3
|
4 |
|
5 |
+
# Database setup
|
6 |
+
conn = sqlite3.connect('chat_history.db')
|
7 |
c = conn.cursor()
|
8 |
|
9 |
c.execute('''
|
10 |
+
CREATE TABLE IF NOT EXISTS history
|
11 |
(role TEXT, message TEXT)
|
12 |
''')
|
13 |
|
14 |
+
# Generative AI setup
|
15 |
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
16 |
+
genai.configure(api_key=api_key)
|
17 |
|
18 |
generation_config = {
|
19 |
+
"temperature": 0.9,
|
20 |
+
"max_output_tokens": 100
|
21 |
}
|
22 |
|
23 |
+
safety_settings = []
|
24 |
|
25 |
model = genai.GenerativeModel(
|
26 |
model_name="gemini-pro",
|
27 |
generation_config=generation_config,
|
28 |
+
safety_settings=safety_settings
|
29 |
)
|
30 |
+
|
31 |
+
# Streamlit UI
|
32 |
+
st.title("Chatbot")
|
33 |
+
|
34 |
+
chat_history = st.session_state.get("chat_history", [])
|
35 |
|
36 |
if len(chat_history) % 2 == 0:
|
37 |
role = "user"
|
38 |
+
else:
|
39 |
role = "model"
|
40 |
|
41 |
for message in chat_history:
|
42 |
+
r, t = message["role"], message["parts"][0]["text"]
|
43 |
st.markdown(f"**{r.title()}:** {t}")
|
44 |
|
45 |
+
user_input = st.text_input("")
|
46 |
+
|
47 |
+
if user_input:
|
48 |
+
chat_history.append({"role": role, "parts": [{"text": user_input}]})
|
49 |
|
50 |
+
if role == "user":
|
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 |
for message in chat_history:
|
58 |
r, t = message["role"], message["parts"][0]["text"]
|
59 |
st.markdown(f"**{r.title()}:** {t}")
|
60 |
|
61 |
+
# Buttons to display, save, and reset chat
|
62 |
+
|
63 |
+
conn.close()
|