Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
# ๊ฐ์ ๋ถ์์ฉ ๋ชจ๋ธ
|
7 |
+
emotion_model = AutoModelForSequenceClassification.from_pretrained("beomi/KcELECTRA-base", num_labels=3)
|
8 |
+
emotion_tokenizer = AutoTokenizer.from_pretrained("beomi/KcELECTRA-base")
|
9 |
+
emotion_labels = ['๋ถ์ ', '์ค๋ฆฝ', '๊ธ์ ']
|
10 |
+
|
11 |
+
# ํ
์คํธ ์์ฑ์ฉ GPT ๋ชจ๋ธ
|
12 |
+
gpt_model = AutoModelForCausalLM.from_pretrained("skt/kogpt2-base-v2")
|
13 |
+
gpt_tokenizer = AutoTokenizer.from_pretrained("skt/kogpt2-base-v2")
|
14 |
+
|
15 |
+
# ๊ฐ์ ๋ถ์ ํจ์
|
16 |
+
def predict_emotion(text):
|
17 |
+
inputs = emotion_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = emotion_model(**inputs)
|
20 |
+
probs = F.softmax(outputs.logits, dim=1)
|
21 |
+
pred = torch.argmax(probs, dim=1).item()
|
22 |
+
return emotion_labels[pred]
|
23 |
+
|
24 |
+
# GPT ์ด์ด์ฐ๊ธฐ ํจ์
|
25 |
+
def emotional_gpt(user_input):
|
26 |
+
emotion = predict_emotion(user_input)
|
27 |
+
|
28 |
+
if emotion == "๊ธ์ ":
|
29 |
+
prompt = "๊ธฐ๋ถ ์ข์ ํ๋ฃจ์๋ค. "
|
30 |
+
elif emotion == "๋ถ์ ":
|
31 |
+
prompt = "์ฐ์ธํ ๊ธฐ๋ถ์ผ๋ก ์์๋ ํ๋ฃจ, "
|
32 |
+
else:
|
33 |
+
prompt = "ํ๋ฒํ ํ๋ฃจ๊ฐ ์์๋์๋ค. "
|
34 |
+
|
35 |
+
prompt += user_input
|
36 |
+
|
37 |
+
input_ids = gpt_tokenizer.encode(prompt, return_tensors="pt")
|
38 |
+
output = gpt_model.generate(input_ids, max_length=150, do_sample=True, temperature=0.8, top_k=50)
|
39 |
+
result = gpt_tokenizer.decode(output[0], skip_special_tokens=True)
|
40 |
+
|
41 |
+
return f"๐ง ๊ฐ์ ๋ถ์ ๊ฒฐ๊ณผ: {emotion}\n\nโ๏ธ GPT๊ฐ ์ด์ด ์ด ๊ธ:\n{result}"
|
42 |
+
|
43 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
44 |
+
gr.Interface(
|
45 |
+
fn=emotional_gpt,
|
46 |
+
inputs=gr.Textbox(lines=3, label="โ๏ธ ๊ฐ์ ์ ๋ด์ ๋ฌธ์ฅ์ ์
๋ ฅํด์ฃผ์ธ์!", placeholder="์: ์ค๋ ๋๋ฌด ์ธ๋ก์ ์ด"),
|
47 |
+
outputs="text",
|
48 |
+
title="๐ญ ๊ฐ์ ํ GPT ํ๊ธ ์๋ฌธ AI",
|
49 |
+
description="๐ง ๊ฐ์ ์ ๋จผ์ ํ์
ํ๊ณ โจ ๊ทธ ๊ฐ์ ์ ์ด์ธ๋ฆฌ๋ ๋ฌธ์ฅ์ ์ด์ด์ ์์ฑํด์ค๋๋ค!",
|
50 |
+
theme="soft",
|
51 |
+
examples=[
|
52 |
+
["๊ธฐ๋ถ์ด ๋๋ฌด ์ข์์ด"],
|
53 |
+
["์ง์ง ์ธ๋กญ๊ณ ํ๋ ํ๋ฃจ์์ด"],
|
54 |
+
["ํ์๊ฐ ๊ทธ๋ฅ ๊ทธ๋ฌ์ด"]
|
55 |
+
]
|
56 |
+
).launch()
|