File size: 2,101 Bytes
269b71f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """CPU inference example for davolu/emotion-s1. Inference only; no training.
Usage:
python example.py --model-dir /path/to/emotion-s1
python example.py # downloads davolu/emotion-s1 from the Hub
"""
import argparse
import sys
import time
def main():
parser = argparse.ArgumentParser(description="Run Emotion-S1 on one example turn (CPU).")
parser.add_argument("--model-dir", default=None, help="Local model directory. If omitted, snapshot_download('davolu/emotion-s1') is used.")
parser.add_argument("--device", default="cpu")
args = parser.parse_args()
if args.model_dir:
model_dir = args.model_dir
else:
from huggingface_hub import snapshot_download
model_dir = snapshot_download("davolu/emotion-s1")
sys.path.insert(0, model_dir)
from emotion_s1_core import load_emotion_s1, ask
model = load_emotion_s1(model_dir, device=args.device)
questions = {
"emotion": {
"type": "choice",
"question": "What is the user's emotion in this turn?",
"options": ["anger", "disgust", "fear", "joy", "sadness", "surprise", "neutral"],
},
"intensity": {
"type": "score",
"question": "How intense is the user's emotion on a 1 to 5 scale?",
"scale": [1, 5],
},
"frustrated": {
"type": "noul",
"question": "Is the user frustrated?",
},
"needs_support": {
"type": "noul",
"question": "Does the user need emotional support?",
},
}
turn = "I've asked three times and it's STILL broken."
context = [("Assistant", "Did that fix it?")]
# Warm-up (first forward pass includes model load overhead already paid above)
ask(model, turn, questions, context=context)
t0 = time.perf_counter()
out = ask(model, turn, questions, context=context)
ms = (time.perf_counter() - t0) * 1000
for name, result in out.items():
print(f"{name}: {result}")
print(f"latency_ms: {ms:.1f}")
if __name__ == "__main__":
main()
|