Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
|
|
|
|
4 |
|
5 |
class ChatApp:
|
6 |
def __init__(self):
|
7 |
-
st.set_page_config(page_title="Inspection Engineer Chat", page_icon="π")
|
8 |
self.initialize_session_state()
|
9 |
self.model_handler = self.load_model()
|
10 |
|
@@ -36,24 +39,52 @@ class ChatApp:
|
|
36 |
def get_user_input(self):
|
37 |
return st.chat_input("Type your message here...")
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def run(self):
|
40 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
self.display_message("assistant", response)
|
56 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
57 |
|
58 |
class ModelHandler:
|
59 |
def __init__(self, model, tokenizer):
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
+
import time
|
5 |
+
import json
|
6 |
+
from datetime import datetime
|
7 |
|
8 |
class ChatApp:
|
9 |
def __init__(self):
|
10 |
+
st.set_page_config(page_title="Inspection Engineer Chat", page_icon="π", layout="wide")
|
11 |
self.initialize_session_state()
|
12 |
self.model_handler = self.load_model()
|
13 |
|
|
|
39 |
def get_user_input(self):
|
40 |
return st.chat_input("Type your message here...")
|
41 |
|
42 |
+
def stream_response(self, response):
|
43 |
+
placeholder = st.empty()
|
44 |
+
full_response = ""
|
45 |
+
for word in response.split():
|
46 |
+
full_response += word + " "
|
47 |
+
placeholder.markdown(full_response + "β")
|
48 |
+
time.sleep(0.01) # Adjust the sleep time between 0.01 and 0.05 for desired speed
|
49 |
+
placeholder.markdown(full_response)
|
50 |
+
return full_response
|
51 |
+
|
52 |
+
def save_chat_history(self):
|
53 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
54 |
+
filename = f"chat_history_{timestamp}.json"
|
55 |
+
with open(filename, "w") as f:
|
56 |
+
json.dump(st.session_state.messages, f, indent=2)
|
57 |
+
return filename
|
58 |
+
|
59 |
def run(self):
|
60 |
+
col1, col2 = st.columns([3, 1])
|
61 |
+
|
62 |
+
with col1:
|
63 |
+
st.title("Inspection Methods Engineer Assistant")
|
64 |
+
|
65 |
+
for message in st.session_state.messages[1:]:
|
66 |
+
self.display_message(message["role"], message["content"])
|
67 |
|
68 |
+
user_input = self.get_user_input()
|
69 |
+
if user_input:
|
70 |
+
self.display_message("user", user_input)
|
71 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
72 |
|
73 |
+
conversation = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages])
|
74 |
+
|
75 |
+
with st.spinner("Analyzing and classifying scope..."):
|
76 |
+
response = self.model_handler.generate_response(conversation)
|
77 |
+
|
78 |
+
with st.chat_message("assistant"):
|
79 |
+
full_response = self.stream_response(response)
|
80 |
+
|
81 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
82 |
|
83 |
+
with col2:
|
84 |
+
st.sidebar.title("Chat Options")
|
85 |
+
if st.sidebar.button("Save Chat History"):
|
86 |
+
filename = self.save_chat_history()
|
87 |
+
st.sidebar.success(f"Chat history saved to {filename}")
|
|
|
|
|
88 |
|
89 |
class ModelHandler:
|
90 |
def __init__(self, model, tokenizer):
|