second update app.py
Browse fileschanged variable name "somme" to "similarities" to be more explicit. Better formatted the description. Added more explicative input and audio labels.
app.py
CHANGED
|
@@ -2,27 +2,31 @@ import pandas as pd
|
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
import gradio
|
| 4 |
|
| 5 |
-
PATH_TO_FILE = '
|
| 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(Question):
|
| 12 |
-
|
| 13 |
for jdx, j in enumerate(list(df.Question)):
|
| 14 |
-
|
| 15 |
model.encode(list(df.Question)[jdx], convert_to_tensor=True),
|
| 16 |
model.encode(Question, convert_to_tensor=True))))
|
| 17 |
-
if round(max(
|
| 18 |
return 'I\'m sorry, I\'m not sure I understood your question. Could you try again?'
|
| 19 |
else:
|
| 20 |
-
return df.loc[
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
description='Hi! This is Cabot, the Consular Affairs bot.\nI will help you answer your questions about the Citizen Services that you can request at the Consular Sections within U.S. Embassy Rome.').launch()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
import gradio
|
| 4 |
|
| 5 |
+
PATH_TO_FILE = 'cabot_qa.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(Question):
|
| 12 |
+
similarities = []
|
| 13 |
for jdx, j in enumerate(list(df.Question)):
|
| 14 |
+
similarities.append(float(util.pytorch_cos_sim(
|
| 15 |
model.encode(list(df.Question)[jdx], convert_to_tensor=True),
|
| 16 |
model.encode(Question, convert_to_tensor=True))))
|
| 17 |
+
if round(max(similarities)*100) < 70:
|
| 18 |
return 'I\'m sorry, I\'m not sure I understood your question. Could you try again?'
|
| 19 |
else:
|
| 20 |
+
return df.loc[similarities.index(max(similarities)), 'Answer']
|
| 21 |
|
| 22 |
+
description = """Hi! I'm is Cabot, the Consular Affairs bot.<br>
|
| 23 |
+
I will answer your questions about the Citizen Services that you can request at the Consular Sections within U.S. Embassy Rome."""
|
|
|
|
| 24 |
|
| 25 |
+
gradio.Interface(run_question,
|
| 26 |
+
title='Introducing C.A.BOT',
|
| 27 |
+
description=description,
|
| 28 |
+
inputs=gradio.Textbox(label="Type your question here!"),
|
| 29 |
+
outputs=gradio.Textbox(label="Answer")).launch()
|
| 30 |
|
| 31 |
|
| 32 |
|