srush commited on
Commit
9b9d9ed
·
1 Parent(s): d782de7

Upload with huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +28 -33
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,52 +1,47 @@
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
- ner_prompt = NERPrompt(backend.OpenAI())
32
- team_prompt = TeamPrompt(backend.OpenAI())
33
- prompt = ner_prompt.chain(team_prompt)
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
- gradio = prompt.to_gradio(fields =["text_input", "labels", "domain"],
43
- examples=[["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.", "Team, Date", "Sports"]])
 
 
 
 
44
 
45
-
46
  if __name__ == "__main__":
47
  gradio.launch()
48
 
49
-
50
  # View prompt examples.
51
 
52
  # + tags=["hide_inp"]
 
1
+ # + tags=["hide_inp"]
2
 
3
+ desc = """
4
+ ### Named Entity Recognition
5
 
6
+ Chain that does named entity recognition with arbitrary labels. [[Code](https://github.com/srush/MiniChain/blob/main/examples/ner.py)]
7
 
8
+ (Adapted from [promptify](https://github.com/promptslab/Promptify/blob/main/promptify/prompts/nlp/templates/ner.jinja)).
9
+ """
10
+ # -
11
 
12
+ # $
13
 
14
+ from minichain import prompt, show, OpenAI
 
15
 
16
+ @prompt(OpenAI(), template_file = "ner.pmpt.tpl", parser="json")
17
+ def ner_extract(model, **kwargs):
18
+ return model(kwargs)
19
 
20
+ @prompt(OpenAI())
21
+ def team_describe(model, inp):
22
+ query = "Can you describe these basketball teams? " + \
23
+ " ".join([i["E"] for i in inp if i["T"] =="Team"])
24
+ return model(query)
25
 
 
 
 
 
26
 
27
+ def ner(text_input, labels, domain):
28
+ extract = ner_extract(dict(text_input=text_input, labels=labels, domain=domain))
29
+ return team_describe(extract)
30
 
 
31
 
32
+ # $
 
 
 
 
 
 
 
 
 
 
33
 
34
+ gradio = show(ner,
35
+ examples=[["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.", "Team, Date", "Sports"]],
36
+ description=desc,
37
+ subprompts=[ner_extract, team_describe],
38
+ code=open("ner.py", "r").read().split("$")[1].strip().strip("#").strip(),
39
+ )
40
 
 
41
  if __name__ == "__main__":
42
  gradio.launch()
43
 
44
+
45
  # View prompt examples.
46
 
47
  # + tags=["hide_inp"]
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- gradio
2
  git+https://github.com/srush/minichain@gradio
3
  manifest-ml
 
 
1
+ gradio==3.21.0
2
  git+https://github.com/srush/minichain@gradio
3
  manifest-ml
4
+ faiss-cpu