Spaces:
Runtime error
Runtime error
Isaac Isaías
commited on
Commit
•
c687998
1
Parent(s):
de6ed29
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,75 @@ classificationResult = pipe("El objetivo de esta tesis es elaborar un estudio de
|
|
28 |
|
29 |
|
30 |
def thesis_prediction(input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
pass
|
32 |
|
33 |
|
|
|
28 |
|
29 |
|
30 |
def thesis_prediction(input):
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained('hiiamsid/BETO_es_binary_classification', use_fast=False)
|
32 |
+
X_val_inputs, X_val_masks = preprocessingtext(_text,tokenizer)
|
33 |
+
t0 = time.time()
|
34 |
+
|
35 |
+
# Deserialization of the file
|
36 |
+
#file = open(path + os.path.sep + 'classIndexAssociation.pkl', 'rb')
|
37 |
+
#new_model = pickle.load(file)
|
38 |
+
|
39 |
+
#sizeOfClass = len(new_model)
|
40 |
+
|
41 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
42 |
+
'hackathon-pln-es/unam_tesis_BETO_finnetuning', num_labels=5, output_attentions=False, output_hidden_states=False)
|
43 |
+
#Bibliografy from:
|
44 |
+
#
|
45 |
+
# https://huggingface.co/docs/transformers/main_classes/output
|
46 |
+
#
|
47 |
+
inputs = tokenizer(_text, return_tensors="pt")
|
48 |
+
labels = torch.tensor([1]).unsqueeze(0) # Batch size 1
|
49 |
+
outputs = model(**inputs, labels=labels)
|
50 |
+
|
51 |
+
loss, logits = outputs[:2]
|
52 |
+
|
53 |
+
#Transform in array
|
54 |
+
logits = logits.detach().cpu().numpy()
|
55 |
+
|
56 |
+
#Get max element and position
|
57 |
+
result = logits.argmax()
|
58 |
+
return result
|
59 |
+
|
60 |
+
#Example from
|
61 |
+
#
|
62 |
+
#
|
63 |
+
#
|
64 |
+
# pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
|
65 |
+
# # Put the model in evaluation mode
|
66 |
+
# classificationResult = pipe(_text)
|
67 |
+
# if classificationResult[0] != None and len (classificationResult[0]) > 0:
|
68 |
+
# #Order the result with more close to 1
|
69 |
+
# classificationResult[0].sort(reverse=True, key=lambda x:x['score'])
|
70 |
+
# # Return the text clasification
|
71 |
+
# keyClass = classificationResult[0][0]['label']
|
72 |
+
# keyClass = keyClass.replace("LABEL_","").strip()
|
73 |
+
# if keyClass.isnumeric():
|
74 |
+
# return new_model[ int (keyClass)]
|
75 |
+
# else:
|
76 |
+
# raise Exception("Not exist class info")
|
77 |
+
# model.eval()
|
78 |
+
# outputs = model(X_val_inputs,
|
79 |
+
# token_type_ids=None,
|
80 |
+
# attention_mask=X_val_masks)
|
81 |
+
#
|
82 |
+
# # The "logits" are the output values
|
83 |
+
# # prior to applying an activation function
|
84 |
+
# logits = outputs[0]
|
85 |
+
#
|
86 |
+
# # Move logits and labels to CPU
|
87 |
+
# logits = logits.detach().cpu().numpy()
|
88 |
+
#
|
89 |
+
# sorted_tuples = sorted(logits.items(), key=lambda item: item[1])
|
90 |
+
# #Return the text clasification
|
91 |
+
# keyClass = sorted_tuples.keys()[0]
|
92 |
+
# return new_model[keyClass]
|
93 |
+
|
94 |
+
else:
|
95 |
+
raise Exception("Not exist model info")
|
96 |
+
else:
|
97 |
+
raise Exception("Not exist model info")
|
98 |
+
return "Text"
|
99 |
+
pass
|
100 |
pass
|
101 |
|
102 |
|