|
import streamlit as st |
|
|
|
import os |
|
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler |
|
from langchain.schema import StrOutputParser |
|
|
|
from custom_llm import CustomLLM, custom_chain_with_history, custom_dataframe_chain, format_df,custom_unique_df_chain |
|
import pandas as pd |
|
|
|
|
|
API_TOKEN = os.getenv('HF_INFER_API') |
|
|
|
|
|
from typing import Optional |
|
|
|
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder |
|
from langchain_community.chat_models import ChatAnthropic |
|
from langchain_core.chat_history import BaseChatMessageHistory |
|
from langchain.memory import ConversationBufferMemory |
|
from langchain_core.runnables.history import RunnableWithMessageHistory |
|
|
|
|
|
|
|
st.title("LMD Chatbot Tiket Ebesha Management") |
|
st.subheader("Monthly Ticket Sample") |
|
|
|
|
|
uploaded_files = st.file_uploader("Choose CSV or XLSX files", accept_multiple_files=True, type=["csv", "xlsx"]) |
|
df_temp = [] |
|
for uploaded_file in uploaded_files: |
|
if uploaded_file.name.split(".")[-1] != 'csv': |
|
a = pd.read_excel(uploaded_file) |
|
uploaded_file = uploaded_file.name.split(".")[0]+".csv" |
|
a.to_csv(uploaded_file, encoding="utf8", header=True, index=False) |
|
|
|
df_temp.append(pd.read_csv(uploaded_file)) |
|
|
|
|
|
|
|
if uploaded_files: |
|
|
|
df = pd.concat(df_temp) |
|
|
|
|
|
|
|
|
|
if 'unique_values' not in st.session_state: |
|
exec(custom_unique_df_chain(llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"]), df=df).invoke({"df_example":format_df(df.head(4))})) |
|
st.session_state.unique_values = response |
|
|
|
@st.cache_resource |
|
def get_llm_chain(): |
|
|
|
|
|
|
|
return custom_dataframe_chain(llm=CustomLLM(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", model_type='text-generation', api_token=API_TOKEN, stop=["\n<|","<|"]), df=df, unique_values=st.session_state.unique_values) |
|
|
|
if 'memory' not in st.session_state: |
|
st.session_state['memory'] = ConversationBufferMemory(return_messages=True) |
|
st.session_state.memory.chat_memory.add_ai_message("Hello there! I'm AI assistant of Lintas Media Danawa. How can I help you today?") |
|
|
|
if 'chain' not in st.session_state: |
|
|
|
st.session_state['chain'] = get_llm_chain() |
|
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [{"role":"assistant", "content":"Hello there! I'm AI assistant of Lintas Media Danawa. How can I help you today?"}] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
|
if prompt := st.chat_input("Ask me anything.."): |
|
|
|
st.chat_message("User").markdown(prompt) |
|
|
|
st.session_state.messages.append({"role": "User", "content": prompt}) |
|
|
|
|
|
|
|
|
|
|
|
full_response = st.session_state.chain.invoke({"question":prompt,"df_example":format_df(df.head(4))}) |
|
print(len(full_response)) |
|
exec(full_response) |
|
|
|
full_response = "Here is the python code: \n\n```python"+ full_response +"\n```\n\nGenerated Response: \n\n"+ str(response) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(full_response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": full_response}) |