Spaces:
Runtime error
Runtime error
import streamlit as st | |
import google.generativeai as genai | |
import sqlite3 | |
from PIL import Image | |
# 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": 3000 | |
} | |
safety_settings = [] | |
# Initialize session state | |
if "chat_history" not in st.session_state: | |
st.session_state["chat_history"] = [] | |
if "user_input" not in st.session_state: | |
st.session_state["user_input"] = "" | |
# Streamlit UI | |
st.set_page_config(page_title="Chatbot", page_icon="🤖") | |
# Header with logo | |
st.markdown(""" | |
<style> | |
.container { | |
display: flex; | |
} | |
.logo-text { | |
font-weight:700 !important; | |
font-size:50px !important; | |
color: #f9a01b !important; | |
padding-top: 75px !important; | |
} | |
.logo-img { | |
float:right; | |
} | |
</style> | |
<div class="container"> | |
<p class="logo-text">Chatbot</p> | |
<img class="logo-img" src="https://example.com/bot-logo.png" width=120 height=120> | |
</div> | |
""", unsafe_allow_html=True) | |
# Sidebar UI elements | |
# Display chat history | |
for message in st.session_state["chat_history"]: | |
r, t = message["role"], message["parts"][0]["text"] | |
st.markdown(f"**{r.title()}:** {t}") | |
# Alternate roles | |
if len(st.session_state["chat_history"]) % 2 == 0: | |
role = "user" | |
else: | |
role = "model" | |
# User input | |
user_input = st.text_area("", value=st.session_state.user_input, height=5, key="user_input") | |
# Other UI elements | |
# Handle user input | |
if user_input: | |
try: | |
# Add user input | |
st.session_state["chat_history"].append({"role": role, "parts": [{"text": user_input}]}) | |
# Model code | |
if role == "user": | |
model = genai.GenerativeModel('gemini-pro') | |
response = model.generate_content( | |
contents=[user_input], | |
generation_config=generation_config | |
) | |
# Add model response | |
st.session_state["chat_history"].append({"role": "model", "parts": [{"text": response}]}) | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
# Display chat history | |
for message in st.session_state["chat_history"]: | |
r, t = message["role"], message["parts"][0]["text"] | |
st.markdown(f"**{r.title()}:** {t}") | |
# Save chat history to database | |
for message in st.session_state["chat_history"]: | |
c.execute("INSERT INTO history VALUES (?, ?)", | |
(message["role"], message["parts"][0]["text"])) | |
conn.commit() | |
# Clear user input | |
st.session_state.user_input = "" | |
# Footer | |
st.markdown(""" | |
<style> | |
.footer { | |
position: fixed; | |
left: 0; | |
bottom: 0; | |
width: 100%; | |
background-color: #f9a01b; | |
color: white; | |
text-align: center; | |
} | |
</style> | |
<div class="footer"> | |
<p>Made with Streamlit and Google Generative AI</p> | |
</div> | |
""", unsafe_allow_html=True) |