pm commited on
Commit
3aae778
1 Parent(s): f0ee914

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -7,20 +7,38 @@ from transformers import pipeline
7
  # output = classifier(text, candidate_labels)
8
  # return output
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def classify(text):
11
- classifier = pipeline("zero-shot-classification")
12
  candidate_labels = ["positive", "negative", "neutral"]
13
  output = classifier(text, candidate_labels)
14
- output_labels = [label['label'] for label in output['labels']]
15
- output_scores = [score for score in output['scores']]
16
- sorted_output = sorted(zip(output_labels, output_scores), key=lambda x: x[1], reverse=True)
17
- return sorted_output[:3]
 
 
18
 
19
  demo = gr.Interface(fn=classify,
20
- inputs=gr.Textbox(label="Enter text to classify"),
21
- outputs=gr.Label(num_top_classes=3))
22
-
23
 
24
  demo.launch()
25
-
26
-
 
7
  # output = classifier(text, candidate_labels)
8
  # return output
9
 
10
+ # def classify(text):
11
+ # classifier = pipeline("zero-shot-classification")
12
+ # candidate_labels = ["positive", "negative", "neutral"]
13
+ # output = classifier(text, candidate_labels)
14
+ # output_labels = [label['label'] for label in output['labels']]
15
+ # output_scores = [score for score in output['scores']]
16
+ # sorted_output = sorted(zip(output_labels, output_scores), key=lambda x: x[1], reverse=True)
17
+ # return sorted_output[:3]
18
+
19
+ # demo = gr.Interface(fn=classify,
20
+ # inputs=gr.Textbox(label="Enter text to classify"),
21
+ # outputs=gr.Label(num_top_classes=3))
22
+
23
+
24
+ # demo.launch()
25
+
26
+
27
+
28
+ classifier = pipeline("zero-shot-classification")
29
+
30
  def classify(text):
 
31
  candidate_labels = ["positive", "negative", "neutral"]
32
  output = classifier(text, candidate_labels)
33
+ # Process the output to match Gradio's expected input format for gr.Label
34
+ labels = output['labels']
35
+ scores = output['scores']
36
+ # Construct a simple string representation of top classifications
37
+ top_classes = ', '.join([f"{labels[i]}: {scores[i]:.2f}" for i in range(len(labels))])
38
+ return top_classes
39
 
40
  demo = gr.Interface(fn=classify,
41
+ inputs=gr.Textbox(label="Enter something"),
42
+ outputs=gr.Label())
 
43
 
44
  demo.launch()