File size: 2,201 Bytes
800273b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import uuid
from typing import List, Dict
import os
from dataset_loader import load_dataset

if not os.path.isdir("r_logic_data"):
    load_dataset()

from r_logic_data.agent import Agent


class StreamlitChatbot:
    def __init__(self):
        self.agent = Agent()

    def initialize_session_state(self):
        if "messages" not in st.session_state:
            st.session_state.messages = []
        if "recipient_id" not in st.session_state:
            st.session_state.recipient_id = str(uuid.uuid4())

    def display_chat_history(self):
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

    def get_chatbot_response(self, user_input: str) -> str:
        item = {
            "content": user_input,
            "recipient_id": st.session_state.recipient_id,
            "sender_id": "chatbot",
            "history_id": f"history_{len(st.session_state.messages)}",
            "qa_id": f"qa_{len(st.session_state.messages)}",
        }

        response = self.agent.answer(
            question=user_input,
            recipient_id=st.session_state.recipient_id,
            sender_id="chatbot",
            item=item,
            mode="chat",
            platform=1,  # Assuming 1 is for Streamlit
            history=st.session_state.messages,
        )

        return response[0]  # Returning just the answer text

    def run(self):
        st.title("R-Logic Computer Repair Chatbot")
        self.initialize_session_state()

        self.display_chat_history()

        if user_input := st.chat_input(
            "Ask about your repair status or any questions..."
        ):
            st.session_state.messages.append({"role": "user", "content": user_input})
            with st.chat_message("user"):
                st.markdown(user_input)

            response = self.get_chatbot_response(user_input)
            st.session_state.messages.append({"role": "assistant", "content": response})
            with st.chat_message("assistant"):
                st.markdown(response)


if __name__ == "__main__":
    chatbot = StreamlitChatbot()
    chatbot.run()