Spaces:
Running
Running
Upload 2 files
Browse files- requirements.txt +5 -0
- streamlit_app.py +132 -0
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
huggingface_hub
|
5 |
+
sentencepiece
|
streamlit_app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import MBartForConditionalGeneration, MBartTokenizer
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# ตั้งค่าหน้าเว็บ
|
7 |
+
st.set_page_config(page_title="ABSA Sentiment Analysis", layout="centered")
|
8 |
+
|
9 |
+
st.markdown(
|
10 |
+
"""
|
11 |
+
<style>
|
12 |
+
.container {
|
13 |
+
max-width: 700px;
|
14 |
+
margin: auto;
|
15 |
+
border-radius: 10px;
|
16 |
+
background-color: #f9f9f9;
|
17 |
+
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
18 |
+
}
|
19 |
+
.content {
|
20 |
+
text-align: justify;
|
21 |
+
line-height: 1.6;
|
22 |
+
}
|
23 |
+
.under {
|
24 |
+
text-decoration-line: underline;
|
25 |
+
text-decoration-style: double;
|
26 |
+
}
|
27 |
+
</style>
|
28 |
+
""",
|
29 |
+
unsafe_allow_html=True,
|
30 |
+
)
|
31 |
+
|
32 |
+
# โหลดโมเดลจาก Hugging Face
|
33 |
+
@st.cache_resource
|
34 |
+
def load_model():
|
35 |
+
try:
|
36 |
+
model_path = hf_hub_download(repo_id="firstmetis/absa_it", filename="model.pth")
|
37 |
+
|
38 |
+
model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50")
|
39 |
+
|
40 |
+
tokenizer = MBartTokenizer.from_pretrained("facebook/mbart-large-50")
|
41 |
+
special_tokens = ['<SYMBOL>', '<ASPECT>', '<OPINION>', '<POS>', '<NEG>', '<NEU>']
|
42 |
+
tokenizer.add_special_tokens({'additional_special_tokens': special_tokens})
|
43 |
+
|
44 |
+
model.resize_token_embeddings(len(tokenizer))
|
45 |
+
model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
46 |
+
|
47 |
+
model.eval()
|
48 |
+
return model, tokenizer
|
49 |
+
|
50 |
+
except Exception as e:
|
51 |
+
st.error(f"❌ เกิดข้อผิดพลาดขณะโหลดโมเดล: {e}")
|
52 |
+
return None, None
|
53 |
+
|
54 |
+
# โหลดโมเดลและ tokenizer
|
55 |
+
model, tokenizer = load_model()
|
56 |
+
|
57 |
+
# ตรวจสอบว่าโหลดสำเร็จหรือไม่
|
58 |
+
if model is not None and tokenizer is not None:
|
59 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
60 |
+
model.to(device)
|
61 |
+
|
62 |
+
def generate_text(input_text):
|
63 |
+
input_ids = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=512).input_ids
|
64 |
+
input_ids = input_ids.to(device)
|
65 |
+
|
66 |
+
with torch.no_grad():
|
67 |
+
outputs = model.generate(
|
68 |
+
input_ids,
|
69 |
+
num_beams=4,
|
70 |
+
do_sample=True,
|
71 |
+
temperature=1.2,
|
72 |
+
top_k=50,
|
73 |
+
top_p=0.95,
|
74 |
+
num_return_sequences=4,
|
75 |
+
max_length=50,
|
76 |
+
return_dict_in_generate=True,
|
77 |
+
output_scores=True
|
78 |
+
)
|
79 |
+
|
80 |
+
sequences = outputs.sequences
|
81 |
+
scores = outputs.scores
|
82 |
+
|
83 |
+
output_texts = [
|
84 |
+
tokenizer.decode(seq, skip_special_tokens=False).replace("</s>", "").replace("<pad>", "").strip()
|
85 |
+
for seq in sequences
|
86 |
+
]
|
87 |
+
|
88 |
+
confidences = []
|
89 |
+
for seq_scores in scores:
|
90 |
+
last_token_logits = seq_scores[-1]
|
91 |
+
probs = torch.nn.functional.softmax(last_token_logits, dim=-1)
|
92 |
+
confidence = probs.max().item()
|
93 |
+
confidences.append(confidence)
|
94 |
+
|
95 |
+
return list(zip(output_texts, confidences))
|
96 |
+
|
97 |
+
# ส่วนหัวของเว็บแอป
|
98 |
+
st.title("📌 Aspect-based Sentiment Analysis (ABSA)")
|
99 |
+
st.markdown(
|
100 |
+
"""
|
101 |
+
<div class='content'>
|
102 |
+
<h4>📍 วิธีการใช้งานเว็บไซต์</h4>
|
103 |
+
<p>
|
104 |
+
 1. เลือกพาดหัวข่าวเกี่ยวกับหุ้นที่สนใจโดยมีเงื่อนไขดังนี้</br>
|
105 |
+
   - เป็นข่าวหุ้นไทยในปี พ.ศ.2566-2567</br>
|
106 |
+
   - เป็นข่าวหุ้นไทยที่มีสัญลักษณ์หุ้นชัดเจน</br>
|
107 |
+
   - เป็นข่าวหุ้นไทยที่มีการออกข่าวค่อนข้างบ่อย</br>
|
108 |
+
    <u class="under">ตัวอย่าง</u> : TISCO ปันผลดี เหมาะสะสม บล.ดีบีเอสฯให้เป้า 118 บ.</br>
|
109 |
+
 2. นำพาดหัวข่าวใส่ลงช่องว่างด้านล่าง</br>
|
110 |
+
 3. กดปุ่ม Apply เพื่อวิเคราะห์
|
111 |
+
</p>
|
112 |
+
</div>
|
113 |
+
""",
|
114 |
+
unsafe_allow_html=True,
|
115 |
+
)
|
116 |
+
st.markdown("ใส่พาดหัวข่าวหุ้น เพื่อวิเคราะห์ Sentiment")
|
117 |
+
|
118 |
+
# กล่องรับข้อความ
|
119 |
+
user_input = st.text_input("✍️ ใส่ข้อความตรงนี้ :", "")
|
120 |
+
|
121 |
+
# ปุ่ม Apply
|
122 |
+
if st.button("Apply"):
|
123 |
+
if user_input:
|
124 |
+
results = generate_text(user_input)
|
125 |
+
|
126 |
+
# 🔹 แสดงผลลัพธ์ทั้งหมด พร้อมเปอร์เซ็นต์ความมั่นใจ
|
127 |
+
st.markdown("### 🔍 ผลลัพธ์:")
|
128 |
+
for i, (text, confidence) in enumerate(results, 1):
|
129 |
+
st.markdown(f"**🔹 ผลลัพธ์ {i}:** {text} \n📌 **ความมั่นใจ:** {confidence:.2%}")
|
130 |
+
|
131 |
+
else:
|
132 |
+
st.warning("⚠️ กรุณากรอกข้อความก่อนกด Apply")
|