Spaces:
Runtime error
Runtime error
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() | |