visor841 commited on
Commit
1418942
·
1 Parent(s): 550ca5c

Add python file and requirements

Browse files
Files changed (2) hide show
  1. app.py +71 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 compareTextAndLabels (userText, userLabels):
54
+ userLabelsArray = userLabels.split(",")
55
+ labelsMatches = {}
56
+
57
+ textScores = netScores (HEXACO, userText, 'akhtet/mDeBERTa-v3-base-myXNLI')
58
+ for label in userLabelsArray:
59
+ labelScores = netScores (HEXACO, label, 'akhtet/mDeBERTa-v3-base-myXNLI')
60
+ labelMatch = scoresMatch(HEXACO, textScores, labelScores)
61
+ labelsMatches[label] = str(np.round(labelMatch*100,2))+"%"
62
+
63
+ return labelsMatches
64
+
65
+
66
+ demo = gr.Interface(
67
+ fn=compareTextAndLabels,
68
+ inputs=[gr.Textbox(label="Text"), gr.Textbox(label="Labels (separated by commas)")],
69
+ outputs=[gr.Textbox(label="Label Scores")],
70
+ )
71
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ numpy
3
+ torch --index-url https://download.pytorch.org/whl/cpu
4
+ torchvision --index-url https://download.pytorch.org/whl/cpu
5
+ torchaudio --index-url https://download.pytorch.org/whl/cpu