Add "interactive.py"
Browse files- Interactive.py +79 -0
Interactive.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
|
| 5 |
+
class MetaphorScorer:
|
| 6 |
+
def __init__(self, model_path='.'):
|
| 7 |
+
"""
|
| 8 |
+
Initialize the metaphor scorer.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
model_path: Path or Hugging Face repo ID.
|
| 12 |
+
Default '.' uses current directory (where model files are)
|
| 13 |
+
Or use 'your-username/Metaphor_Scoring_Model' to load from Hub
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 17 |
+
|
| 18 |
+
print(f"Loading model from: {model_path}")
|
| 19 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 20 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 21 |
+
self.model.to(self.device)
|
| 22 |
+
self.model.eval()
|
| 23 |
+
print(f"Model loaded on {self.device}")
|
| 24 |
+
|
| 25 |
+
def score_sentence(self, sentence):
|
| 26 |
+
"""
|
| 27 |
+
Score a sentence for metaphorical novelty.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
sentence: Input sentence to score
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
score: Novelty score (1-4)
|
| 34 |
+
confidence: Model confidence (0-1)
|
| 35 |
+
"""
|
| 36 |
+
inputs = self.tokenizer(
|
| 37 |
+
sentence,
|
| 38 |
+
return_tensors='pt',
|
| 39 |
+
max_length=256,
|
| 40 |
+
truncation=True,
|
| 41 |
+
padding='max_length'
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 45 |
+
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
outputs = self.model(**inputs)
|
| 48 |
+
logits = outputs.logits
|
| 49 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
| 50 |
+
score = predicted_class + 1
|
| 51 |
+
probabilities = torch.softmax(logits, dim=-1)
|
| 52 |
+
confidence = probabilities[0][predicted_class].item()
|
| 53 |
+
|
| 54 |
+
return score, confidence
|
| 55 |
+
|
| 56 |
+
def main():
|
| 57 |
+
# Load model (will use current directory by default)
|
| 58 |
+
scorer = MetaphorScorer()
|
| 59 |
+
|
| 60 |
+
print("\n=== Metaphorical Sentence Scorer ===")
|
| 61 |
+
print("Enter metaphorical sentences to get novelty scores (1-4)")
|
| 62 |
+
print("Higher scores = Higher metaphorical novelty")
|
| 63 |
+
print("Type 'quit' to exit\n")
|
| 64 |
+
|
| 65 |
+
while True:
|
| 66 |
+
sentence = input("Enter sentence: ").strip()
|
| 67 |
+
|
| 68 |
+
if sentence.lower() in ['quit', 'exit', 'q']:
|
| 69 |
+
print("Goodbye!")
|
| 70 |
+
break
|
| 71 |
+
|
| 72 |
+
if not sentence:
|
| 73 |
+
continue
|
| 74 |
+
|
| 75 |
+
score, confidence = scorer.score_sentence(sentence)
|
| 76 |
+
print(f"Score: {score}/4 (confidence: {confidence:.3f})\n")
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
main()
|