Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Gradio interface function
|
45 |
+
def chatbot(question):
|
46 |
+
to_predict = [
|
47 |
+
{
|
48 |
+
"context": "The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse (\"Norman\" comes from \"Norseman\") raiders and pirates from Denmark, Iceland and Norway who, under their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct cultural and ethnic identity of the Normans emerged initially in the first half of the 10th century, and it continued to evolve over the succeeding centuries.",
|
49 |
+
"qas": [
|
50 |
+
{
|
51 |
+
"question": question,
|
52 |
+
"id": "user_question",
|
53 |
+
}
|
54 |
+
],
|
55 |
+
}
|
56 |
+
]
|
57 |
+
|
58 |
+
answers, probabilities = model.predict(to_predict)
|
59 |
+
top_answer = answers[0]['answer'][0]
|
60 |
+
return top_answer
|
61 |
+
|
62 |
+
# Gradio interface setup
|
63 |
+
iface = gr.Interface(
|
64 |
+
fn=chatbot,
|
65 |
+
inputs="text",
|
66 |
+
outputs="text",
|
67 |
+
live=True,
|
68 |
+
title="Chatbot Interface",
|
69 |
+
description="Ask a question about the Normans",
|
70 |
+
)
|
71 |
+
|
72 |
+
# Launch Gradio interface
|
73 |
+
iface.launch()
|