Spaces:
Runtime error
Runtime error
Commit
·
1d3466d
1
Parent(s):
a608f56
Adding more features
Browse files
app.py
CHANGED
|
@@ -1,6 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# API key
|
| 6 |
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
|
@@ -14,16 +27,18 @@ st.title("Gemini API Chatbot")
|
|
| 14 |
# Get chat history from session state
|
| 15 |
chat_history = st.session_state.get("chat_history", [])
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Get user input from text box
|
| 18 |
user_input = st.text_input("You")
|
| 19 |
|
| 20 |
# Check if user input is not empty
|
| 21 |
if user_input:
|
| 22 |
# Add user message to chat history
|
| 23 |
-
chat_history.append(user_input)
|
| 24 |
-
|
| 25 |
-
# Display user message with markdown
|
| 26 |
-
st.markdown(f"**You:** {user_input}")
|
| 27 |
|
| 28 |
# Create model object
|
| 29 |
model = genai.GenerativeModel(model_name="gemini-pro")
|
|
@@ -36,10 +51,31 @@ if user_input:
|
|
| 36 |
response_text = response.text
|
| 37 |
|
| 38 |
# Add response message to chat history
|
| 39 |
-
chat_history.append(response_text)
|
| 40 |
-
|
| 41 |
-
# Display response message with markdown
|
| 42 |
-
st.markdown(f"**Gemini Bot:** {response_text}")
|
| 43 |
|
| 44 |
# Update session state with chat history
|
| 45 |
st.session_state["chat_history"] = chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import google.generativeai as genai
|
| 4 |
+
import sqlite3
|
| 5 |
+
|
| 6 |
+
# Connect to SQLite database
|
| 7 |
+
conn = sqlite3.connect('chat_history.db')
|
| 8 |
+
c = conn.cursor()
|
| 9 |
+
|
| 10 |
+
# Create table if it doesn't exist
|
| 11 |
+
c.execute('''
|
| 12 |
+
CREATE TABLE IF NOT EXISTS history (
|
| 13 |
+
role TEXT,
|
| 14 |
+
message TEXT
|
| 15 |
+
)
|
| 16 |
+
''')
|
| 17 |
|
| 18 |
# API key
|
| 19 |
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
|
|
|
| 27 |
# Get chat history from session state
|
| 28 |
chat_history = st.session_state.get("chat_history", [])
|
| 29 |
|
| 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")
|
| 37 |
|
| 38 |
# Check if user input is not empty
|
| 39 |
if user_input:
|
| 40 |
# Add user message to chat history
|
| 41 |
+
chat_history.append(("You", user_input))
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Create model object
|
| 44 |
model = genai.GenerativeModel(model_name="gemini-pro")
|
|
|
|
| 51 |
response_text = response.text
|
| 52 |
|
| 53 |
# Add response message to chat history
|
| 54 |
+
chat_history.append(("Gemini Bot", response_text))
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Update session state with chat history
|
| 57 |
st.session_state["chat_history"] = chat_history
|
| 58 |
+
|
| 59 |
+
# Add a button to reset chat
|
| 60 |
+
if st.button("Reset Chat"):
|
| 61 |
+
st.session_state["chat_history"] = []
|
| 62 |
+
|
| 63 |
+
# Add a button to display chat history from database
|
| 64 |
+
if st.button("Display History"):
|
| 65 |
+
c.execute("SELECT * FROM history")
|
| 66 |
+
rows = c.fetchall()
|
| 67 |
+
for row in rows:
|
| 68 |
+
st.markdown(f"**{row[0]}:** {row[1]}")
|
| 69 |
+
|
| 70 |
+
# Add a button to clear chat history from database
|
| 71 |
+
if st.button("Clear History"):
|
| 72 |
+
c.execute("DELETE FROM history")
|
| 73 |
+
conn.commit()
|
| 74 |
+
|
| 75 |
+
# Save chat history to database
|
| 76 |
+
for message in chat_history:
|
| 77 |
+
c.execute("INSERT INTO history VALUES (?, ?)", message)
|
| 78 |
+
conn.commit()
|
| 79 |
+
|
| 80 |
+
# Close the connection
|
| 81 |
+
conn.close()
|