File size: 5,619 Bytes
89fd071
8ec9cee
24ce302
 
8e2ca88
8ec9cee
 
 
 
 
31956ab
8ea5675
31956ab
 
8ec9cee
24ce302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31956ab
24ce302
 
8ec9cee
31956ab
 
 
8ec9cee
31956ab
 
 
 
 
16de7f7
24ce302
 
16de7f7
 
 
 
24ce302
 
16de7f7
 
 
 
 
 
24ce302
 
31956ab
 
 
 
 
16de7f7
646fedb
31956ab
 
6b4e7ad
8e2ca88
 
c50a72b
8ec9cee
24ce302
 
 
 
 
646fedb
 
24ce302
 
 
 
646fedb
24ce302
 
 
 
646fedb
 
24ce302
 
 
 
 
 
 
646fedb
d1e0b7e
 
 
 
 
 
24ce302
d1e0b7e
 
 
24ce302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646fedb
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
import streamlit as st
import uuid
import sqlite3
import hashlib
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain.memory import ConversationBufferMemory
from langchain.memory.chat_message_histories import SQLChatMessageHistory
from langchain_core.output_parsers import StrOutputParser

# Configure Gemini API
GEMINI_API_KEY = "AIzaSyAE05Dp1I8XvjzcQL0YaMDOt1htrM-T44Y"
model = ChatGoogleGenerativeAI(model="gemini-2.0-pro-exp", google_api_key=GEMINI_API_KEY)
output_parser = StrOutputParser()

# Database setup
conn = sqlite3.connect("users.db", check_same_thread=False)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS users (
    username TEXT PRIMARY KEY,
    password TEXT,
    user_id TEXT
)
""")
conn.commit()

# Hash function for passwords
def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()

# User authentication functions
def register_user(username, password):
    user_id = str(uuid.uuid4())
    hashed_password = hash_password(password)
    try:
        c.execute("INSERT INTO users (username, password, user_id) VALUES (?, ?, ?)", (username, hashed_password, user_id))
        conn.commit()
        return user_id
    except sqlite3.IntegrityError:
        return None  # Username already exists

def authenticate_user(username, password):
    hashed_password = hash_password(password)
    c.execute("SELECT user_id FROM users WHERE username = ? AND password = ?", (username, hashed_password))
    user = c.fetchone()
    return user[0] if user else None

# Function to create a chat history store for each user
def get_history_store(user_id):
    return SQLChatMessageHistory(connection_string="sqlite:///chat_history.db", session_id=user_id)

# Retrieve past chat history
def get_chat_history(history_store):
    return history_store.messages

# Save chat history
def save_chat(history_store, message, response):
    history_store.add_message(HumanMessage(content=message))
    history_store.add_message(AIMessage(content=response))

# Delete chat history
def clear_chat_history(user_id):
    history_store = get_history_store(user_id)
    history_store.clear()
    st.session_state.messages = []

# Delete selected message
def delete_chat_message(user_id, message_content):
    history_store = get_history_store(user_id)
    messages = history_store.messages
    history_store.clear()
    for msg in messages:
        if msg.content != message_content:
            history_store.add_message(msg)

def chat_with_ai(user_id, message):
    history_store = get_history_store(user_id)
    history = get_chat_history(history_store)
    memory = ConversationBufferMemory()
    for msg in history:
        memory.chat_memory.add_message(msg)
    memory.chat_memory.add_message(HumanMessage(content=message))
    
    response = model.invoke([SystemMessage(content="You are a Data Science Tutor. Answer only data science-related questions. introduce your self as a data science tutor only and give answer less then 500 words")] + memory.chat_memory.messages)
    parsed_response = output_parser.parse(response.content.strip())
    save_chat(history_store, message, parsed_response)
    return parsed_response

# Streamlit UI
st.title("Conversational Data Sience Tutor AI Chatbot")

# User authentication
if "user_id" not in st.session_state:
    login_tab, register_tab = st.tabs(["Login", "Register"])
    
    with login_tab:
        username = st.text_input("Username", key="login_user")
        password = st.text_input("Password", type="password", key="login_pass")
        if st.button("Login"):
            user_id = authenticate_user(username, password)
            if user_id:
                st.session_state.user_id = user_id
                st.session_state.username = username
            else:
                st.error("Invalid username or password.")
    
    with register_tab:
        new_username = st.text_input("New Username", key="reg_user")
        new_password = st.text_input("New Password", type="password", key="reg_pass")
        if st.button("Register"):
            user_id = register_user(new_username, new_password)
            if user_id:
                st.success("Registration successful! Please log in.")
            else:
                st.error("Username already exists.")
else:
    st.write(f"Logged in as: {st.session_state.username}")
    col1, col2 = st.columns(2)

    with col1:
        if st.button("Logout"):
            st.session_state.clear()  # Clears session data
            st.rerun()  # Redirects to login
    
    with col2:
        if st.button("Clear Chat"):
            clear_chat_history(st.session_state.user_id)
    
    # Display chat history with delete buttons
    history_store = get_history_store(st.session_state.user_id)
    past_chats = get_chat_history(history_store)
    for chat in past_chats:
        col1, col2 = st.columns([0.9, 0.1])
        with col1:
            if isinstance(chat, HumanMessage):
                st.chat_message("user").write(chat.content)
            elif isinstance(chat, AIMessage):
                st.chat_message("assistant").write(chat.content)
        with col2:
            if st.button("🗑️", key=chat.content[:20]):
                delete_chat_message(st.session_state.user_id, chat.content)
    
    # Chat input
    user_input = st.chat_input("Type your message...")
    if user_input:
        st.chat_message("user").write(user_input)
        ai_response = chat_with_ai(st.session_state.user_id, user_input)
        st.chat_message("assistant").write(ai_response)