question answering model
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
import gradio
|
| 4 |
+
|
| 5 |
+
PATH_TO_FILE = 'cabot_dataset_def_csv.csv'
|
| 6 |
+
df = pd.read_csv(PATH_TO_FILE, sep=';')
|
| 7 |
+
df.drop_duplicates(inplace=True, ignore_index=True)
|
| 8 |
+
|
| 9 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 10 |
+
|
| 11 |
+
def run_question(domanda):
|
| 12 |
+
somme = []
|
| 13 |
+
for jdx, j in enumerate(list(df.Question)):
|
| 14 |
+
somme.append(float(util.pytorch_cos_sim(
|
| 15 |
+
model.encode(list(df.Question)[jdx], convert_to_tensor=True),
|
| 16 |
+
model.encode(domanda, convert_to_tensor=True))))
|
| 17 |
+
if round(max(somme)*100) < 70:
|
| 18 |
+
return 'Sorry, could you paraphrase your question? I quide didn\'t catch that...'
|
| 19 |
+
else:
|
| 20 |
+
return df.loc[somme.index(max(somme)), 'Answer']
|
| 21 |
+
|
| 22 |
+
gradio.Interface(run_question, "text", "text",
|
| 23 |
+
title='Introducing C.A.BOT',
|
| 24 |
+
description='This is Cabot, the Consular Affairs bot. Give it a try asking').launch()
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|