Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +91 -0
- b48c8274-61df-480f-9cd9-47d697ef03e9.jpg +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
b48c8274-61df-480f-9cd9-47d697ef03e9.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 4 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
+
from langchain_community.chat_message_histories import SQLChatMessageHistory
|
| 6 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Use environment variable for security
|
| 10 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 11 |
+
|
| 12 |
+
# Define Chat Prompt Template
|
| 13 |
+
template = ChatPromptTemplate(
|
| 14 |
+
messages=[
|
| 15 |
+
("system", """You are a highly knowledgeable and helpful AI assistant specializing in Data Science.
|
| 16 |
+
Your primary objective is to assist users with topics related to Data Science, including but not limited to Machine Learning,
|
| 17 |
+
Deep Learning, Natural Language Processing (NLP), Computer Vision (CV), Data Engineering, Statistics,
|
| 18 |
+
Data Analysis, and related programming techniques in Python, SQL, and relevant tools.
|
| 19 |
+
You should provide accurate, well-explained, and relevant answers, ensuring clarity and conciseness.
|
| 20 |
+
If a query is outside the scope of Data Science, politely inform the user that you can only answer Data Science-related questions."""),
|
| 21 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 22 |
+
("human", "{input}")
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Initialize AI Model
|
| 27 |
+
model = ChatGoogleGenerativeAI(api_key=API_KEY, model='models/gemini-2.0-flash')
|
| 28 |
+
output = StrOutputParser()
|
| 29 |
+
chain = template | model | output
|
| 30 |
+
|
| 31 |
+
# Chat History Management
|
| 32 |
+
def messages_history(session_id):
|
| 33 |
+
return SQLChatMessageHistory(session_id=session_id, connection="sqlite:///sqlite.db")
|
| 34 |
+
|
| 35 |
+
conversation_chain = RunnableWithMessageHistory(
|
| 36 |
+
chain, messages_history, input_message_key="input", history_messages_key="chat_history"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Streamlit UI Enhancements
|
| 40 |
+
st.set_page_config(page_title="AI Data Science Chatbot", layout="wide")
|
| 41 |
+
st.markdown("""
|
| 42 |
+
<style>
|
| 43 |
+
body { background-color: #f5f5f5; }
|
| 44 |
+
.stChatMessage { border-radius: 12px; padding: 10px; }
|
| 45 |
+
</style>
|
| 46 |
+
""", unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
# Sidebar - User Login
|
| 49 |
+
with st.sidebar:
|
| 50 |
+
st.image("b48c8274-61df-480f-9cd9-47d697ef03e9.jpg", width=150) # Optional: Add chatbot logo
|
| 51 |
+
st.title("🤖 AI Data Science Chatbot")
|
| 52 |
+
st.markdown("💡 Ask me anything about Data Science!")
|
| 53 |
+
st.divider()
|
| 54 |
+
st.header("User Login")
|
| 55 |
+
user_id = st.text_input("Enter your User ID:", key="user_id_input")
|
| 56 |
+
if st.button("Logout"):
|
| 57 |
+
st.session_state.clear()
|
| 58 |
+
st.rerun()
|
| 59 |
+
|
| 60 |
+
if not user_id:
|
| 61 |
+
st.warning("Please enter a User ID to start chatting.")
|
| 62 |
+
st.stop()
|
| 63 |
+
|
| 64 |
+
if "last_user_id" not in st.session_state or st.session_state.last_user_id != user_id:
|
| 65 |
+
st.session_state.chat_history = []
|
| 66 |
+
st.session_state.last_user_id = user_id
|
| 67 |
+
|
| 68 |
+
chat_history = messages_history(user_id).messages
|
| 69 |
+
st.session_state.chat_history = [(msg.type, msg.content) for msg in chat_history]
|
| 70 |
+
|
| 71 |
+
st.markdown("<h2 style='text-align: center;'>💬 Chat with the AI Assistant</h2>", unsafe_allow_html=True)
|
| 72 |
+
|
| 73 |
+
for role, message in st.session_state.chat_history:
|
| 74 |
+
if role == "user":
|
| 75 |
+
st.chat_message("user").markdown(f"**🧑💻 You:** {message}")
|
| 76 |
+
else:
|
| 77 |
+
st.chat_message("assistant").markdown(f"**🤖 AI:** {message}")
|
| 78 |
+
|
| 79 |
+
# User Input & AI Response
|
| 80 |
+
user_input = st.chat_input("Type your message...")
|
| 81 |
+
|
| 82 |
+
if user_input:
|
| 83 |
+
st.session_state.chat_history.append(("user", user_input))
|
| 84 |
+
st.chat_message("user").write(user_input)
|
| 85 |
+
|
| 86 |
+
config = {"configurable": {"session_id": user_id}}
|
| 87 |
+
input_prompt = {"input": user_input}
|
| 88 |
+
response = conversation_chain.invoke(input_prompt, config=config)
|
| 89 |
+
|
| 90 |
+
st.session_state.chat_history.append(("assistant", response))
|
| 91 |
+
st.chat_message("assistant").write(response)
|
b48c8274-61df-480f-9cd9-47d697ef03e9.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain_core
|
| 3 |
+
langchain_community
|
| 4 |
+
langchain_google_genai
|