Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -194,49 +194,43 @@ label_mapping = {
|
|
| 194 |
|
| 195 |
#Function to classify user input
|
| 196 |
def classifyTimeFrame(user_input):
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
# Tokenize and predict
|
| 204 |
-
input_encoding = tokenizer(user_input, padding=True, truncation=True, return_tensors="pt").to('cpu')
|
| 205 |
-
|
| 206 |
-
with torch.no_grad():
|
| 207 |
-
attention_mask = input_encoding['attention_mask'].clone()
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
|
| 224 |
-
|
| 225 |
|
| 226 |
-
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
|
| 241 |
iface = gr.Interface(fn=classifyTimeFrame, inputs="text", outputs="text")
|
| 242 |
iface.launch(share=True)
|
|
|
|
| 194 |
|
| 195 |
#Function to classify user input
|
| 196 |
def classifyTimeFrame(user_input):
|
| 197 |
+
# Tokenize and predict
|
| 198 |
+
input_encoding = tokenizer(user_input, padding=True, truncation=True, return_tensors="pt").to('cpu')
|
| 199 |
+
|
| 200 |
+
with torch.no_grad():
|
| 201 |
+
attention_mask = input_encoding['attention_mask'].clone()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
+
# Modify the attention mask to emphasize certain key tokens
|
| 206 |
+
# for idx, token_id in enumerate(input_encoding['input_ids'][0]):
|
| 207 |
+
# word = tokenizer.decode([token_id])
|
| 208 |
+
# print(word)
|
| 209 |
+
# if word.strip() in ["now", "same", "continue", "again", "also"]: # Target key tokens
|
| 210 |
+
# attention_mask[0, idx] = 3 # Increase attention weight for these words
|
| 211 |
+
# else:
|
| 212 |
+
# attention_mask[0, idx] = 0
|
| 213 |
+
# print (attention_mask)
|
| 214 |
+
# input_encoding['attention_mask'] = attention_mask
|
| 215 |
+
# print (input_encoding)
|
| 216 |
+
output = model(**input_encoding, output_hidden_states=True)
|
| 217 |
|
| 218 |
+
probabilities = F.softmax(output.logits, dim=-1)
|
| 219 |
|
| 220 |
+
prediction = torch.argmax(output.logits, dim=1).cpu().numpy()
|
| 221 |
|
| 222 |
+
# Map prediction back to label
|
| 223 |
+
print(prediction)
|
| 224 |
+
predicted_label = label_mapping[prediction[0]]
|
| 225 |
|
| 226 |
|
| 227 |
+
print(f"Predicted intent: {predicted_label}\n")
|
| 228 |
+
# Print the confidence for each label
|
| 229 |
+
print("\nLabel Confidence Scores:")
|
| 230 |
+
for i, label in label_mapping.items():
|
| 231 |
+
confidence = probabilities[0][i].item() # Get confidence score for each label
|
| 232 |
+
print(f"{label}: {confidence:.4f}")
|
| 233 |
+
print("\n")
|
| 234 |
|
| 235 |
iface = gr.Interface(fn=classifyTimeFrame, inputs="text", outputs="text")
|
| 236 |
iface.launch(share=True)
|