BioMike commited on
Commit
3a433e8
1 Parent(s): 78a8e3f

Upload 5 files

Browse files
app.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ subprocess.run(["pip", "uninstall", "gradio"])
3
+ subprocess.run(["pip", "install", "gliclass==0.1.3"])
4
+ subprocess.run(["pip", "install", "gradio==4.20.0"])
5
+
6
+ import gradio as gr
7
+ from interfaces import landing_interface, main_pipeline
8
+
9
+ demo = gr.TabbedInterface([landing_interface, main_pipeline],
10
+ ["Introduction", "Zero-Shot Text Classification"],
11
+ title="GLiClass SandBox",
12
+ theme=gr.themes.Base())
13
+
14
+ demo.queue()
15
+ demo.launch(debug=True, share=True)
interfaces/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ from .landing import landing_interface
2
+ from .main_pipeline import main_pipeline
interfaces/landing.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ with open('materials/introduction.html', 'r', encoding='utf-8') as file:
5
+ html_description = file.read()
6
+
7
+ with gr.Blocks() as landing_interface:
8
+ gr.HTML(html_description)
9
+
10
+ with gr.Accordion("How to run this model locally", open=False):
11
+ gr.Markdown(
12
+ """
13
+ ## Installation
14
+ To use this model, you must install the GLiClass Python library:
15
+ ```
16
+ !pip install gliclass
17
+ ```
18
+
19
+ ## Usage
20
+ Once you've downloaded the GLiClass library, you can import the GLiClassModel and ZeroShotClassificationPipeline classes.
21
+ """
22
+ )
23
+ gr.Code(
24
+ '''
25
+ from gliclass import GLiClassModel, ZeroShotClassificationPipeline
26
+ from transformers import AutoTokenizer
27
+
28
+ model = GLiClassModel.from_pretrained("knowledgator/gliclass-small-v1")
29
+ tokenizer = AutoTokenizer.from_pretrained("knowledgator/gliclass-small-v1")
30
+
31
+ pipeline = ZeroShotClassificationPipeline(model, tokenizer, classification_type='multi-label', device='cuda:0')
32
+
33
+ text = "One day I will see the world!"
34
+ labels = ["travel", "dreams", "sport", "science", "politics"]
35
+ results = pipeline(text, labels, threshold=0.5)[0] #because we have one text
36
+
37
+ for result in results:
38
+ print(result["label"], "=>", result["score"])
39
+ ''',
40
+ language="python",
41
+ )
interfaces/main_pipeline.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Union
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoTokenizer
5
+
6
+ from gliclass import GLiClassModel, ZeroShotClassificationPipeline
7
+
8
+ device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
9
+
10
+ model_path = "knowledgator/gliclass-base-v1.0-lw"
11
+ model = GLiClassModel.from_pretrained(model_path)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
13
+
14
+
15
+ multi_label_pipeline = ZeroShotClassificationPipeline(model, tokenizer, classification_type='multi-label', device=device)
16
+ single_label_pipeline = ZeroShotClassificationPipeline(model, tokenizer, classification_type='single-label', device=device)
17
+
18
+
19
+ text1 = """
20
+ "I recently purchased the Sony WH-1000XM4 Wireless Noise-Canceling Headphones from Amazon and I must say, I'm thoroughly impressed. The package arrived in New York within 2 days, thanks to Amazon Prime's expedited shipping.
21
+
22
+ The headphones themselves are remarkable. The noise-canceling feature works like a charm in the bustling city environment, and the 30-hour battery life means I don't have to charge them every day. Connecting them to my Samsung Galaxy S21 was a breeze, and the sound quality is second to none.
23
+
24
+ I also appreciated the customer service from Amazon when I had a question about the warranty. They responded within an hour and provided all the information I needed.
25
+
26
+ However, the headphones did not come with a hard case, which was listed in the product description. I contacted Amazon, and they offered a 10% discount on my next purchase as an apology.
27
+
28
+ Overall, I'd give these headphones a 4.5/5 rating and highly recommend them to anyone looking for top-notch quality in both product and service."""
29
+
30
+ text2 = """
31
+ Apple Inc. is an American multinational technology company headquartered in Cupertino, California. Apple is the world's largest technology company by revenue, with US$394.3 billion in 2022 revenue. As of March 2023, Apple is the world's biggest company by market capitalization. As of June 2022, Apple is the fourth-largest personal computer vendor by unit sales and the second-largest mobile phone manufacturer in the world. It is considered one of the Big Five American information technology companies, alongside Alphabet (parent company of Google), Amazon, Meta Platforms, and Microsoft.
32
+ Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975 to develop and sell BASIC interpreters for the Altair 8800. During his career at Microsoft, Gates held the positions of chairman, chief executive officer, president and chief software architect, while also being the largest individual shareholder until May 2014.
33
+ Apple was founded as Apple Computer Company on April 1, 1976, by Steve Wozniak, Steve Jobs (1955–2011) and Ronald Wayne to develop and sell Wozniak's Apple I personal computer. It was incorporated by Jobs and Wozniak as Apple Computer, Inc. in 1977. The company's second computer, the Apple II, became a best seller and one of the first mass-produced microcomputers. Apple went public in 1980 to instant financial success. The company developed computers featuring innovative graphical user interfaces, including the 1984 original Macintosh, announced that year in a critically acclaimed advertisement called "1984". By 1985, the high cost of its products, and power struggles between executives, caused problems. Wozniak stepped back from Apple and pursued other ventures, while Jobs resigned and founded NeXT, taking some Apple employees with him.
34
+ """
35
+
36
+ text3 = """
37
+ Several studies have reported its pharmacological activities, including anti-inflammatory, antimicrobial, and antitumoral effects.
38
+ The effect of E-anethole was studied in the osteosarcoma MG-63 cell line, and the antiproliferative activity was evaluated by an MTT assay.
39
+ It showed a GI50 value of 60.25 μM with apoptosis induction through the mitochondrial-mediated pathway. Additionally, it induced cell cycle arrest at the G0/G1 phase, up-regulated the expression of p53, caspase-3, and caspase-9, and down-regulated Bcl-xL expression.
40
+ Moreover, the antitumoral activity of anethole was assessed against oral tumor Ca9-22 cells, and the cytotoxic effects were evaluated by MTT and LDH assays.
41
+ It demonstrated a LD50 value of 8 μM, and cellular proliferation was 42.7% and 5.2% at anethole concentrations of 3 μM and 30 μM, respectively.
42
+ It was reported that it could selectively and in a dose-dependent manner decrease cell proliferation and induce apoptosis, as well as induce autophagy, decrease ROS production, and increase glutathione activity. The cytotoxic effect was mediated through NF-kB, MAP kinases, Wnt, caspase-3 and -9, and PARP1 pathways. Additionally, treatment with anethole inhibited cyclin D1 oncogene expression, increased cyclin-dependent kinase inhibitor p21WAF1, up-regulated p53 expression, and inhibited the EMT markers.
43
+ """
44
+ examples = [
45
+ [
46
+ text1,
47
+ "product review, sport, competition, electronics, positive feadback, negative feadback",
48
+ 0.5,
49
+ False
50
+ ],
51
+ [
52
+ text2,
53
+ "business, computers, sport, politics, science",
54
+ 0.5,
55
+ False
56
+ ],
57
+ [
58
+ text3,
59
+ "business, biology, science, politics, positive review",
60
+ 0.5,
61
+ False
62
+ ],
63
+ ]
64
+
65
+ def classification(
66
+ text, labels: str, threshold: float, multi_label: bool = False
67
+ ) -> str:
68
+ labels = labels.split(",")
69
+ if multi_label:
70
+ pipeline = multi_label_pipeline
71
+ else:
72
+ pipeline = single_label_pipeline
73
+ results = pipeline(text, labels, threshold=threshold)[0] #because we have one text
74
+ predictions = {}
75
+ for predict in results:
76
+ predictions[predict["label"]] = predict["score"]
77
+ print(predictions)
78
+ return predictions
79
+
80
+
81
+ with gr.Blocks(title="GLiClass-small-v1.0") as main_pipeline:
82
+ input_text = gr.Textbox(
83
+ value=examples[0][0], label="Text input", placeholder="Enter your text here"
84
+ )
85
+ with gr.Row() as row:
86
+ labels = gr.Textbox(
87
+ value=examples[0][1],
88
+ label="Labels",
89
+ placeholder="Enter your labels here (comma separated)",
90
+ scale=2,
91
+ )
92
+ threshold = gr.Slider(
93
+ 0,
94
+ 1,
95
+ value=0.3,
96
+ step=0.01,
97
+ label="Threshold",
98
+ info="Lower the threshold to increase how many entities get predicted.",
99
+ scale=1,
100
+ )
101
+ multi_label = gr.Checkbox(
102
+ value=examples[0][2],
103
+ label="Multi-label classification",
104
+ info="Allow for multi-label classification?",
105
+ scale=0,
106
+ )
107
+ output = gr.Label(num_top_classes=3)
108
+ submit_btn = gr.Button("Submit")
109
+ examples = gr.Examples(
110
+ examples,
111
+ fn=classification,
112
+ inputs=[input_text, labels, threshold, multi_label],
113
+ outputs=output,
114
+ cache_examples=True,
115
+ )
116
+
117
+ # Submitting
118
+ input_text.submit(
119
+ fn=classification, inputs=[input_text, labels, threshold, multi_label], outputs=output
120
+ )
121
+ labels.submit(
122
+ fn=classification, inputs=[input_text, labels, threshold, multi_label], outputs=output
123
+ )
124
+ threshold.release(
125
+ fn=classification, inputs=[input_text, labels, threshold, multi_label], outputs=output
126
+ )
127
+ submit_btn.click(
128
+ fn=classification, inputs=[input_text, labels, threshold, multi_label], outputs=output
129
+ )
130
+ multi_label.change(
131
+ fn=classification, inputs=[input_text, labels, threshold, multi_label], outputs=output
132
+ )
133
+
134
+ demo.queue()
135
+ demo.launch(debug=True, share=True)
matherials/introduction.html ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>UTC (Universal Token Classification) HandyLab App Description</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ margin: 10px;
11
+ line-height: 1.6;
12
+ }
13
+ .link-button {
14
+ display: inline-flex;
15
+ align-items: center;
16
+ justify-content: center;
17
+ margin: 10px;
18
+ padding: 10px;
19
+ background-color: white;
20
+ border: 1px solid grey; /* Added border to make the button visible against white background */
21
+ color: #007bff; /* Text color changed to make it visible against white background */
22
+ text-decoration: none;
23
+ border-radius: 10px;
24
+ text-align: center;
25
+ vertical-align: middle;
26
+ box-sizing: border-box;
27
+ }
28
+ .link-button:hover {
29
+ background-color: #c0dcfc;
30
+ }
31
+ .link-button img {
32
+ height: 20px;
33
+ width: auto;
34
+ display: block;
35
+ }
36
+ .links-container {
37
+ text-align: center; /* Center the container's content */
38
+ margin: auto; /* Auto margins for horizontal centering if necessary */
39
+ display: flex; /* Use flexbox */
40
+ justify-content: center; /* Center flex items horizontally */
41
+ flex-wrap: wrap; /* Allow items to wrap */
42
+ }
43
+ </style>
44
+ </head>
45
+ <body>
46
+ <h2>Welcome to GliClass SandBox!</h2>
47
+ <h3>GLiClass: Generalist and Lightweight Model for Sequence Classification:</h3>
48
+ <ol>
49
+ <li><b>Zero-Shot text classification</b></li>
50
+ <li><b>Topic Classification</b></li>
51
+ <li><b>Sentiment Analysis</b></li>
52
+ <li><b>Reranker in RAG pipelines</b></li>
53
+ </ol>
54
+ <h3>What is GliClass?</h3>
55
+ <p>This is an efficient zero-shot classifier inspired by <a href="https://github.com/urchade/GLiNER/tree/main">"GLiNER"</a> work. It demonstrates the same performance as a cross-encoder while being more compute-efficient because classification is done at a single forward path. It can be used for topic classification, sentiment analysis and as a reranker in RAG pipelines.<p>
56
+ <h3>Remember, information extraction is not just about data; it's about insights. Let's uncover those insights together!💫</h3>
57
+ <!-- Links Section -->
58
+ <div class="links-container">
59
+ <a href="https://www.knowledgator.com/" class="link-button" target="_blank"><img src="https://assets-global.website-files.com/65902be8ba48a05dfdb73331/6590476fcc8e8f35b2332781_Group%201000002504%20(1).png" alt="Visit our website"></a>
60
+ <a href="https://www.linkedin.com/company/knowledgator/" class="link-button" target="_blank"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/01/LinkedIn_Logo.svg/1280px-LinkedIn_Logo.svg.png" alt="Follow on LinkedIn"></a>
61
+ <a href="https://huggingface.co/knowledgator/" class="link-button" target="_blank"><img src="https://vectorseek.com/wp-content/uploads/2023/12/Hugging-Face-Logo-Vector.svg-.png" alt="Hugging Face Profile"></a>
62
+ <a href="https://twitter.com/knowledgator" class="link-button" target="_blank"><img src="https://freepnglogo.com/images/all_img/1691832278twitter-x-logo-png.png" alt="Follow on X"></a>
63
+ <a href="https://blog.knowledgator.com/" class="link-button" target="_blank"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Medium_%28website%29_logo.svg/2560px-Medium_%28website%29_logo.svg.png" alt="Follow on Medium"></a>
64
+ <a href="https://discord.com/invite/dkyeAgs9DG" class="link-button" target="_blank"><img src="https://assets-global.website-files.com/6257adef93867e50d84d30e2/636e0b52aa9e99b832574a53_full_logo_blurple_RGB.png" alt="Join our Discord">
65
+ </a>
66
+ </div>
67
+ </body>
68
+ </html>