Spaces:
Sleeping
Sleeping
File size: 5,223 Bytes
cbca970 965710c cbca970 0881cff c4ba536 b8e0f6f 965710c 8284733 bfc799d cfb6b98 c4ba536 e507109 c4ba536 cfb6b98 e507109 c4ba536 e507109 c4ba536 e507109 c4ba536 e507109 c4ba536 a1bfbea c4ba536 cfb6b98 c4ba536 de9f07e cbca970 a1bfbea cbca970 c4ba536 797b03c c4ba536 e507109 c4ba536 a1bfbea e507109 c4ba536 cbca970 951dccc cbca970 a1bfbea 951dccc 1441e1b 100ce6d 1441e1b c4ba536 cbca970 c4ba536 cfb6b98 c4ba536 cbca970 cfb6b98 c4ba536 cbca970 cfb6b98 c4ba536 cbca970 cfb6b98 cbca970 a1bfbea cbca970 5e1ad3d 526b85a cfb6b98 cbca970 |
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
import streamlit as st
import sqlite3
import uuid
import time
from langchain_google_genai import GoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.output_parsers import StrOutputParser
from langchain_community.chat_message_histories import SQLChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
# Load API key
GOOGLE_API_KEY = st.secrets.get("GOOGLE_API_KEY")
# Set up the Gemini 1.5 Pro model
llm = GoogleGenerativeAI(api_key=GOOGLE_API_KEY, model="gemini-1.5-pro")
# Initialize SQLite database
conn = sqlite3.connect("chat_history.db", check_same_thread=False)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS chat (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
role TEXT,
content TEXT
)
""")
conn.commit()
# Function to save messages
def save_message(session_id, role, content):
cursor.execute("INSERT INTO chat (session_id, role, content) VALUES (?, ?, ?)", (session_id, role, content))
conn.commit()
# Function to load chat history
def load_chat_history(session_id):
cursor.execute("SELECT role, content FROM chat WHERE session_id = ?", (session_id,))
return cursor.fetchall()
# Chat history instance
def chat_history(session_id):
return SQLChatMessageHistory(
session_id=session_id,
connection="sqlite:///chat_history.db"
)
# Generate unique session ID
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())
col1, col2 = st.columns([4, 1])
with col2:
if st.button("π New Chat"):
st.session_state.session_id = str(uuid.uuid4()) # Generate new session
st.session_state.messages = [] # Clear chat history
st.rerun() # Refresh the app
with col1:
# Custom CSS for UI enhancements
st.markdown("""
<style>
/* Style for the title animation */
.title-text {
text-align: center;
font-size: 30px;
font-weight: bold;
color: #FF4500;
margin-bottom: 20px;
}
/* Keep input field fixed at the bottom */
.stTextInput {
position: fixed;
bottom: 10px;
width: 80%;
left: 10%;
z-index: 999;
}
</style>
""", unsafe_allow_html=True)
# πΉ **Animated Title Function**
def animated_text(text, speed=0.05):
placeholder = st.empty()
displayed_text = ""
for letter in text:
displayed_text += letter
placeholder.markdown(f"""
<h1 style="text-align:center; color: #00D1FF;">{displayed_text} π</h1>
""", unsafe_allow_html=True) # Corrected f-string formatting
time.sleep(speed)
# πΉ **Display Animated Welcome Message**
animated_text('Conversational AI Data Science Tutor')
# Get session ID
session_id = st.session_state.session_id
chat_history_instance = chat_history(session_id)
# Define Chat Prompt Template
chat_prompt = ChatPromptTemplate(
messages=[
('system', """You are an AI assistant specialized in Data Science tutoring.
You will only answer questions related to Data Science.
If asked anything outside this topic, politely decline and request a Data Science-related question.
"""),
MessagesPlaceholder(variable_name="history", optional=True),
('human', '{prompt}')
]
)
# Define output parser
out_parser = StrOutputParser()
# Create a chain
chain = chat_prompt | llm | out_parser
# Define Runnable with message history
chat = RunnableWithMessageHistory(
chain,
lambda session: SQLChatMessageHistory(session, "sqlite:///chat_history.db"),
input_messages_key="prompt",
history_messages_key="history"
)
# πΉ **Chat History Container**
chat_container = st.container()
# Load chat history and display it
if "messages" not in st.session_state:
st.session_state.messages = load_chat_history(session_id)
with chat_container:
for role, content in st.session_state.messages:
with st.chat_message(role):
st.markdown(content)
# User input at the bottom
# πΉ **Fixed Bottom User Input**
user_input = st.text_input("Type your message here:", key="user_message")
# If user submits a message
if user_input:
# Save user message
save_message(session_id, "user", user_input)
st.session_state.messages.append(("user", user_input))
# Invoke AI model
config = {'configurable': {'session_id': session_id}}
response = chat.invoke({'prompt': user_input}, config)
# Save AI response
save_message(session_id, "assistant", response)
st.session_state.messages.append(("assistant", response))
# Display AI response
with chat_container:
with st.chat_message("assistant"):
st.markdown(response)
# β
Clear the input field after message submission
st.session_state.pop("user_message")
st.session_state["user_message"] = ""
st.rerun() # Refresh the app
|