shujath000's picture
Update ch.py
3e96d82 verified
import streamlit as st
import langchain_google_genai as l
import time
from streamlit_option_menu import option_menu
with st.sidebar:
s=option_menu("select from start",options=["select title","enter chat bot","download chat history"])
if s=="select title":
b=st.selectbox(label="select the topic",options=["Python","SQL","Power BI","Exploratory Data Analysis (EDA)","Machine Learning (ML)","Deep Learning (DL)","Generative AI (Gen AI)","Agentic AI"])
if not st.session_state:
st.session_state["topic"]=b
elif "topic" in st.session_state:
st.session_state["topic"]=b
elif s=="enter chat bot":
api="AIzaSyCaw1tgfw_90aB3R2R6lfxAJMXjBSLqE8o"
time.sleep(5)
model=l.GoogleGenerativeAI(model="gemini-2.5-flash-lite",api_key=api,max_tokens=10,temperature=0.2)
if st.session_state:
var="behave like a 1 year experice chat model trainer of"+ st.session_state.get("topic")+"Topic you strictly should not discuss anything other that specified topic"
st.session_state["messages"]=[["system",var]]
prompt=st.chat_input("Type your message")
if prompt:
st.session_state["messages"].append(["user",prompt])
st.session_state.update({"topic":""})
ai_mess=model.invoke(st.session_state["messages"])
st.session_state["messages"].append(["ai",ai_mess])
if "bytes" not in st.session_state:
st.session_state["bytes"]=0
else:
with open("chat.txt","w") as f:
f.seek(st.session_state["bytes"])
for role,mess in st.session_state["messages"]:
bytes=f.write("\n"+role+f": {mess}"+"\n")
st.session_state["bytes"]=bytes
for user,message in st.session_state["messages"]:
if user=="user":
with st.chat_message("user"):
st.write(message)
elif user=="ai":
with st.chat_message("assistant"):
st.write(st.session_state["messages"][-1][-1])
elif s=="download chat history":
with open("chat.txt","r") as f:
st.download_button(label="download chat history",data= f,file_name="chat.txt")
#else:
#st.markdown("please go and select the title to access the chatbot")