Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from sentence_transformers import SentenceTransformer, util
|
| 7 |
+
|
| 8 |
+
def encode_column(model, filename, col_name):
|
| 9 |
+
df = pd.read_csv(filename)
|
| 10 |
+
df["embedding"] = list(model.encode(df[col_name]))
|
| 11 |
+
return df
|
| 12 |
+
|
| 13 |
+
def item_level_ccr(data_encoded_df, questionnaire_encoded_df):
|
| 14 |
+
q_embeddings = questionnaire_encoded_df.embedding
|
| 15 |
+
d_embeddings = data_encoded_df.embedding
|
| 16 |
+
similarities = util.pytorch_cos_sim(d_embeddings, q_embeddings)
|
| 17 |
+
for i in range(1,len(questionnaire_encoded_df)+1):
|
| 18 |
+
data_encoded_df["sim_item_{}".format(i)] = similarities[:, i-1]
|
| 19 |
+
return data_encoded_df
|
| 20 |
+
|
| 21 |
+
# encoding questionnaire
|
| 22 |
+
def ccr_wrapper(data_file, data_col, q_file, q_col, model='all-MiniLM-L6-v2'):
|
| 23 |
+
"""
|
| 24 |
+
Returns a Dataframe that is the content of data_file with one additional column for CCR value per question
|
| 25 |
+
Parameters:
|
| 26 |
+
data_file (str): path to the file containing user text
|
| 27 |
+
data_col (str): column that includes user text
|
| 28 |
+
q_file (str): path to the file containing questionnaires
|
| 29 |
+
q_col (str): column that includes questions
|
| 30 |
+
model (str): name of the SBERT model to use for CCR see https://www.sbert.net/docs/pretrained_models.html for full list
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
model = SentenceTransformer(model)
|
| 34 |
+
except:
|
| 35 |
+
print("model name was not included, using all-MiniLM-L6-v2")
|
| 36 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 37 |
+
|
| 38 |
+
questionnaire_filename = q_file.name
|
| 39 |
+
data_filename = data_file.name
|
| 40 |
+
|
| 41 |
+
q_encoded_df = encode_column(model, questionnaire_filename, q_col)
|
| 42 |
+
data_encoded_df = encode_column(model, data_filename, data_col)
|
| 43 |
+
ccr_df = item_level_ccr(data_encoded_df, q_encoded_df)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
ccr_df.to_csv("ccr_results.csv")
|
| 47 |
+
return "ccr_results.csv"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def read_dataframe(data_file, data_col, q_file, q_col):
|
| 52 |
+
|
| 53 |
+
# df = pd.read_csv(data_file.name)
|
| 54 |
+
return data_file.name
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def single_text_ccr(text, question):
|
| 59 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 60 |
+
text_embedding = model.encode(text)
|
| 61 |
+
question_embedding = model.encode(question)
|
| 62 |
+
return round(util.pytorch_cos_sim(text_embedding, question_embedding).item(),3)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
# gr.Markdown('This is the first page for CCR, info goes here!')
|
| 70 |
+
gr.Markdown("""<h1><center>Contextual Construct Representations</center></h1>
|
| 71 |
+
<h3><center>Ali Omrani and Mohammad Atari</center></h3>""")
|
| 72 |
+
|
| 73 |
+
gr.Markdown("""<br><h4>Play around with your items!</h4>""")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
user_txt = gr.Textbox(label="Input Text", placeholder="Enter your desired text here ...")
|
| 77 |
+
question = gr.Textbox(label="Question", placeholder="Enter the question text here ...")
|
| 78 |
+
|
| 79 |
+
submit2 = gr.Button("Get CCR for this Text!")
|
| 80 |
+
|
| 81 |
+
submit2.click(single_text_ccr, inputs=[user_txt, question], outputs=gr.Textbox(label="CCR Value"))
|
| 82 |
+
|
| 83 |
+
gr.Markdown("""<br><h4>Or process a whole file!</h4>""")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
model_name = gr.Dropdown(label="Choose the Model",
|
| 87 |
+
choices=["all-mpnet-base-v2","multi-qa-mpnet-base-dot-v1", "distiluse-base-multilingual-cased-v2",
|
| 88 |
+
"distiluse-base-multilingual-cased-v1", "paraphrase-MiniLM-L3-v2", "paraphrase-multilingual-MiniLM-L12-v2",
|
| 89 |
+
"paraphrase-albert-small-v2", "paraphrase-multilingual-mpnet-base-v2", "multi-qa-MiniLM-L6-cos-v1",
|
| 90 |
+
"all-MiniLM-L6-v2", "multi-qa-distilbert-cos-v1", "all-MiniLM-L12-v2", "all-distilroberta-v1"])
|
| 91 |
+
with gr.Row():
|
| 92 |
+
with gr.Column():
|
| 93 |
+
user_data = gr.File(label="Participant Data File")
|
| 94 |
+
text_col = gr.Textbox(label="Text Column", placeholder="text column ... ")
|
| 95 |
+
with gr.Column():
|
| 96 |
+
questionnaire_data = gr.File(label="Questionnaire File")
|
| 97 |
+
q_col = gr.Textbox(label="Question Column", placeholder="questionnaire column ... ")
|
| 98 |
+
|
| 99 |
+
submit = gr.Button("Get CCR!")
|
| 100 |
+
|
| 101 |
+
outputs=gr.File()
|
| 102 |
+
submit.click(ccr_wrapper, inputs=[user_data, text_col,questionnaire_data,q_col, model_name], outputs=[outputs])
|
| 103 |
+
demo.launch()
|