Spaces:
Sleeping
Sleeping
Add python file and requirements
Browse files- app.py +47 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def netScores(tagList: list, sequence_to_classify: str, modelName: str) -> dict:
|
6 |
+
classifier = pipeline("zero-shot-classification", model=modelName)
|
7 |
+
hypothesis_template_pos = "This example is {}"
|
8 |
+
hypothesis_template_neg = "This example is not {}"
|
9 |
+
output_pos = classifier(sequence_to_classify, tagList, hypothesis_template=hypothesis_template_pos, multi_label=True)
|
10 |
+
output_neg = classifier(sequence_to_classify, tagList, hypothesis_template=hypothesis_template_neg, multi_label=True)
|
11 |
+
|
12 |
+
positive_scores = {}
|
13 |
+
for x in range(len(tagList)):
|
14 |
+
positive_scores[output_pos["labels"][x]] = output_pos["scores"][x]
|
15 |
+
|
16 |
+
negative_scores = {}
|
17 |
+
for x in range(len(tagList)):
|
18 |
+
negative_scores[output_neg["labels"][x]] = output_neg["scores"][x]
|
19 |
+
|
20 |
+
pos_neg_scores = {}
|
21 |
+
for tag in tagList:
|
22 |
+
pos_neg_scores[tag] = [positive_scores[tag],negative_scores[tag]]
|
23 |
+
|
24 |
+
net_scores = {}
|
25 |
+
for tag in tagList:
|
26 |
+
net_scores[tag] = positive_scores[tag]-negative_scores[tag]
|
27 |
+
|
28 |
+
net_scores = dict(sorted(net_scores.items(), key=lambda x:x[1], reverse=True))
|
29 |
+
|
30 |
+
return net_scores
|
31 |
+
|
32 |
+
def compareTextAndLabels (userText, userLabels):
|
33 |
+
userLabelsArray = userLabels.split(",")
|
34 |
+
|
35 |
+
labelsScores = netScores (userLabelsArray, userText, 'akhtet/mDeBERTa-v3-base-myXNLI')
|
36 |
+
for label in labelsScores:
|
37 |
+
labelsScores[label] = str(np.round(labelsScores[label]*100,2))+"%"
|
38 |
+
|
39 |
+
return labelsScores
|
40 |
+
|
41 |
+
|
42 |
+
demo = gr.Interface(
|
43 |
+
fn=compareTextAndLabels,
|
44 |
+
inputs=[gr.Textbox(label="Text"), gr.Textbox(label="Labels (separated by commas)")],
|
45 |
+
outputs=[gr.Textbox(label="Label Scores")],
|
46 |
+
)
|
47 |
+
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
|