corbear67 commited on
Commit
bda145e
ยท
verified ยท
1 Parent(s): e1430ab

Create app.py

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