CoolFace
Apppublic

RustX/CSV-ChatBot

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
history.py57 linesDownload Raw Back to modules
1import os2import streamlit as st3from streamlit_chat import message4 5 6class ChatHistory:7    def __init__(self):8        self.history = st.session_state.get("history", [])9        st.session_state["history"] = self.history10 11    def default_greeting(self):12        return "안녕 ! 👋"13 14    def default_prompt(self, topic):15        return f"안녕하세요 ! {topic}에 대해 무엇이든 물어보세요 🤗"16 17    def initialize_user_history(self):18        st.session_state["user"] = [self.default_greeting()]19 20    def initialize_assistant_history(self, uploaded_file):21        st.session_state["assistant"] = [self.default_prompt(uploaded_file.name)]22 23    def initialize(self, uploaded_file):24        if "assistant" not in st.session_state:25            self.initialize_assistant_history(uploaded_file)26        if "user" not in st.session_state:27            self.initialize_user_history()28 29    def reset(self, uploaded_file):30        st.session_state["history"] = []31        self.initialize_user_history()32        self.initialize_assistant_history(uploaded_file)33        st.session_state["reset_chat"] = False34 35    def append(self, mode, message):36        st.session_state[mode].append(message)37 38    def generate_messages(self, container):39        if st.session_state["assistant"]:40            with container:41                for i in range(len(st.session_state["assistant"])):42                    message(43                        st.session_state["user"][i],44                        is_user=True,45                        key=f"{i}_user",46                        avatar_style="big-smile",47                    )48                    message(st.session_state["assistant"][i], key=str(i), avatar_style="thumbs")49 50    def load(self):51        if os.path.exists(self.history_file):52            with open(self.history_file, "r") as f:53                self.history = f.read().splitlines()54 55    def save(self):56        with open(self.history_file, "w") as f:57            f.write("\n".join(self.history))