Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -38,32 +38,37 @@ book1_raw_1 = re.sub(r'Mrs. ', 'Mrs ', book1_raw_1)
|
|
38 |
# Group into 3 sentences-long parts
|
39 |
paragraphs = re.findall("[^.?!]+[.?!][^.?!]+[.?!][^.?!]+[.?!]", book1_raw_1)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
41 |
# Type in HP-related query here
|
42 |
query = st.text_area("Hello my dears! What is your question? Be patient please, I am not a Ravenclaw!")
|
43 |
|
44 |
-
|
45 |
-
|
|
|
46 |
|
47 |
-
model_embed = SentenceTransformer(model_embed_name)
|
48 |
-
doc_emb = model_embed.encode(paragraphs)
|
49 |
-
query_emb = model_embed.encode(query)
|
50 |
|
51 |
-
#Compute dot score between query and all document embeddings
|
52 |
-
scores = util.cos_sim(query_emb, doc_emb)[0].cpu().tolist()
|
53 |
|
54 |
-
#Combine docs & scores
|
55 |
-
doc_score_pairs = list(zip(paragraphs, scores))
|
56 |
|
57 |
-
#Sort by decreasing score and get only 3 most similar groups
|
58 |
-
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1],
|
59 |
reverse=True)[:3]
|
60 |
|
61 |
-
# Join these similar groups to form the context
|
62 |
-
context = "".join(x[0] for x in doc_score_pairs)
|
63 |
-
|
64 |
|
65 |
-
# Perform the querying
|
66 |
-
QA_input = {'question': query, 'context': context}
|
67 |
-
res = pipe(QA_input)
|
68 |
-
out = res.get('answer')
|
69 |
-
st.write(out)
|
|
|
38 |
# Group into 3 sentences-long parts
|
39 |
paragraphs = re.findall("[^.?!]+[.?!][^.?!]+[.?!][^.?!]+[.?!]", book1_raw_1)
|
40 |
|
41 |
+
# desc = "Uses LSTM neural network trained on *The Lord of the Rings*. Check out the code [here](https://github.com/christian-doucette/tolkein_text)!"
|
42 |
+
|
43 |
+
st.title('Harry Potter and the Extractive Question Answering Model')
|
44 |
+
# st.write(desc)
|
45 |
+
|
46 |
# Type in HP-related query here
|
47 |
query = st.text_area("Hello my dears! What is your question? Be patient please, I am not a Ravenclaw!")
|
48 |
|
49 |
+
if st.button('Ask'):
|
50 |
+
# Perform sentence embedding on query and sentence groups
|
51 |
+
model_embed_name = 'sentence-transformers/multi-qa-MiniLM-L6-cos-v1'
|
52 |
|
53 |
+
model_embed = SentenceTransformer(model_embed_name)
|
54 |
+
doc_emb = model_embed.encode(paragraphs)
|
55 |
+
query_emb = model_embed.encode(query)
|
56 |
|
57 |
+
#Compute dot score between query and all document embeddings
|
58 |
+
scores = util.cos_sim(query_emb, doc_emb)[0].cpu().tolist()
|
59 |
|
60 |
+
#Combine docs & scores
|
61 |
+
doc_score_pairs = list(zip(paragraphs, scores))
|
62 |
|
63 |
+
#Sort by decreasing score and get only 3 most similar groups
|
64 |
+
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1],
|
65 |
reverse=True)[:3]
|
66 |
|
67 |
+
# Join these similar groups to form the context
|
68 |
+
context = "".join(x[0] for x in doc_score_pairs)
|
|
|
69 |
|
70 |
+
# Perform the querying
|
71 |
+
QA_input = {'question': query, 'context': context}
|
72 |
+
res = pipe(QA_input)
|
73 |
+
out = res.get('answer')
|
74 |
+
st.write(out)
|