Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -330,69 +330,4 @@ else:
|
|
330 |
model = AutoModelForSequenceClassification.from_pretrained(model_save_path).to('cpu')
|
331 |
tokenizer = AutoTokenizer.from_pretrained(tokenizer_save_path)
|
332 |
|
333 |
-
#Define the label mappings (this must match the mapping used during training)
|
334 |
-
label_mapping = model.config.label_mapping
|
335 |
-
label_mapping_reverse = {value: key for key, value in label_mapping.items()}
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
#Function to classify user input
|
342 |
-
def classify_user_input():
|
343 |
-
while True:
|
344 |
-
user_input = input("Enter a command (or type 'q' to quit): ")
|
345 |
-
if user_input.lower() == 'q':
|
346 |
-
print("Exiting...")
|
347 |
-
break
|
348 |
-
|
349 |
-
# Tokenize and predict
|
350 |
-
input_encoding = tokenizer(user_input, padding=True, truncation=True, return_tensors="pt").to('cpu')
|
351 |
-
|
352 |
-
with torch.no_grad():
|
353 |
-
#attention_mask = input_encoding['attention_mask'].clone()
|
354 |
-
|
355 |
-
# Modify the attention mask to emphasize certain key tokens
|
356 |
-
for idx, token_id in enumerate(input_encoding['input_ids'][0]):
|
357 |
-
word = tokenizer.decode([token_id])
|
358 |
-
print(word)
|
359 |
-
#if word.strip() in ["point", "summarize", "oil", "maintenance"]: # Target key tokens
|
360 |
-
#attention_mask[0, idx] = 2 # Increase attention weight for these words
|
361 |
-
# else:
|
362 |
-
# attention_mask[0, idx] = 0
|
363 |
-
#print (attention_mask)
|
364 |
-
#input_encoding['attention_mask'] = attention_mask
|
365 |
-
output = model(**input_encoding, output_hidden_states=True)
|
366 |
-
# print('start-logits')
|
367 |
-
# print(output.logits)
|
368 |
-
# print('end-logits')
|
369 |
-
#print(output)
|
370 |
-
attention = output.attentions # Get attention scores
|
371 |
-
#print('atten')
|
372 |
-
#print(attention)
|
373 |
-
# Apply softmax to get the probabilities (confidence scores)
|
374 |
-
probabilities = F.softmax(output.logits, dim=-1)
|
375 |
-
|
376 |
-
# tokens = tokenizer.convert_ids_to_tokens(input_encoding['input_ids'][0].cpu().numpy())
|
377 |
-
# # Display the attention visualization
|
378 |
-
# input_text = tokenizer.convert_ids_to_tokens(input_encoding['input_ids'][0])
|
379 |
-
|
380 |
-
prediction = torch.argmax(output.logits, dim=1).cpu().numpy()
|
381 |
-
|
382 |
-
# Map prediction back to label
|
383 |
-
print(prediction)
|
384 |
-
predicted_label = label_mapping_reverse[prediction[0]]
|
385 |
-
|
386 |
-
|
387 |
-
print(f"Predicted intent: {predicted_label}\n")
|
388 |
-
# Print the confidence for each label
|
389 |
-
print("\nLabel Confidence Scores:")
|
390 |
-
for i, label in label_mapping_reverse.items():
|
391 |
-
confidence = probabilities[0][i].item() # Get confidence score for each label
|
392 |
-
print(f"{label}: {confidence:.4f}")
|
393 |
-
print("\n")
|
394 |
-
|
395 |
-
#Run the function
|
396 |
-
classify_user_input()
|
397 |
-
|
398 |
|
|
|
330 |
model = AutoModelForSequenceClassification.from_pretrained(model_save_path).to('cpu')
|
331 |
tokenizer = AutoTokenizer.from_pretrained(tokenizer_save_path)
|
332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|