Files changed (3) hide show
  1. app.py +0 -193
  2. e_text_best_model.bin +0 -3
  3. p_text_best_model.bin +0 -3
app.py DELETED
@@ -1,193 +0,0 @@
1
- # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
2
- import os
3
- import pandas as pd # pandas๋Š” ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” ์ง์ ‘ ์‚ฌ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ด€๋ จ ์œ ํ‹ธ๋ฆฌํ‹ฐ๋กœ ๋‚จ๊ฒจ๋‘˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
4
- import numpy as np
5
- import torch
6
- import torch.nn as nn
7
- import torch.nn.functional as F
8
- from torch.utils.data import Dataset, DataLoader # DataLoader์™€ Dataset์€ ์ถ”๋ก  ์‹œ ์ง์ ‘ ์‚ฌ์šฉ๋˜์ง„ ์•Š์ง€๋งŒ, ๋ชจ๋ธ ์ •์˜์— ํ•„์š”ํ•  ์ˆ˜ ์žˆ์–ด ๋‚จ๊ฒจ๋‘ 
9
- from transformers import LongformerForSequenceClassification, AutoTokenizer
10
- import gradio as gr
11
-
12
- # =======================================================
13
- # 1. ์ „์—ญ ์„ค์ • ๋ฐ ์ƒ์ˆ˜ ์ •์˜
14
- # =======================================================
15
- MODEL_NAME = 'kiddothe2b/longformer-mini-1024' # HuggingFace ๋ชจ๋ธ ์ด๋ฆ„
16
- MAX_LEN = 1024 # ๋ชจ๋ธ ์ž…๋ ฅ ์ตœ๋Œ€ ๊ธธ์ด
17
-
18
- # GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ํ™•์ธ ๋ฐ ๋””๋ฐ”์ด์Šค ์„ค์ •
19
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
20
- print(f"Using device: {device}")
21
-
22
- # ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ (์ถ”๋ก  ์‹œ ํ•„์š”)
23
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
24
-
25
- # =======================================================
26
- # 2. PyTorch ๋ฐ์ดํ„ฐ์…‹ ์ •์˜ (ํ•™์Šต ์‹œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํด๋ž˜์Šค. ์ถ”๋ก  ์‹œ ์ง์ ‘ ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ๋งŒ๋“ค์ง€๋Š” ์•Š์Œ)
27
- # =======================================================
28
- # ์ด ํด๋ž˜์Šค๋Š” ๋ชจ๋ธ์ด ํ•™์Šต๋  ๋•Œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
29
- # ์ถ”๋ก  ์‹œ์—๋Š” ๋‹จ์ผ ํ…์ŠคํŠธ ์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋ฏ€๋กœ ์ง์ ‘ DataLoader๋ฅผ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.
30
- # ํ•˜์ง€๋งŒ ๋ชจ๋ธ์ด ๊ธฐ๋Œ€ํ•˜๋Š” ์ž…๋ ฅ ํ˜•ํƒœ๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด encoding ๊ณผ์ •์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
31
- class DepressionDataset(Dataset):
32
- def __init__(self, texts, labels, tokenizer, max_len):
33
- self.texts = texts
34
- self.labels = labels
35
- self.tokenizer = tokenizer
36
- self.max_len = max_len
37
-
38
- def __len__(self):
39
- return len(self.texts)
40
-
41
- def __getitem__(self, item):
42
- text = str(self.texts[item])
43
- label = self.labels[item]
44
- encoding = self.tokenizer.encode_plus(
45
- text,
46
- add_special_tokens=True,
47
- max_length=self.max_len,
48
- return_token_type_ids=False,
49
- padding='max_length',
50
- truncation=True,
51
- return_attention_mask=True,
52
- return_tensors='pt',
53
- )
54
- return {
55
- 'input_ids': encoding['input_ids'].flatten(),
56
- 'attention_mask': encoding['attention_mask'].flatten(),
57
- 'labels': torch.tensor(label, dtype=torch.long)
58
- }
59
-
60
- # =======================================================
61
- # 3. ๋ชจ๋ธ ๋กœ๋”ฉ (ํ•™์Šต๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œ)
62
- # =======================================================
63
- print("\n--- Loading models for inference ---")
64
-
65
- # ๋ชจ๋ธ ํŒŒ์ผ ๊ฒฝ๋กœ (saved_models ํด๋”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •)
66
- # ์ด์ „์— ์žˆ๋˜ save_dir ๋ณ€์ˆ˜๋Š” ์ด์ œ ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค.
67
- p_model_path = 'p_text_best_model.bin' # ํŒŒ์ผ๋ช…์ด ๋ฃจํŠธ์— ๋ฐ”๋กœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •
68
- e_model_path = 'e_text_best_model.bin' # ํŒŒ์ผ๋ช…์ด ๋ฃจํŠธ์— ๋ฐ”๋กœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •
69
-
70
- # ๋ชจ๋ธ ๋กœ๋”ฉ ๋ฐ ํ‰๊ฐ€ ๋ชจ๋“œ ์„ค์ •
71
- p_model_for_inference = None
72
- e_model_for_inference = None
73
-
74
- try:
75
- # ์ฐธ๊ฐ€์ž ๋ฐœํ™” ๋ชจ๋ธ (P-model) ๋กœ๋“œ
76
- if os.path.exists(p_model_path):
77
- p_model_for_inference = LongformerForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
78
- p_model_for_inference.load_state_dict(torch.load(p_model_path, map_location=device))
79
- p_model_for_inference.to(device)
80
- p_model_for_inference.eval() # ํ‰๊ฐ€ ๋ชจ๋“œ ์„ค์ •
81
- print(f"P-model loaded successfully from {p_model_path}")
82
- else:
83
- print(f"Warning: P-model file not found at {p_model_path}. Please ensure it's uploaded to the root directory.")
84
-
85
- # ์—˜๋ฆฌ ๋ฐœํ™” ๋ชจ๋ธ (E-model) ๋กœ๋“œ
86
- if os.path.exists(e_model_path):
87
- e_model_for_inference = LongformerForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
88
- e_model_for_inference.load_state_dict(torch.load(e_model_path, map_location=device))
89
- e_model_for_inference.to(device)
90
- e_model_for_inference.eval() # ํ‰๊ฐ€ ๋ชจ๋“œ ์„ค์ •
91
- print(f"E-model loaded successfully from {e_model_path}")
92
- else:
93
- print(f"Warning: E-model file not found at {e_model_path}. Please ensure it's uploaded to the root directory.")
94
-
95
- except Exception as e:
96
- print(f"Error loading models: {e}")
97
- # ๋ชจ๋ธ ๋กœ๋”ฉ ์‹คํŒจ ์‹œ, UI๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋„๋ก ์„ค์ •
98
- p_model_for_inference = None
99
- e_model_for_inference = None
100
-
101
- # =======================================================
102
- # 4. Gradio ์˜ˆ์ธก ํ•จ์ˆ˜ ์ •์˜
103
- # =======================================================
104
- def predict_depression(participant_text, ellie_text):
105
- # ๋ชจ๋ธ์ด ์ œ๋Œ€๋กœ ๋กœ๋“œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
106
- if p_model_for_inference is None or e_model_for_inference is None:
107
- return "**์˜ค๋ฅ˜:** ๋ชจ๋ธ์ด ๋กœ๋“œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ด€๏ฟฝ๏ฟฝ์ž์—๊ฒŒ ๋ฌธ์˜ํ•˜๊ฑฐ๋‚˜ ๋ชจ๋ธ ํŒŒ์ผ ์—…๋กœ๋“œ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
108
-
109
- # ์—˜๋ฆฌ ๋ฐœํ™” ์ „์ฒ˜๋ฆฌ (ํ•™์Šต ์‹œ์™€ ๋™์ผํ•œ ๋กœ์ง ์ ์šฉ)
110
- e_text_words = ellie_text.split()
111
- if len(e_text_words) > 0:
112
- ellie_text_processed = " ".join(e_text_words[len(e_text_words) // 2:])
113
- else:
114
- ellie_text_processed = ""
115
-
116
- # P-model ์˜ˆ์ธก
117
- p_encoding = tokenizer.encode_plus(
118
- participant_text,
119
- add_special_tokens=True,
120
- max_length=MAX_LEN,
121
- return_token_type_ids=False,
122
- padding='max_length',
123
- truncation=True,
124
- return_attention_mask=True,
125
- return_tensors='pt',
126
- )
127
- p_input_ids = p_encoding['input_ids'].to(device)
128
- p_attention_mask = p_encoding['attention_mask'].to(device)
129
-
130
- with torch.no_grad(): # ์ถ”๋ก  ์‹œ์—๋Š” ๊ทธ๋ผ๋””์–ธํŠธ ๊ณ„์‚ฐ ๋ถˆํ•„์š”
131
- p_outputs = p_model_for_inference(input_ids=p_input_ids, attention_mask=p_attention_mask)
132
- p_probs = F.softmax(p_outputs.logits, dim=1).cpu().numpy().flatten()
133
- p_pred_label = np.argmax(p_probs)
134
-
135
- # E-model ์˜ˆ์ธก
136
- e_encoding = tokenizer.encode_plus(
137
- ellie_text_processed,
138
- add_special_tokens=True,
139
- max_length=MAX_LEN,
140
- return_token_type_ids=False,
141
- padding='max_length',
142
- truncation=True,
143
- return_attention_mask=True,
144
- return_tensors='pt',
145
- )
146
- e_input_ids = e_encoding['input_ids'].to(device)
147
- e_attention_mask = e_encoding['attention_mask'].to(device)
148
-
149
- with torch.no_grad(): # ์ถ”๋ก  ์‹œ์—๋Š” ๊ทธ๋ผ๋””์–ธํŠธ ๊ณ„์‚ฐ ๋ถˆํ•„์š”
150
- e_outputs = e_model_for_inference(input_ids=e_input_ids, attention_mask=e_attention_mask)
151
- e_probs = F.softmax(e_outputs.logits, dim=1).cpu().numpy().flatten()
152
- e_pred_label = np.argmax(e_probs)
153
-
154
- # ์•™์ƒ๋ธ” (OR ์ „๋žต): ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์šฐ์šธ์ฆ(1)์œผ๋กœ ์˜ˆ์ธกํ•˜๋ฉด ์šฐ์šธ์ฆ์œผ๋กœ ๊ฐ„์ฃผ
155
- ensemble_pred_label = 1 if p_pred_label == 1 or e_pred_label == 1 else 0
156
-
157
- labels = ['Control (๋น„์šฐ์šธ)', 'Depressed (์šฐ์šธ)']
158
- ensemble_result = labels[ensemble_pred_label]
159
- p_model_result = labels[p_pred_label]
160
- e_model_result = labels[e_pred_label]
161
-
162
- return (f"**์ตœ์ข… ์•™์ƒ๋ธ” ์˜ˆ์ธก (OR ์ „๋žต): {ensemble_result}**\n\n"
163
- f" - ์ฐธ๊ฐ€์ž ๋ชจ๋ธ (P-longBERT) ์˜ˆ์ธก: {p_model_result} (ํ™•๋ฅ : Control={p_probs[0]:.2f}, Depressed={p_probs[1]:.2f})\n"
164
- f" - ์—˜๋ฆฌ ๋ชจ๋ธ (E-longBERT) ์˜ˆ์ธก: {e_model_result} (ํ™•๋ฅ : Control={e_probs[0]:.2f}, Depressed={e_probs[1]:.2f})\n\n"
165
- f"**์ฐธ๊ณ :**\n"
166
- f"- ์˜ˆ์ธก์€ ๊ฐ ๋Œ€ํ™” ๋‚ด์šฉ์—๋งŒ ๊ธฐ๋ฐ˜ํ•˜๋ฉฐ, ์‹ค์ œ ์ง„๋‹จ์€ ์ „๋ฌธ๊ฐ€์™€ ์ƒ๋‹ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\n"
167
- f"- GPU ํ™˜๊ฒฝ์—์„œ๋Š” ์˜ˆ์ธก์ด ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค."
168
- )
169
-
170
- # =======================================================
171
- # 5. Gradio UI ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ ๋ฐ ์‹คํ–‰
172
- # =======================================================
173
- print("\n--- Setting up Gradio UI ---")
174
-
175
- # ๋ชจ๋ธ์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๋“œ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋งŒ Gradio UI๋ฅผ ์‹คํ–‰
176
- if p_model_for_inference is not None and e_model_for_inference is not None:
177
- gr.Interface(
178
- fn=predict_depression,
179
- inputs=[
180
- gr.Textbox(lines=10, label="์ฐธ๊ฐ€์ž ๋ฐœํ™” ๋‚ด์šฉ (Participant's speech)", placeholder="์—ฌ๊ธฐ์— ์ฐธ๊ฐ€์ž์˜ ๋ฐœํ™” ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”..."),
181
- gr.Textbox(lines=10, label="์—˜๋ฆฌ ๋ฐœํ™” ๋‚ด์šฉ (Ellie's speech)", placeholder="์—ฌ๊ธฐ์— ์—˜๋ฆฌ(๊ฐ€์ƒ ์—์ด์ „ํŠธ)์˜ ๋ฐœํ™” ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”... (์ „์ฒด ๋‚ด์šฉ ์ค‘ ํ›„๋ฐ˜๋ถ€๋งŒ ์‚ฌ์šฉ๋จ)")
182
- ],
183
- outputs="markdown",
184
- title="DAIC-WOZ ์šฐ์šธ์ฆ ๊ฐ์ง€ ์•™์ƒ๋ธ” ๋ชจ๋ธ (GPU ๊ฐ€์†)",
185
- description=f"""์ด ์•ฑ์€ DAIC-WOZ ๋ฐ์ดํ„ฐ์…‹์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฐธ๊ฐ€์ž์™€ ๊ฐ€์ƒ ์—์ด์ „ํŠธ(์—˜๋ฆฌ)์˜ ๋Œ€ํ™” ๋‚ด์šฉ์„ ๋ถ„์„ํ•˜์—ฌ ์šฐ์šธ์ฆ ์—ฌ๋ถ€๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.
186
- P-longBERT (์ฐธ๊ฐ€์ž ๋ฐœํ™”)์™€ E-longBERT (์—˜๋ฆฌ ๋ฐœํ™”) ๋ชจ๋ธ์˜ ์•™์ƒ๋ธ” (OR ์ „๋žต) ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
187
- **GPU ํ™˜๊ฒฝ์—์„œ๋Š” ์˜ˆ์ธก์ด ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.**
188
- **์ฐธ๊ณ :** ์ด๋Š” AI ๋ชจ๋ธ์˜ ์˜ˆ์ธก์ผ ๋ฟ์ด๋ฉฐ, **์‹ค์ œ ์˜ํ•™์  ์ง„๋‹จ์€ ๋ฐ˜๋“œ์‹œ ์ „๋ฌธ๊ฐ€์™€ ์ƒ๋‹ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.**
189
- ์‚ฌ์šฉ ์ค‘์ธ ๋””๋ฐ”์ด์Šค: {device}
190
- """
191
- ).launch() # Hugging Face Spaces์—์„œ๋Š” share=True๊ฐ€ ํ•„์š” ์—†์Œ
192
- else:
193
- print("\nGradio UI could not be launched because models failed to load. Please check model files.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e_text_best_model.bin DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:accfd3aab9348aed05f32753969b48f9eae028f8f2139e1ec26d0c746cb0f0e4
3
- size 56324610
 
 
 
 
p_text_best_model.bin DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:a29208f799be73233fecbde5a3103c8df2de952d53dd071e4edc2a7eb73cdefe
3
- size 56324610