Davide Fiocco commited on
Commit
0fdee06
·
1 Parent(s): 35dc28d

Refactor the app and change the context a bit

Browse files
Files changed (4) hide show
  1. app.py +18 -16
  2. context.json +1 -1
  3. test_answers.py +60 -0
  4. utils.py +5 -0
app.py CHANGED
@@ -1,8 +1,12 @@
 
 
1
  import streamlit as st
2
- from transformers import Pipeline, pipeline
3
  import tokenizers
4
  import torch
5
- import json
 
 
 
6
 
7
  @st.cache(
8
  hash_funcs={
@@ -13,18 +17,20 @@ import json
13
  allow_output_mutation=True,
14
  show_spinner=False,
15
  )
16
-
17
  def load_engine() -> Pipeline:
18
 
19
  nlp_qa = pipeline(
20
- 'question-answering',
21
- model='mrm8488/bert-italian-finedtuned-squadv1-it-alfa',
22
- tokenizer='mrm8488/bert-italian-finedtuned-squadv1-it-alfa'
23
  )
24
 
25
  return nlp_qa
26
 
27
- with st.spinner(text="Sto preparando il necessario per rispondere alle tue domande personali..."):
 
 
 
28
 
29
  engine = load_engine()
30
 
@@ -38,15 +44,11 @@ input = st.text_input("Fai una domanda e comparirà la risposta!")
38
  if input:
39
  try:
40
 
41
- answer = engine(
42
- {
43
- 'question': input,
44
- 'context': context["info"]
45
- }
46
- )
47
-
48
- st.subheader(answer["answer"])
49
 
50
  except:
51
 
52
- st.error("Qualcosa é andato storto. Prova di nuovo con un'altra domanda magari!")
 
 
 
1
+ import json
2
+
3
  import streamlit as st
 
4
  import tokenizers
5
  import torch
6
+ from transformers import Pipeline, pipeline
7
+
8
+ from utils import get_answer
9
+
10
 
11
  @st.cache(
12
  hash_funcs={
 
17
  allow_output_mutation=True,
18
  show_spinner=False,
19
  )
 
20
  def load_engine() -> Pipeline:
21
 
22
  nlp_qa = pipeline(
23
+ "question-answering",
24
+ model="mrm8488/bert-italian-finedtuned-squadv1-it-alfa",
25
+ tokenizer="mrm8488/bert-italian-finedtuned-squadv1-it-alfa",
26
  )
27
 
28
  return nlp_qa
29
 
30
+
31
+ with st.spinner(
32
+ text="Sto preparando il necessario per rispondere alle tue domande personali..."
33
+ ):
34
 
35
  engine = load_engine()
36
 
 
44
  if input:
45
  try:
46
 
47
+ answer = get_answer(input, context["info"], engine)
48
+ st.subheader(answer)
 
 
 
 
 
 
49
 
50
  except:
51
 
52
+ st.error(
53
+ "Qualcosa é andato storto. Prova di nuovo con un'altra domanda magari!"
54
+ )
context.json CHANGED
@@ -1,2 +1,2 @@
1
- {"info": "Sei Giuseppe, hai 69 anni e abiti assieme a Davide a Villafranca di Verona in Corso Vittorio Emanuele 238. Ora siamo a novembre dell'anno 2021 e hai avuto un'encefalite virale a gennaio 2021. Davide è tuo figlio e ha 37 anni. A causa dell'encefalite hai perso la memoria. Tua moglie Raffaella vive a Verona. Raffaella sta bene. Con Raffaella hai rapporti cordiali. I tuoi fratelli Alessandro, Giovanni e Grazia stanno bene."
2
  }
 
1
+ {"info": "Sei Giuseppe, hai 69 anni e abiti e vivi a Villafranca di Verona con Davide in Corso Vittorio Emanuele 238. Ora siamo a novembre dell'anno 2021 e hai avuto un'encefalite virale a gennaio 2021. Davide è tuo figlio, ha 37 anni. A causa dell'encefalite hai perso la memoria. Tua moglie Raffaella abita a Verona. Raffaella sta bene. Con Raffaella hai rapporti cordiali. Alessandro, Giovanni e Grazia sono i tuoi fratelli. I tuoi fratelli stanno bene."
2
  }
test_answers.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils import get_answer
2
+ from transformers import pipeline
3
+ import json
4
+
5
+ nlp_qa = pipeline(
6
+ "question-answering",
7
+ model="mrm8488/bert-italian-finedtuned-squadv1-it-alfa",
8
+ tokenizer="mrm8488/bert-italian-finedtuned-squadv1-it-alfa",
9
+ )
10
+
11
+ with open("context.json") as f:
12
+ context = json.load(f)["info"]
13
+
14
+
15
+ def test_name():
16
+ q = "Come mi chiamo?"
17
+ a = get_answer(q, context, nlp_qa)
18
+ assert a == "Giuseppe"
19
+
20
+
21
+ def test_age():
22
+ q = "Quanti anni ho?"
23
+ a = get_answer(q, context, nlp_qa)
24
+ assert a == "69"
25
+
26
+
27
+ def test_home():
28
+ q = "Dove vivo?"
29
+ a = get_answer(q, context, nlp_qa)
30
+ assert a == "Villafranca di Verona"
31
+
32
+
33
+ def test_family():
34
+ q = "Con chi vivo?"
35
+ a = get_answer(q, context, nlp_qa)
36
+ assert a == "Davide"
37
+
38
+
39
+ def test_family_2():
40
+ q = "Come si chiama mio figlio?"
41
+ a = get_answer(q, context, nlp_qa)
42
+ assert a == "Davide"
43
+
44
+
45
+ def test_family_3():
46
+ q = "Quanti anni ha mio figlio?"
47
+ a = get_answer(q, context, nlp_qa)
48
+ assert a == "37"
49
+
50
+
51
+ def test_family_4():
52
+ q = "Come sta Raffaella?"
53
+ a = get_answer(q, context, nlp_qa)
54
+ assert a == "Raffaella sta bene"
55
+
56
+
57
+ def test_family_5():
58
+ q = "In che rapporti sono con Raffaella?"
59
+ a = get_answer(q, context, nlp_qa)
60
+ assert a == "cordiali"
utils.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ def get_answer(input, context, engine):
2
+
3
+ answer = engine({"question": input, "context": context})
4
+
5
+ return answer["answer"]