khadija3818 commited on
Commit
8bb404f
·
1 Parent(s): 387c187

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from deepsparse import Pipeline
2
+ import time
3
+ import gradio as gr
4
+
5
+
6
+ task = "zero_shot_text_classification"
7
+ sparse_classification_pipeline = Pipeline.create(
8
+ task=task,
9
+ model_path="zoo:nlp/text_classification/distilbert-none/pytorch/huggingface/mnli/pruned80_quant-none-vnni",
10
+ model_scheme="mnli",
11
+ model_config={"hypothesis_template": "This text is related to {}"},
12
+ )
13
+ def run_pipeline(text):
14
+ sparse_start = time.perf_counter()
15
+ sparse_output = sparse_classification_pipeline(sequences= text,labels=['politics', 'public health', 'Europe'],)
16
+ sparse_result = dict(sparse_output)
17
+ sparse_end = time.perf_counter()
18
+ sparse_duration = (sparse_end - sparse_start) * 1000.0
19
+ dict_r = {sparse_result['labels'][0]:sparse_result['scores'][0],sparse_result['labels'][1]:sparse_result['scores'][1], sparse_result['labels'][2]:sparse_result['scores'][2]}
20
+ return dict_r, sparse_duration
21
+
22
+
23
+ with gr.Blocks() as demo:
24
+ with gr.Row():
25
+ with gr.Column():
26
+ gr.Markdown(markdownn)
27
+
28
+ with gr.Column():
29
+ gr.Markdown("""
30
+ ### Text classification demo
31
+ """)
32
+ text = gr.Text(label="Text")
33
+ btn = gr.Button("Submit")
34
+
35
+ sparse_answers = gr.Label(label="Sparse model answers",
36
+ num_top_classes=3
37
+ )
38
+ sparse_duration = gr.Number(label="Sparse Latency (ms):")
39
+ gr.Examples([["The senate passed 3 laws today"],["Who are you voting for in 2020?"],["Public health is very important"]],inputs=[text],)
40
+
41
+ btn.click(
42
+ run_pipeline,
43
+ inputs=[text],
44
+ outputs=[sparse_answers,sparse_duration],
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ demo.launch()