Update app.py
Browse files
app.py
CHANGED
|
@@ -59,6 +59,11 @@ bert_tokenizer = AutoTokenizer.from_pretrained("my_finetuned_model")
|
|
| 59 |
bert_model = AutoModelForSequenceClassification.from_pretrained("my_finetuned_model")
|
| 60 |
bert_model.eval()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
label_names = {0: 'pozitivno', 1: 'neutralno', 2: 'negativno'}
|
| 63 |
|
| 64 |
def text_to_indices(text, max_len=100):
|
|
@@ -103,19 +108,28 @@ def predict_bert(text):
|
|
| 103 |
confidence = probs[0][pred].item()
|
| 104 |
return f"{label_names[pred]} (p={confidence:.2f})"
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def predict_all(text):
|
| 107 |
return (
|
| 108 |
predict_svm(text),
|
| 109 |
predict_cnn(text),
|
| 110 |
predict_gru(text),
|
| 111 |
predict_bert(text),
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
def clear_all():
|
| 115 |
return "", "", "", "", ""
|
| 116 |
|
| 117 |
with gr.Blocks() as demo:
|
| 118 |
-
# Naslov veći, centriran
|
| 119 |
gr.Markdown(
|
| 120 |
"""
|
| 121 |
<h1 style="text-align: center; font-size: 48px; margin-bottom: 5px;">Analiza sentimenta</h1>
|
|
@@ -141,9 +155,10 @@ with gr.Blocks() as demo:
|
|
| 141 |
with gr.Column():
|
| 142 |
gr.Markdown("### Transformers")
|
| 143 |
bert_output = gr.Textbox(label="BERTić")
|
|
|
|
| 144 |
|
| 145 |
-
submit_btn.click(fn=predict_all, inputs=input_text, outputs=[svm_output, cnn_output, gru_output, bert_output])
|
| 146 |
-
clear_btn.click(fn=clear_all, inputs=None, outputs=[input_text, svm_output, cnn_output, gru_output, bert_output])
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
demo.launch(share=True)
|
|
|
|
| 59 |
bert_model = AutoModelForSequenceClassification.from_pretrained("my_finetuned_model")
|
| 60 |
bert_model.eval()
|
| 61 |
|
| 62 |
+
# CroSlo model/tokenizer
|
| 63 |
+
croslo_tokenizer = AutoTokenizer.from_pretrained("CroSlo")
|
| 64 |
+
croslo_model = AutoModelForSequenceClassification.from_pretrained("CroSlo")
|
| 65 |
+
croslo_model.eval()
|
| 66 |
+
|
| 67 |
label_names = {0: 'pozitivno', 1: 'neutralno', 2: 'negativno'}
|
| 68 |
|
| 69 |
def text_to_indices(text, max_len=100):
|
|
|
|
| 108 |
confidence = probs[0][pred].item()
|
| 109 |
return f"{label_names[pred]} (p={confidence:.2f})"
|
| 110 |
|
| 111 |
+
def predict_croslo(text):
|
| 112 |
+
inputs = croslo_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 113 |
+
with torch.no_grad():
|
| 114 |
+
outputs = croslo_model(**inputs)
|
| 115 |
+
probs = F.softmax(outputs.logits, dim=1)
|
| 116 |
+
pred = torch.argmax(probs, dim=1).item()
|
| 117 |
+
confidence = probs[0][pred].item()
|
| 118 |
+
return f"{label_names[pred]} (p={confidence:.2f})"
|
| 119 |
+
|
| 120 |
def predict_all(text):
|
| 121 |
return (
|
| 122 |
predict_svm(text),
|
| 123 |
predict_cnn(text),
|
| 124 |
predict_gru(text),
|
| 125 |
predict_bert(text),
|
| 126 |
+
predict_croslo(text),
|
| 127 |
)
|
| 128 |
|
| 129 |
def clear_all():
|
| 130 |
return "", "", "", "", ""
|
| 131 |
|
| 132 |
with gr.Blocks() as demo:
|
|
|
|
| 133 |
gr.Markdown(
|
| 134 |
"""
|
| 135 |
<h1 style="text-align: center; font-size: 48px; margin-bottom: 5px;">Analiza sentimenta</h1>
|
|
|
|
| 155 |
with gr.Column():
|
| 156 |
gr.Markdown("### Transformers")
|
| 157 |
bert_output = gr.Textbox(label="BERTić")
|
| 158 |
+
croslo_output = gr.Textbox(label="CroSlo BERT")
|
| 159 |
|
| 160 |
+
submit_btn.click(fn=predict_all, inputs=input_text, outputs=[svm_output, cnn_output, gru_output, bert_output, croslo_output])
|
| 161 |
+
clear_btn.click(fn=clear_all, inputs=None, outputs=[input_text, svm_output, cnn_output, gru_output, bert_output, croslo_output])
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
demo.launch(share=True)
|