Spaces:
Sleeping
Sleeping
Francesco-A
commited on
Commit
·
de2bc93
1
Parent(s):
1811d79
model change
Browse files
app.py
CHANGED
@@ -1,25 +1,23 @@
|
|
1 |
# AUTOGENERATED! DO NOT EDIT! File to edit: ../drive/MyDrive/Codici/Python/Apps/Gradio_App/SemanticSearch_QA-v2.1.ipynb.
|
2 |
|
3 |
-
|
4 |
__all__ = ['model_name', 'qa_model', 'contexts', 'question', 'df_results', 'question_1', 'question_2', 'question_3', 'question_4',
|
5 |
'question_5', 'question_6', 'question_7', 'question_8', 'question_9', 'question_10', 'title', 'description',
|
6 |
'data', 'context_df', 'question_input', 'contexts_input', 'n_answers_input', 'full_context_input',
|
7 |
'confidence_threshold_input', 'intf', 'get_answers']
|
8 |
|
9 |
-
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/SemanticSearch_QA-v2.1.ipynb 2
|
10 |
import pandas as pd
|
11 |
import gradio as gr
|
12 |
import transformers
|
13 |
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
14 |
|
15 |
-
model_name = 'Francesco-A/bert-finetuned-squad-v1'
|
16 |
-
|
17 |
|
18 |
qa_model = pipeline(task = 'question-answering',
|
19 |
model = model_name,
|
20 |
tokenizer = model_name)
|
21 |
|
22 |
-
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/SemanticSearch_QA-v2.1.ipynb 5
|
23 |
def get_answers(question, contexts, n_answers=1, full_context=True, confidence_threshold = 0.5):
|
24 |
results = []
|
25 |
|
@@ -30,7 +28,7 @@ def get_answers(question, contexts, n_answers=1, full_context=True, confidence_t
|
|
30 |
|
31 |
for i, context in enumerate(contexts):
|
32 |
QA_input = {'question': question, 'context': context}
|
33 |
-
res = qa_model(QA_input)
|
34 |
|
35 |
results_dict = {
|
36 |
'context_idx': i,
|
@@ -43,7 +41,7 @@ def get_answers(question, contexts, n_answers=1, full_context=True, confidence_t
|
|
43 |
results_dict['Full Context'] = context
|
44 |
|
45 |
results.append(results_dict)
|
46 |
-
|
47 |
df = pd.DataFrame(results)
|
48 |
df = df[df['Score'] >= confidence_threshold]
|
49 |
df = df.sort_values(by='Score', ascending=False).head(n_answers)
|
@@ -63,7 +61,6 @@ question = "Why is model conversion important?"
|
|
63 |
df_results = get_answers(question,contexts,n_answers=2,full_context=False, confidence_threshold = 0.25)
|
64 |
df_results
|
65 |
|
66 |
-
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/SemanticSearch_QA-v2.1.ipynb 6
|
67 |
# Define example question(s)
|
68 |
question_1 = "What are the main features of the new XPhone 20?"
|
69 |
question_2 = "What are some benefits of regular exercise?"
|
@@ -120,10 +117,9 @@ contexts = [
|
|
120 |
"The Declaration of Independence was adopted by the Continental Congress on July 4, 1776.",
|
121 |
]
|
122 |
|
123 |
-
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/SemanticSearch_QA-v2.1.ipynb 9
|
124 |
title = 'SemanticSearch_QA-v2'
|
125 |
description = """
|
126 |
-
QA model: [
|
127 |
"""
|
128 |
|
129 |
data = {
|
|
|
1 |
# AUTOGENERATED! DO NOT EDIT! File to edit: ../drive/MyDrive/Codici/Python/Apps/Gradio_App/SemanticSearch_QA-v2.1.ipynb.
|
2 |
|
3 |
+
|
4 |
__all__ = ['model_name', 'qa_model', 'contexts', 'question', 'df_results', 'question_1', 'question_2', 'question_3', 'question_4',
|
5 |
'question_5', 'question_6', 'question_7', 'question_8', 'question_9', 'question_10', 'title', 'description',
|
6 |
'data', 'context_df', 'question_input', 'contexts_input', 'n_answers_input', 'full_context_input',
|
7 |
'confidence_threshold_input', 'intf', 'get_answers']
|
8 |
|
|
|
9 |
import pandas as pd
|
10 |
import gradio as gr
|
11 |
import transformers
|
12 |
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
13 |
|
14 |
+
# model_name = 'Francesco-A/bert-finetuned-squad-v1'
|
15 |
+
model_name = "deepset/roberta-base-squad2"
|
16 |
|
17 |
qa_model = pipeline(task = 'question-answering',
|
18 |
model = model_name,
|
19 |
tokenizer = model_name)
|
20 |
|
|
|
21 |
def get_answers(question, contexts, n_answers=1, full_context=True, confidence_threshold = 0.5):
|
22 |
results = []
|
23 |
|
|
|
28 |
|
29 |
for i, context in enumerate(contexts):
|
30 |
QA_input = {'question': question, 'context': context}
|
31 |
+
res = qa_model(question = QA_input['question'], context = QA_input['context'])
|
32 |
|
33 |
results_dict = {
|
34 |
'context_idx': i,
|
|
|
41 |
results_dict['Full Context'] = context
|
42 |
|
43 |
results.append(results_dict)
|
44 |
+
|
45 |
df = pd.DataFrame(results)
|
46 |
df = df[df['Score'] >= confidence_threshold]
|
47 |
df = df.sort_values(by='Score', ascending=False).head(n_answers)
|
|
|
61 |
df_results = get_answers(question,contexts,n_answers=2,full_context=False, confidence_threshold = 0.25)
|
62 |
df_results
|
63 |
|
|
|
64 |
# Define example question(s)
|
65 |
question_1 = "What are the main features of the new XPhone 20?"
|
66 |
question_2 = "What are some benefits of regular exercise?"
|
|
|
117 |
"The Declaration of Independence was adopted by the Continental Congress on July 4, 1776.",
|
118 |
]
|
119 |
|
|
|
120 |
title = 'SemanticSearch_QA-v2'
|
121 |
description = """
|
122 |
+
QA model: [deepset/roberta-base-squad2](https://huggingface.co/deepset/roberta-base-squad2)
|
123 |
"""
|
124 |
|
125 |
data = {
|