eldoraboo commited on
Commit
9c4b465
1 Parent(s): 790cf47

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spacy
3
+ import classy_classification
4
+ import csv
5
+ import pandas as pd
6
+ import os
7
+
8
+ title = "Few-Shot Text Classification with spaCy"
9
+ description = "classy-classification"
10
+
11
+ #define a function to process your input and output
12
+ def few_shot(doc, csv_file):
13
+ df = pd.read_csv(csv_file.name)
14
+ data = {}
15
+ sample_size = 10
16
+
17
+ candidate_labels = df['label'].unique().tolist()
18
+
19
+ for label in candidate_labels:
20
+ candidate_values = df.query(f"`label` == '{label}'").sample(
21
+ n=sample_size)['text'].values.tolist()
22
+ data[label] = candidate_values
23
+
24
+ nlp = spacy.blank("en")
25
+ nlp.add_pipe(
26
+ "text_categorizer",
27
+ config={
28
+ "data": data,
29
+ "model": "sentence-transformers/all-mpnet-base-v2",
30
+ "device": "gpu"
31
+ }
32
+ )
33
+
34
+ dictionary = nlp(doc)._.cats
35
+ return dictionary
36
+
37
+ #create input and output objects
38
+ #input object1
39
+ input1 = gr.Textbox(label="Text")
40
+ #input object2
41
+ input2 = gr.File(label="CSV File")
42
+ #output object
43
+ output = gr.Label(label="Output")
44
+ #example object
45
+ examples = [
46
+ ["Fertiglobe plc produces and distributes nitrogen fertilizers. The company offers ammonia used as a building block for other fertilizer products to industrial and agricultural customers; urea for agricultural and industrial customers; and diesel exhaust fluids for industrial customers. It operates in Europe, North America, South America, the Middle East, Africa, Asia, and Oceania. The company was incorporated in 2019 and is headquartered in Abu Dhabi, the United Arab Emirates.", os.path.join(os.path.dirname(__file__),"files/train1.csv")],
47
+ ["As of December 18, 2009, Media of Medias Public Co. Ltd. was acquired by Grand Canal Land Public Company Limited, in a reverse merger transaction. Media of Medias Public Company Limited, together with its subsidiaries, engages in producing television programs and selling advertising air time, and property development businesses. It also involves in entertainment and recreation business, which include television, VCD business, selling pocket books, and operation of golf course and hotel. The company also engages in the development, construction, rental, and sale of condominiums and plazas. Media of Medias Public Company Limited is based in Bangkok, Thailand.", os.path.join(os.path.dirname(__file__),"files/train2.csv")],
48
+ ["Milsy A.s. provides dairy products. It offers fresh dairy products, steamed unsmoked and smoked cheeses, and spreads of various flavors. The company was founded in 1954 and is based in Banovce nad Bebravou, Slovakia.", os.path.join(os.path.dirname(__file__),"files/train3.csv")]
49
+ ]
50
+ #create interface
51
+ gui = gr.Interface(title=title,
52
+ description=description,
53
+ fn=few_shot,
54
+ inputs=[input1, input2],
55
+ outputs=[output],
56
+ examples=examples)
57
+
58
+ #display the interface
59
+ gui.launch()