-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (68 loc) · 2.2 KB
/
app.py
File metadata and controls
88 lines (68 loc) · 2.2 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
from subprocess import PIPE, Popen
import streamlit as st
from transformers import pipeline
import sys
current_dir = os.getcwd()
vietsenti = Popen(
[f"{sys.executable}", "vietsenti.py"],
cwd=current_dir + "/VietSentiWordNet/",
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
)
segment = Popen(
[f"{sys.executable}", "segment.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE
)
### Ignore the first 34 lines
for i in range(0, 34):
ignore = vietsenti.stdout.readline()
ignore = segment.stdout.readline()
def word_segment(text):
input = text + "\n"
segment.stdin.write(input.encode())
segment.stdin.flush()
return segment.stdout.readline().decode()
def get_sentiment_words(text):
input = text + "\n"
vietsenti.stdin.write(input.encode())
vietsenti.stdin.flush()
output = vietsenti.stdout.readline()
output = vietsenti.stdout.readline()
output = vietsenti.stdout.readline()
output = vietsenti.stdout.readline()
output = vietsenti.stdout.readline()
### Extract sentiment word
output = output.decode()
first = output.find("Sentiment words: ")
last = output.find("SentencScore", first + 16)
sentiment_word = output[first + 16 : last - 5]
return sentiment_word.split(",")
def print_label(output):
def mapping(label):
if label == "LABEL_1":
return "Negative"
else:
return "Positive"
st.write(mapping(output[0]["label"]), "with score", output[0]["score"])
st.write(mapping(output[1]["label"]), "with score", output[1]["score"])
classifier = pipeline(
task="text-classification",
model="./SentimentPhoBERT-LoRA-256",
tokenizer="./SentimentPhoBERT-LoRA-256",
)
with st.form(key="my_form"):
text = st.text_input(label="Enter your text here")
submit_button = st.form_submit_button(label="Submit")
if submit_button:
text = word_segment(text)
sentiment_words = get_sentiment_words(text)
print_label(classifier(text, top_k=None))
st.write("Sentiment words:", ", ".join(sentiment_words))
vietsenti.stdin.close()
vietsenti.terminate()
vietsenti.wait(timeout=0.2)
segment.stdin.close()
segment.terminate()
segment.wait(timeout=0.2)
print("Done")