Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +149 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
import io
|
6 |
+
from torch.utils.data import DataLoader, Dataset
|
7 |
+
from torch.optim import AdamW
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
|
10 |
+
# ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ก๋
|
11 |
+
MODEL_NAME = "beomi/kcbert-base"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
|
14 |
+
|
15 |
+
# ๋ฐ์ดํฐ์
ํด๋์ค ์ ์
|
16 |
+
class CustomDataset(Dataset):
|
17 |
+
def __init__(self, dataframe, tokenizer, max_len=128):
|
18 |
+
self.tokenizer = tokenizer
|
19 |
+
self.data = dataframe
|
20 |
+
self.max_len = max_len
|
21 |
+
|
22 |
+
def __len__(self):
|
23 |
+
return len(self.data)
|
24 |
+
|
25 |
+
def __getitem__(self, index):
|
26 |
+
item = self.data.iloc[index]
|
27 |
+
description = str(item['description'])
|
28 |
+
label = item['label']
|
29 |
+
|
30 |
+
encoding = self.tokenizer.encode_plus(
|
31 |
+
description,
|
32 |
+
add_special_tokens=True,
|
33 |
+
max_length=self.max_len,
|
34 |
+
return_token_type_ids=False,
|
35 |
+
padding='max_length',
|
36 |
+
truncation=True,
|
37 |
+
return_attention_mask=True,
|
38 |
+
return_tensors='pt',
|
39 |
+
)
|
40 |
+
|
41 |
+
return {
|
42 |
+
'input_ids': encoding['input_ids'].flatten(),
|
43 |
+
'attention_mask': encoding['attention_mask'].flatten(),
|
44 |
+
'labels': torch.tensor(label, dtype=torch.long)
|
45 |
+
}
|
46 |
+
|
47 |
+
# ํ๋ จ ๋ฐ์ดํฐ ์ค๋น ๋ฐ ๋ชจ๋ธ ํ๋ จ
|
48 |
+
def train_model():
|
49 |
+
csv_data = """description,gender
|
50 |
+
"๊ทธ๋ ์ถ๊ตฌ๋ฅผ ์ ๋ง ์ข์ํ๊ณ , ๊ทผ์ก์ง์ ๋ชธ๋งค๋ฅผ ๊ฐ์ก๋ค.",๋จ์
|
51 |
+
"๊ทธ๋
๋ ๊ธด ๋จธ๋ฆฌ๋ฅผ ๊ฐ์ก๊ณ , ๋ถํ์ ์ํผ์ค๋ฅผ ์
์๋ค.",์ฌ์
|
52 |
+
"์งง์ ๋จธ๋ฆฌ์ ์ ์ฅ์ ์
์ ๊ทธ๋ ํ์์ ์ฐธ์ํ๋ค.",๋จ์
|
53 |
+
"์๋ฆ๋ค์ด ๋ชฉ์๋ฆฌ๋ก ๋
ธ๋ํ๋ ๊ทธ๋
๋ ๊ฐ์๋ค.",์ฌ์
|
54 |
+
"๊ทธ์ ์ทจ๋ฏธ๋ ์๋์ฐจ ์ ๋น์ ์ปดํจํฐ ๊ฒ์์ด๋ค.",๋จ์
|
55 |
+
"๊ทธ๋
๋ ์ฌ์ธํ ์๊ธธ๋ก ์๊ธฐ ์ธํ์ ๋ง๋ค์๋ค.",์ฌ์
|
56 |
+
"๊ตฐ๋์์ ๋ง ์ ๋ํ ๊ทธ๋ ์ฉ์ฉํด ๋ณด์๋ค.",๋จ์
|
57 |
+
"๊ทธ๋
๋ ์น๊ตฌ๋ค๊ณผ ์๋ค ๋ ๋ ๊ฒ์ ์ข์ํ๋ค.",์ฌ์
|
58 |
+
"๊ฐ๋ ฅํ ๋ฆฌ๋์ญ์ผ๋ก ํ์ ์ด๋๋ ๋ชจ์ต์ด ์ธ์์ ์ด์๋ค.",๋จ์
|
59 |
+
"์์ ์ด ์ง์ ๋ง๋ ์ฟ ํค๋ฅผ ์ฃผ๋ณ์ ๋๋์ด์ฃผ๊ณค ํ๋ค.",์ฌ์
|
60 |
+
"์์ผ์ฐฌ",์ฌ์
|
61 |
+
"""
|
62 |
+
|
63 |
+
data = pd.read_csv(io.StringIO(csv_data))
|
64 |
+
data['label'] = data['gender'].apply(lambda x: 0 if x == '๋จ์' else 1)
|
65 |
+
train_data, _ = train_test_split(data, test_size=0.2, random_state=42)
|
66 |
+
|
67 |
+
train_dataset = CustomDataset(train_data, tokenizer)
|
68 |
+
train_loader = DataLoader(train_dataset, batch_size=2)
|
69 |
+
|
70 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
71 |
+
model.to(device)
|
72 |
+
|
73 |
+
optimizer = AdamW(model.parameters(), lr=5e-5)
|
74 |
+
|
75 |
+
print("๋ชจ๋ธ ํ๋ จ ์์...")
|
76 |
+
model.train()
|
77 |
+
for epoch in range(3):
|
78 |
+
for batch in train_loader:
|
79 |
+
optimizer.zero_grad()
|
80 |
+
input_ids = batch['input_ids'].to(device)
|
81 |
+
attention_mask = batch['attention_mask'].to(device)
|
82 |
+
labels = batch['labels'].to(device)
|
83 |
+
|
84 |
+
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
|
85 |
+
loss = outputs.loss
|
86 |
+
loss.backward()
|
87 |
+
optimizer.step()
|
88 |
+
print(f"Epoch {epoch + 1} ์๋ฃ")
|
89 |
+
|
90 |
+
print("๋ชจ๋ธ ํ๋ จ ์๋ฃ!")
|
91 |
+
|
92 |
+
# ์์ธก ํจ์
|
93 |
+
def predict_gender(text):
|
94 |
+
if not text.strip():
|
95 |
+
return "ํ
์คํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
96 |
+
|
97 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
98 |
+
model.eval()
|
99 |
+
|
100 |
+
encoding = tokenizer.encode_plus(
|
101 |
+
text,
|
102 |
+
add_special_tokens=True,
|
103 |
+
max_length=128,
|
104 |
+
return_token_type_ids=False,
|
105 |
+
padding='max_length',
|
106 |
+
truncation=True,
|
107 |
+
return_attention_mask=True,
|
108 |
+
return_tensors='pt',
|
109 |
+
)
|
110 |
+
|
111 |
+
input_ids = encoding['input_ids'].to(device)
|
112 |
+
attention_mask = encoding['attention_mask'].to(device)
|
113 |
+
|
114 |
+
with torch.no_grad():
|
115 |
+
outputs = model(input_ids, attention_mask=attention_mask)
|
116 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
|
117 |
+
prediction = torch.argmax(outputs.logits, dim=1).flatten().item()
|
118 |
+
confidence = probabilities[0][prediction].item()
|
119 |
+
|
120 |
+
gender = "๋จ์" if prediction == 0 else "์ฌ์"
|
121 |
+
return f"์์ธก ์ฑ๋ณ: {gender} (์ ๋ขฐ๋: {confidence:.2%})"
|
122 |
+
|
123 |
+
# ์ฑ ์์ ์ ๋ชจ๋ธ ํ๋ จ
|
124 |
+
print("์ฑ ์ด๊ธฐํ ์ค...")
|
125 |
+
train_model()
|
126 |
+
|
127 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
128 |
+
iface = gr.Interface(
|
129 |
+
fn=predict_gender,
|
130 |
+
inputs=gr.Textbox(
|
131 |
+
lines=3,
|
132 |
+
placeholder="์ฑ๋ณ์ ์์ธกํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์.\n์: '๊ทธ๋ ์ถ๊ตฌ๋ฅผ ์ข์ํ๊ณ ๊ทผ์ก์ง์ด๋ค.'",
|
133 |
+
label="ํ
์คํธ ์
๋ ฅ"
|
134 |
+
),
|
135 |
+
outputs=gr.Textbox(label="์์ธก ๊ฒฐ๊ณผ"),
|
136 |
+
title="๐ค AI ์ฑ๋ณ ์๏ฟฝ๏ฟฝ๏ฟฝ๊ธฐ",
|
137 |
+
description="์
๋ ฅ๋ ํ
์คํธ๋ฅผ ๋ฐํ์ผ๋ก ์ฑ๋ณ์ ์์ธกํฉ๋๋ค.",
|
138 |
+
examples=[
|
139 |
+
["๊ทธ๋ ์ถ๊ตฌ๋ฅผ ์ ๋ง ์ข์ํ๊ณ , ๊ทผ์ก์ง์ ๋ชธ๋งค๋ฅผ ๊ฐ์ก๋ค."],
|
140 |
+
["๊ทธ๋
๋ ๊ธด ๋จธ๋ฆฌ๋ฅผ ๊ฐ์ก๊ณ , ๋ถํ์ ์ํผ์ค๋ฅผ ์
์๋ค."],
|
141 |
+
["์งง์ ๋จธ๋ฆฌ์ ์ ์ฅ์ ์
์ ๊ทธ๋ ํ์์ ์ฐธ์ํ๋ค."],
|
142 |
+
["์๋ฆ๋ค์ด ๋ชฉ์๋ฆฌ๋ก ๋
ธ๋ํ๋ ๊ทธ๋
๋ ๊ฐ์๋ค."]
|
143 |
+
],
|
144 |
+
theme=gr.themes.Soft()
|
145 |
+
)
|
146 |
+
|
147 |
+
# ์ฑ ์คํ
|
148 |
+
if __name__ == "__main__":
|
149 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|
4 |
+
pandas
|
5 |
+
scikit-learn
|