srush commited on
Commit
f61705b
·
1 Parent(s): e275c0b

Upload with huggingface_hub

Browse files
Files changed (3) hide show
  1. app.py +57 -0
  2. ner.pmpt.tpl +10 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # # NER
2
+
3
+ # Notebook implementation of named entity recognition.
4
+ # Adapted from [promptify](https://github.com/promptslab/Promptify/blob/main/promptify/prompts/nlp/templates/ner.jinja).
5
+
6
+ import json
7
+
8
+ import minichain
9
+
10
+ # Prompt to extract NER tags as json
11
+
12
+ class NERPrompt(minichain.TemplatePrompt):
13
+ template_file = "ner.pmpt.tpl"
14
+
15
+ def parse(self, response, inp):
16
+ return json.loads(response)
17
+
18
+ # Use NER to ask a simple queston.
19
+
20
+ class TeamPrompt(minichain.Prompt):
21
+ def prompt(self, inp):
22
+ return "Can you describe these basketball teams? " + \
23
+ " ".join([i["E"] for i in inp if i["T"] =="Team"])
24
+
25
+ def parse(self, response, inp):
26
+ return response
27
+
28
+ # Run the system.
29
+
30
+ with minichain.start_chain("ner") as backend:
31
+ p1 = NERPrompt(backend.OpenAI())
32
+ p2 = TeamPrompt(backend.OpenAI())
33
+ prompt = p1.chain(p2)
34
+ results = prompt(
35
+ {"text_input": "An NBA playoff pairing a year ago, the 76ers (39-20) meet the Miami Heat (32-29) for the first time this season on Monday night at home.",
36
+ "labels" : ["Team", "Date"],
37
+ "domain": "Sports"
38
+ }
39
+ )
40
+ print(results)
41
+
42
+ # View prompt examples.
43
+
44
+ # + tags=["hide_inp"]
45
+ NERPrompt().show(
46
+ {
47
+ "input": "I went to New York",
48
+ "domain": "Travel",
49
+ "labels": ["City"]
50
+ },
51
+ '[{"T": "City", "E": "New York"}]',
52
+ )
53
+ # -
54
+
55
+ # View log.
56
+
57
+ minichain.show_log("ner.log")
ner.pmpt.tpl ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a highly intelligent and accurate {{ domain }} domain Named-entity recognition(NER) system. You take Passage as input and your task is to recognize and extract specific types of {{ domain }} domain named entities in that given passage and classify into a set of following predefined entity types:
2
+
3
+ {% for l in labels %}
4
+ * {{ l }}
5
+ {% endfor %}
6
+
7
+ Your output format is only {{ output_format|default('[{"T": type of entity from predefined entity types, "E": entity in the input text}]') }} form, no other form.
8
+
9
+ Input: {{ text_input }}
10
+ Output:
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ git+https://github.com/srush/minichain
3
+ manifest-ml