Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
def chatbot(question):
|
| 4 |
to_predict = [
|
| 5 |
{
|
|
@@ -17,6 +66,7 @@ def chatbot(question):
|
|
| 17 |
top_answer = answers[0]['answer'][0]
|
| 18 |
return top_answer
|
| 19 |
|
|
|
|
| 20 |
iface = gr.Interface(
|
| 21 |
fn=chatbot,
|
| 22 |
inputs="text",
|
|
@@ -26,4 +76,5 @@ iface = gr.Interface(
|
|
| 26 |
description="Ask a question about the Normans",
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
iface.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from typing import List
|
| 5 |
import gradio as gr
|
| 6 |
+
from simpletransformers.question_answering import QuestionAnsweringModel, QuestionAnsweringArgs
|
| 7 |
+
|
| 8 |
+
# Load test data
|
| 9 |
+
with open("test.json", "r") as read_file:
|
| 10 |
+
test = json.load(read_file)
|
| 11 |
+
|
| 12 |
+
# Load train data
|
| 13 |
+
with open("konbert-export-a07a2fb8c3174.json", "r") as json_file:
|
| 14 |
+
train_data = json.load(json_file)
|
| 15 |
+
|
| 16 |
+
# Adapt the training data
|
| 17 |
+
adapted_data = []
|
| 18 |
+
for paragraph in train_data:
|
| 19 |
+
qas_list = []
|
| 20 |
+
if "answers" in paragraph and "text" in paragraph["answers"] and "answer_start" in paragraph["answers"]:
|
| 21 |
+
for i in range(len(paragraph["answers"]["text"])):
|
| 22 |
+
answer_text = paragraph["answers"]["text"][i].strip()
|
| 23 |
+
if answer_text:
|
| 24 |
+
qa_dict = {
|
| 25 |
+
"id": f"{paragraph['id']}_{i}",
|
| 26 |
+
"question": paragraph.get("question", ""),
|
| 27 |
+
"answers": [{"text": answer_text, "answer_start": paragraph["answers"]["answer_start"][i]}]
|
| 28 |
+
}
|
| 29 |
+
qas_list.append(qa_dict)
|
| 30 |
+
|
| 31 |
+
if qas_list:
|
| 32 |
+
adapted_data.append({
|
| 33 |
+
"context": paragraph.get("context", ""),
|
| 34 |
+
"qas": qas_list
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
# Model training arguments
|
| 38 |
+
model_args = QuestionAnsweringArgs()
|
| 39 |
+
model_args.train_batch_size = 16
|
| 40 |
+
model_args.evaluate_during_training = True
|
| 41 |
+
model_args.n_best_size = 3
|
| 42 |
+
model_args.num_train_epochs = 5
|
| 43 |
+
|
| 44 |
+
# Model definition
|
| 45 |
+
model = QuestionAnsweringModel('bert', 'bert-base-uncased', use_cuda=False, args={'overwrite_output_dir': True, 'num_train_epochs': 6})
|
| 46 |
+
model.train_model(adapted_data, num_train_epochs=6)
|
| 47 |
+
model.save_model(f"outputs/bert/final_model")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
|
| 51 |
+
# Gradio interface function
|
| 52 |
def chatbot(question):
|
| 53 |
to_predict = [
|
| 54 |
{
|
|
|
|
| 66 |
top_answer = answers[0]['answer'][0]
|
| 67 |
return top_answer
|
| 68 |
|
| 69 |
+
# Gradio interface setup
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=chatbot,
|
| 72 |
inputs="text",
|
|
|
|
| 76 |
description="Ask a question about the Normans",
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# Launch Gradio interface
|
| 80 |
iface.launch()
|