Spaces:
Runtime error
Runtime error
File size: 1,817 Bytes
c3cd834 9257db8 1d3466d 7a4fa7e 1d3466d 7a4fa7e fc87a94 7a4fa7e 9257db8 7a4fa7e 5b0a6ee e21ddc9 7a4fa7e e21ddc9 7a4fa7e fc87a94 88cfe62 9257db8 7a4fa7e fc87a94 7a4fa7e fc87a94 9257db8 7a4fa7e 9257db8 1d3466d 7a4fa7e 2b31cd9 7a4fa7e 9257db8 7a4fa7e 9257db8 7a4fa7e 9257db8 fc87a94 7a4fa7e 22dd94c 9257db8 2b31cd9 cecacb2 7a4fa7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import streamlit as st
import google.generativeai as genai
import sqlite3
# Database setup
conn = sqlite3.connect('chat_history.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS history
(role TEXT, message TEXT)
''')
# Generative AI setup
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
genai.configure(api_key=api_key)
generation_config = {
"temperature": 0.9,
"max_output_tokens": 100
}
safety_settings = []
model = genai.GenerativeModel(
model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings
)
# Streamlit UI
st.title("Chatbot")
chat_history = st.session_state.get("chat_history", [])
if len(chat_history) % 2 == 0:
role = "user"
else:
role = "model"
for message in chat_history:
r, t = message["role"], message["parts"][0]["text"]
st.markdown(f"**{r.title()}:** {t}")
user_input = st.text_input("")
if user_input:
chat_history.append({"role": role, "parts": [{"text": user_input}]})
if role == "user":
response = model.generate_content(chat_history)
response_text = response.text
chat_history.append({"role": "model", "parts": [{"text": response_text}]})
st.session_state["chat_history"] = chat_history
for message in chat_history:
r, t = message["role"], message["parts"][0]["text"]
st.markdown(f"**{r.title()}:** {t}")
if st.button("Display History"):
c.execute("SELECT * FROM history")
rows = c.fetchall()
for row in rows:
st.markdown(f"**{row[0].title()}:** {row[1]}")
# Save chat history to database
for message in chat_history:
c.execute("INSERT INTO history VALUES (?, ?)",
(message["role"], message["parts"][0]["text"]))
conn.commit()
conn.close() |