Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +42 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import BertConfig, BertForSequenceClassification, BertTokenizer
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
|
7 |
+
# Load the model and tokenizer
|
8 |
+
def load_model():
|
9 |
+
tokenizer = BertTokenizer.from_pretrained('beomi/kcbert-base')
|
10 |
+
config = BertConfig.from_pretrained('beomi/kcbert-base', num_labels=7)
|
11 |
+
model = BertForSequenceClassification.from_pretrained('beomi/kcbert-base', config=config)
|
12 |
+
model_state_dict = torch.load('sentiment7_model_acc8878.pth', map_location=torch.device('cpu')) # cpu μ¬μ©
|
13 |
+
model.load_state_dict(model_state_dict)
|
14 |
+
model.eval()
|
15 |
+
return model, tokenizer
|
16 |
+
|
17 |
+
model, tokenizer = load_model()
|
18 |
+
|
19 |
+
# Define the inference function
|
20 |
+
def inference(input_doc):
|
21 |
+
inputs = tokenizer(input_doc, return_tensors='pt')
|
22 |
+
outputs = model(**inputs)
|
23 |
+
probs = torch.softmax(outputs.logits, dim=1).squeeze().tolist()
|
24 |
+
class_idx = {'곡ν¬': 0, 'λλ': 1, 'λΆλ
Έ': 2, 'μ¬ν': 3, 'μ€λ¦½': 4, 'ν볡': 5, 'νμ€': 6}
|
25 |
+
results = {class_name: prob for class_name, prob in zip(class_idx, probs)}
|
26 |
+
# Find the class with the highest probability
|
27 |
+
max_prob_class = max(results, key=results.get)
|
28 |
+
max_prob = results[max_prob_class]
|
29 |
+
# Display results
|
30 |
+
return [results, f"κ°μ₯ κ°νκ² λνλ κ°μ : {max_prob_class}"]
|
31 |
+
''' for class_name, prob in results.items():
|
32 |
+
print(f"{class_name}: {prob:.2%}")'''
|
33 |
+
|
34 |
+
# Set up the Streamlit interface
|
35 |
+
st.title('κ°μ λΆμ(Sentiment Analysis)')
|
36 |
+
st.markdown('<small style="color:grey;">κΈμ λνλ 곡ν¬, λλ, λΆλ
Έ, μ¬ν, μ€λ¦½, ν볡, νμ€μ μ λλ₯Ό λΉμ¨λ‘ μλ €λ립λλ€.</small>', unsafe_allow_html=True)
|
37 |
+
user_input = st.text_area("μ΄ κ³³μ κΈ μ
λ ₯(100μ μ΄ν κΆμ₯):")
|
38 |
+
if st.button('μμ'):
|
39 |
+
result = inference(user_input)
|
40 |
+
st.write(result[0])
|
41 |
+
st.write(result[1])
|
42 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
numpy
|