Spaces:
Runtime error
Runtime error
| # emotion_engine.py (μμ ν μ΅μ’ λ²μ ) | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline | |
| import os | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline | |
| import os | |
| def load_emotion_classifier(): | |
| # --- μ΄ λΆλΆμ μμ ν©λλ€ --- | |
| # λ‘컬 κ²½λ‘ λμ , Hugging Face Hubμ λͺ¨λΈ IDλ₯Ό μ¬μ©ν©λλ€. | |
| MODEL_PATH = "koons/korean-emotion-classifier-final" # "μ¬μ©μμ΄λ¦/λͺ¨λΈμ΄λ¦" νμ | |
| print(f"Hugging Face Hub λͺ¨λΈ '{MODEL_PATH}'μμ λͺ¨λΈμ λΆλ¬μ΅λλ€...") | |
| try: | |
| # local_files_only μ΅μ μ μ κ±°νμ¬ μ¨λΌμΈμμ λ€μ΄λ‘λνλλ‘ ν©λλ€. | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH) | |
| print("β Hugging Face Hub λͺ¨λΈ λ‘λ© μ±κ³΅!") | |
| except Exception as e: | |
| print(f"β λͺ¨λΈ λ‘λ© μ€ μ€λ₯: {e}") | |
| return None | |
| device = 0 if torch.cuda.is_available() else -1 | |
| emotion_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=device) | |
| return emotion_classifier | |
| # predict_emotion ν¨μλ κ·Έλλ‘ λ‘λλ€. | |
| def predict_emotion(classifier, text): | |
| if not text or not text.strip(): return "λ΄μ© μμ" | |
| if classifier is None: return "μ€λ₯: κ°μ λΆμ μμ§ μ€λΉ μλ¨." | |
| result = classifier(text) | |
| return result[0]['label'] |