Add python file and requirements
Browse files- app.py +65 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
HEXACO = [
|
| 6 |
+
"honesty-humility",
|
| 7 |
+
"emotionality",
|
| 8 |
+
"extraversion",
|
| 9 |
+
"agreeableness",
|
| 10 |
+
"conscientiousness",
|
| 11 |
+
"openness to experience"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def netScores(tagList: list, sequence_to_classify: str, modelName: str) -> dict:
|
| 15 |
+
classifier = pipeline("zero-shot-classification", model=modelName)
|
| 16 |
+
hypothesis_template_pos = "This example is {}"
|
| 17 |
+
hypothesis_template_neg = "This example is not {}"
|
| 18 |
+
output_pos = classifier(sequence_to_classify, tagList, hypothesis_template=hypothesis_template_pos, multi_label=True)
|
| 19 |
+
output_neg = classifier(sequence_to_classify, tagList, hypothesis_template=hypothesis_template_neg, multi_label=True)
|
| 20 |
+
|
| 21 |
+
positive_scores = {}
|
| 22 |
+
for x in range(len(tagList)):
|
| 23 |
+
positive_scores[output_pos["labels"][x]] = output_pos["scores"][x]
|
| 24 |
+
|
| 25 |
+
negative_scores = {}
|
| 26 |
+
for x in range(len(tagList)):
|
| 27 |
+
negative_scores[output_neg["labels"][x]] = output_neg["scores"][x]
|
| 28 |
+
|
| 29 |
+
pos_neg_scores = {}
|
| 30 |
+
for tag in tagList:
|
| 31 |
+
pos_neg_scores[tag] = [positive_scores[tag],negative_scores[tag]]
|
| 32 |
+
|
| 33 |
+
net_scores = {}
|
| 34 |
+
for tag in tagList:
|
| 35 |
+
net_scores[tag] = positive_scores[tag]-negative_scores[tag]
|
| 36 |
+
|
| 37 |
+
net_scores = dict(sorted(net_scores.items(), key=lambda x:x[1], reverse=True))
|
| 38 |
+
|
| 39 |
+
return net_scores
|
| 40 |
+
|
| 41 |
+
def scoresMatch(tagList: list, scoresA: dict, scoresB: dict):
|
| 42 |
+
maxDistance = 2*np.sqrt(len(tagList))
|
| 43 |
+
differenceSquares = []
|
| 44 |
+
for tag in tagList:
|
| 45 |
+
difference = (scoresA[tag] - scoresB[tag])
|
| 46 |
+
differenceSquare = difference*difference
|
| 47 |
+
differenceSquares.append(differenceSquare)
|
| 48 |
+
distance = np.sqrt(np.sum(differenceSquares))
|
| 49 |
+
percentDifference = distance/maxDistance
|
| 50 |
+
|
| 51 |
+
return 1-percentDifference
|
| 52 |
+
|
| 53 |
+
def compareDocuments (userText1, userText2):
|
| 54 |
+
scores1 = netScores (HEXACO, userText1, 'akhtet/mDeBERTa-v3-base-myXNLI')
|
| 55 |
+
scores2 = netScores (HEXACO, userText2, 'akhtet/mDeBERTa-v3-base-myXNLI')
|
| 56 |
+
|
| 57 |
+
return scoresMatch(HEXACO, scores1, scores2)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
demo = gr.Interface(
|
| 61 |
+
fn=compareDocuments,
|
| 62 |
+
inputs=["text", "text"],
|
| 63 |
+
outputs=["text"],
|
| 64 |
+
)
|
| 65 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
numpy
|