Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
model = BertForQuestionAnswering.from_pretrained("outputs/bert/best_model")
|
| 8 |
|
| 9 |
# Função para realizar predições
|
| 10 |
def perform_prediction(context, question):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
with torch.no_grad():
|
| 16 |
-
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 17 |
-
start_scores, end_scores = outputs.start_logits, outputs.end_logits
|
| 18 |
-
|
| 19 |
-
# Encontrando a posição da resposta
|
| 20 |
-
answer_start = torch.argmax(start_scores)
|
| 21 |
-
answer_end = torch.argmax(end_scores) + 1
|
| 22 |
-
|
| 23 |
-
# Decodificando a resposta
|
| 24 |
-
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[0][answer_start:answer_end]))
|
| 25 |
-
|
| 26 |
-
return answer
|
| 27 |
|
| 28 |
# Criando a interface do Gradio
|
| 29 |
iface = gr.Interface(
|
|
@@ -38,4 +27,4 @@ iface = gr.Interface(
|
|
| 38 |
)
|
| 39 |
|
| 40 |
# Iniciando a interface
|
| 41 |
-
iface.launch()
|
|
|
|
| 1 |
+
# Instalar as dependências manualmente, se necessário
|
| 2 |
+
import os
|
| 3 |
+
os.system("pip install simpletransformers")
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
from simpletransformers.question_answering import QuestionAnsweringModel
|
|
|
|
| 7 |
|
| 8 |
+
# Load model from training checkpoint
|
| 9 |
+
model = QuestionAnsweringModel("bert", "outputs/bert/best_model", use_cuda=False)
|
|
|
|
| 10 |
|
| 11 |
# Função para realizar predições
|
| 12 |
def perform_prediction(context, question):
|
| 13 |
+
to_predict = [{"context": context, "qas": [{"question": question, "id": "0"}]}]
|
| 14 |
+
answers, probabilities = model.predict(to_predict, n_best_size=2)
|
| 15 |
+
return answers[0]["answer"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Criando a interface do Gradio
|
| 18 |
iface = gr.Interface(
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
# Iniciando a interface
|
| 30 |
+
iface.launch()
|