Spaces:
Runtime error
Runtime error
File size: 3,111 Bytes
57c5bcb c3cd834 5b0a6ee 1d3466d aa89276 1d3466d c3cd834 54385f0 37ac5ae 5b0a6ee e21ddc9 aa89276 e21ddc9 aa89276 e21ddc9 aa89276 e21ddc9 74fa8e5 485a5d1 74fa8e5 0195449 e4e897b 1d3466d e4e897b aa89276 1d3466d 74fa8e5 b118f5f 37ac5ae 74fa8e5 37ac5ae aa89276 e4e897b c589cac aa89276 57c5bcb e4e897b aa89276 49bb563 aa89276 96d0abe 49bb563 aa89276 e4e897b e21ddc9 aa89276 c589cac aa89276 1d3466d aa89276 1d3466d aa89276 1d3466d aa89276 1d3466d e4e897b 1d3466d |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import os
import streamlit as st
import google.generativeai as genai
import sqlite3
# Connect to SQLite database
conn = sqlite3.connect('chat_history.db')
c = conn.cursor()
# Create table if it doesn't exist
c.execute('''
CREATE TABLE IF NOT EXISTS history (
role TEXT,
message TEXT
)
''')
# API key
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
# Configure the API key
genai.configure(api_key=api_key)
# Set up the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
]
model = genai.GenerativeModel(model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings)
# Create chatbot interface
st.title("Gemini API Chatbot")
# Get chat history from session state
chat_history = st.session_state.get("chat_history", [])
# Convert chat history to the expected format
chat_history = [{"role": message["role"], "parts": [{"text": message["content"]}]} for message in chat_history]
# Display previous messages
for message in chat_history:
role, text = message["role"], message["parts"][0]["text"]
st.markdown(f"**{role.title()}:** {text}")
# Get user input from text box
user_input = st.text_input("You")
# Check if user input is not empty
if user_input:
# Add user message to chat history
chat_history.append({"role": "user", "parts": [{"text": user_input}]})
# Display user message with markdown
st.markdown(f"**You:** {user_input}")
# Get model response with generate_content method```
with st.spinner("Thinking..."):
response = model.generate_content(chat_history)
# Get response text from response object
response_text = response.result.text
# Add response message to chat history
chat_history.append({"role": "assistant", "parts": [{"text": response_text}]})
# Display response message with markdown
st.markdown(f"**Gemini Bot:** {response_text}")
# Update session state with chat history
st.session_state["chat_history"] = chat_history
# Add a button to reset chat
if st.button("Reset Chat"):
st.session_state["chat_history"] = []
# Add a button to display chat history from database
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]}")
# Add a button to clear chat history from database
if st.button("Clear History"):
c.execute("DELETE FROM history")
conn.commit()
# Save chat history to database
for message in chat_history:
c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["parts"][0]["text"]))
conn.commit()
# Close the connection
conn.close()
|