Update app.py
Browse files
app.py
CHANGED
@@ -1,151 +1,28 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
-
import
|
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 |
-
MAX_HISTORY_LENGTH = 5
|
31 |
-
|
32 |
-
if 'user_id' in st.session_state:
|
33 |
-
user_id = st.session_state['user_id']
|
34 |
-
else:
|
35 |
-
user_id = str(uuid.uuid4())
|
36 |
-
st.session_state['user_id'] = user_id
|
37 |
-
|
38 |
-
if 'chat_history' not in st.session_state:
|
39 |
-
st.session_state['chat_history'] = []
|
40 |
-
|
41 |
-
if "chats" not in st.session_state:
|
42 |
-
st.session_state.chats = [
|
43 |
-
{
|
44 |
-
'id': 0,
|
45 |
-
'question': '',
|
46 |
-
'answer': ''
|
47 |
-
}
|
48 |
-
]
|
49 |
-
|
50 |
-
if "questions" not in st.session_state:
|
51 |
-
st.session_state.questions = []
|
52 |
-
|
53 |
-
if "answers" not in st.session_state:
|
54 |
-
st.session_state.answers = []
|
55 |
-
|
56 |
-
if "input" not in st.session_state:
|
57 |
-
st.session_state.input = ""
|
58 |
-
|
59 |
-
st.markdown("""
|
60 |
-
<style>
|
61 |
-
.block-container {
|
62 |
-
padding-top: 32px;
|
63 |
-
padding-bottom: 32px;
|
64 |
-
padding-left: 0;
|
65 |
-
padding-right: 0;
|
66 |
-
}
|
67 |
-
.element-container img {
|
68 |
-
background-color: #000000;
|
69 |
-
}
|
70 |
-
|
71 |
-
.main-header {
|
72 |
-
font-size: 24px;
|
73 |
-
}
|
74 |
-
</style>
|
75 |
-
""", unsafe_allow_html=True)
|
76 |
-
|
77 |
-
# Load the model outside the handle_input() function
|
78 |
-
with open('model_saved.pkl', 'rb') as f:
|
79 |
-
model = pickle.load(f)
|
80 |
-
if not isinstance(model, str):
|
81 |
-
st.error("The loaded model is not valid.")
|
82 |
-
|
83 |
-
def write_top_bar():
|
84 |
-
col1, col2, col3 = st.columns([1,10,2])
|
85 |
-
with col1:
|
86 |
-
st.image(AI_ICON, use_column_width='always')
|
87 |
-
with col2:
|
88 |
-
header = "Cogwise Intelligent Assistant"
|
89 |
-
st.write(f"<h3 class='main-header'>{header}</h3>", unsafe_allow_html=True)
|
90 |
-
with col3:
|
91 |
-
clear = st.button("Clear Chat")
|
92 |
-
return clear
|
93 |
-
|
94 |
-
clear = write_top_bar()
|
95 |
-
|
96 |
-
if clear:
|
97 |
-
st.session_state.questions = []
|
98 |
-
st.session_state.answers = []
|
99 |
-
st.session_state.input = ""
|
100 |
-
st.session_state["chat_history"] = []
|
101 |
-
|
102 |
-
def handle_input():
|
103 |
-
input = st.session_state.input
|
104 |
-
question_with_id = {
|
105 |
-
'question': input,
|
106 |
-
'id': len(st.session_state.questions)
|
107 |
-
}
|
108 |
-
st.session_state.questions.append(question_with_id)
|
109 |
-
|
110 |
-
chat_history = st.session_state["chat_history"]
|
111 |
-
if len(chat_history) == MAX_HISTORY_LENGTH:
|
112 |
-
chat_history = chat_history[:-1]
|
113 |
-
|
114 |
-
prompt = input
|
115 |
-
answer = model # Replace the predict() method with the model itself
|
116 |
-
|
117 |
-
chat_history.append((input, answer))
|
118 |
-
|
119 |
-
st.session_state.answers.append({
|
120 |
-
'answer': answer,
|
121 |
-
'id': len(st.session_state.questions)
|
122 |
-
})
|
123 |
-
st.session_state.input = ""
|
124 |
-
|
125 |
-
def write_user_message(md):
|
126 |
-
col1, col2 = st.columns([1,12])
|
127 |
-
|
128 |
-
with col1:
|
129 |
-
st.image(USER_ICON, use_column_width='always')
|
130 |
-
with col2:
|
131 |
-
st.warning(md['question'])
|
132 |
-
|
133 |
-
def render_answer(answer):
|
134 |
-
col1, col2 = st.columns([1,12])
|
135 |
-
with col1:
|
136 |
-
st.image(AI_ICON, use_column_width='always')
|
137 |
-
with col2:
|
138 |
-
st.info(answer)
|
139 |
-
|
140 |
-
def write_chat_message(md, q):
|
141 |
-
chat = st.container()
|
142 |
-
with chat:
|
143 |
-
render_answer(md['answer'])
|
144 |
-
|
145 |
-
with st.container():
|
146 |
-
for (q, a) in zip(st.session_state.questions, st.session_state.answers):
|
147 |
-
write_user_message(q)
|
148 |
-
write_chat_message(a, q)
|
149 |
-
|
150 |
-
st.markdown('---')
|
151 |
-
input = st.text_input("You are talking to an AI, ask any question.", key="input", on_change=handle_input)
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def chat_with_model(input_text):
|
5 |
+
# Call the chat model and return the response
|
6 |
+
response = chat_model.predict(input_text)
|
7 |
+
return response
|
8 |
+
|
9 |
+
# Load the chat model using Gradio
|
10 |
+
chat_model = gr.Interface.load("models/CogwiseAI/CogwiseAI-chatwithMS")
|
11 |
+
|
12 |
+
# Create a Streamlit app
|
13 |
+
def main():
|
14 |
+
st.title("Cogwise Intelligent Assistant")
|
15 |
+
st.markdown("---")
|
16 |
+
|
17 |
+
# Create a text input for user interaction
|
18 |
+
input_text = st.text_input("You are talking to an AI, ask any question.")
|
19 |
+
|
20 |
+
# Process user input and display the response
|
21 |
+
if st.button("Ask"):
|
22 |
+
with st.spinner("Thinking..."):
|
23 |
+
response = chat_with_model(input_text)
|
24 |
+
st.markdown("---")
|
25 |
+
st.write(response)
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|