YEINJEONG commited on
Commit
a0d14d5
β€’
1 Parent(s): 40a5d47

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +42 -0
  2. 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