Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sentence_transformers import models
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
res = pd.read_csv('qa2.csv')
|
| 9 |
+
|
| 10 |
+
# Load pre-computed embeddings
|
| 11 |
+
with open("embeddings_words.pkl", "rb") as f:
|
| 12 |
+
embedded_texts = pickle.load(f)
|
| 13 |
+
|
| 14 |
+
# Define model
|
| 15 |
+
model_name = 'kornwtp/simcse-model-phayathaibert'
|
| 16 |
+
word_embedding_model = models.Transformer(model_name)
|
| 17 |
+
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), pooling_mode='cls') # Use CLS token for representation
|
| 18 |
+
model = SentenceTransformer(modules=[word_embedding_model, pooling_model])
|
| 19 |
+
|
| 20 |
+
# Streamlit UI setup with custom CSS for styling
|
| 21 |
+
st.title("Thai Chat Bot", anchor="top")
|
| 22 |
+
st.markdown("""
|
| 23 |
+
<style>
|
| 24 |
+
.css-1kyxreq { display: none; } # Hide the Streamlit default hamburger menu
|
| 25 |
+
.stApp { background-color: #F4F8FC; }
|
| 26 |
+
.stChatMessage-User { background-color: #4CAF50; color: white; padding: 15px; border-radius: 12px; margin-bottom: 10px; }
|
| 27 |
+
.stChatMessage-Assistant { background-color: #2196F3; color: white; padding: 15px; border-radius: 12px; margin-bottom: 10px; }
|
| 28 |
+
.stButton { background-color: #4CAF50; color: white; padding: 12px 25px; font-size: 18px; border-radius: 12px; }
|
| 29 |
+
.stTextInput { border-radius: 12px; padding: 10px; font-size: 16px; }
|
| 30 |
+
.stTextInput input { background-color: #f7f7f7; border: none; color: #333; }
|
| 31 |
+
.stMarkdown { font-size: 18px; font-family: 'Arial', sans-serif; line-height: 1.5; }
|
| 32 |
+
</style>
|
| 33 |
+
""", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
# Initialize session state for messages
|
| 36 |
+
if "messages" not in st.session_state:
|
| 37 |
+
st.session_state.messages = []
|
| 38 |
+
|
| 39 |
+
# Display existing chat messages
|
| 40 |
+
for message in st.session_state.messages:
|
| 41 |
+
with st.chat_message(message["role"]):
|
| 42 |
+
st.markdown(message["content"])
|
| 43 |
+
|
| 44 |
+
# Display a greeting message
|
| 45 |
+
with st.chat_message("ai"):
|
| 46 |
+
st.write("สวัสดี! 😊")
|
| 47 |
+
|
| 48 |
+
# Get user input
|
| 49 |
+
if prompt := st.chat_input("พิมพ์ข้อความที่นี่ ..."):
|
| 50 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 51 |
+
with st.chat_message("user"):
|
| 52 |
+
st.markdown(prompt)
|
| 53 |
+
|
| 54 |
+
# Show a loading spinner while processing
|
| 55 |
+
with st.spinner("กำลังค้นหาคำตอบ..."):
|
| 56 |
+
# Encode the user's prompt and calculate similarities
|
| 57 |
+
b = model.encode([prompt], normalize_embeddings=True)
|
| 58 |
+
inner_products = np.inner(b, embedded_texts) # Calculate inner products
|
| 59 |
+
|
| 60 |
+
# Get the index of the highest value
|
| 61 |
+
top_index = np.argmax(inner_products)
|
| 62 |
+
inner_products = inner_products.flatten()
|
| 63 |
+
similarity_percent = str(round(inner_products[top_index],2))
|
| 64 |
+
answer = f"{similarity_percent}% : {res['A'][top_index]}"
|
| 65 |
+
|
| 66 |
+
with st.chat_message("assistant"):
|
| 67 |
+
st.write(answer)
|
| 68 |
+
|
| 69 |
+
# Save the assistant's answer in session state
|
| 70 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 71 |
+
|
| 72 |
+
st.success("คำตอบเสร็จสิ้นแล้ว! 😊", icon="✅")
|