Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM | |
| import torch | |
| import torch.nn.functional as F | |
| # ๊ฐ์ ๋ถ์์ฉ ๋ชจ๋ธ | |
| emotion_model = AutoModelForSequenceClassification.from_pretrained("beomi/KcELECTRA-base", num_labels=3) | |
| emotion_tokenizer = AutoTokenizer.from_pretrained("beomi/KcELECTRA-base") | |
| emotion_labels = ['๋ถ์ ', '์ค๋ฆฝ', '๊ธ์ '] | |
| # ํ ์คํธ ์์ฑ์ฉ GPT ๋ชจ๋ธ | |
| gpt_model = AutoModelForCausalLM.from_pretrained("skt/kogpt2-base-v2") | |
| gpt_tokenizer = AutoTokenizer.from_pretrained("skt/kogpt2-base-v2") | |
| # ๊ฐ์ ๋ถ์ ํจ์ | |
| def predict_emotion(text): | |
| inputs = emotion_tokenizer(text, return_tensors="pt", truncation=True, padding=True) | |
| with torch.no_grad(): | |
| outputs = emotion_model(**inputs) | |
| probs = F.softmax(outputs.logits, dim=1) | |
| pred = torch.argmax(probs, dim=1).item() | |
| return emotion_labels[pred] | |
| # GPT ์ด์ด์ฐ๊ธฐ ํจ์ | |
| def emotional_gpt(user_input): | |
| emotion = predict_emotion(user_input) | |
| if emotion == "๊ธ์ ": | |
| prompt = "๊ธฐ๋ถ ์ข์ ํ๋ฃจ์๋ค. " | |
| elif emotion == "๋ถ์ ": | |
| prompt = "์ฐ์ธํ ๊ธฐ๋ถ์ผ๋ก ์์๋ ํ๋ฃจ, " | |
| else: | |
| prompt = "ํ๋ฒํ ํ๋ฃจ๊ฐ ์์๋์๋ค. " | |
| prompt += user_input | |
| input_ids = gpt_tokenizer.encode(prompt, return_tensors="pt") | |
| output = gpt_model.generate(input_ids, max_length=150, do_sample=True, temperature=0.8, top_k=50) | |
| result = gpt_tokenizer.decode(output[0], skip_special_tokens=True) | |
| return f"๐ง ๊ฐ์ ๋ถ์ ๊ฒฐ๊ณผ: {emotion}\n\nโ๏ธ GPT๊ฐ ์ด์ด ์ด ๊ธ:\n{result}" | |
| # Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ | |
| gr.Interface( | |
| fn=emotional_gpt, | |
| inputs=gr.Textbox(lines=3, label="โ๏ธ ๊ฐ์ ์ ๋ด์ ๋ฌธ์ฅ์ ์ ๋ ฅํด์ฃผ์ธ์!", placeholder="์: ์ค๋ ๋๋ฌด ์ธ๋ก์ ์ด"), | |
| outputs="text", | |
| title="๐ญ ๊ฐ์ ํ GPT ํ๊ธ ์๋ฌธ AI", | |
| description="๐ง ๊ฐ์ ์ ๋จผ์ ํ์ ํ๊ณ โจ ๊ทธ ๊ฐ์ ์ ์ด์ธ๋ฆฌ๋ ๋ฌธ์ฅ์ ์ด์ด์ ์์ฑํด์ค๋๋ค!", | |
| theme="soft", | |
| examples=[ | |
| ["๊ธฐ๋ถ์ด ๋๋ฌด ์ข์์ด"], | |
| ["์ง์ง ์ธ๋กญ๊ณ ํ๋ ํ๋ฃจ์์ด"], | |
| ["ํ์๊ฐ ๊ทธ๋ฅ ๊ทธ๋ฌ์ด"] | |
| ] | |
| ).launch() | |