rocket-yg commited on
Commit
c7f3d38
1 Parent(s): 5b752af

setting language pipes

Browse files
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +67 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ get_completion = pipeline("summarization",model="sshleifer/distilbart-cnn-12-6")
5
+ get_ner = pipeline("ner", model="dslim/bert-base-NER")
6
+ get_caption = pipeline("image-to-text")
7
+ def summarize_text(input):
8
+ output = get_completion(input)
9
+ return output[0]['summary_text']
10
+
11
+ def merge_tokens(tokens):
12
+ merged_tokens = []
13
+ for token in tokens:
14
+ if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
15
+ # If current token continues the entity of the last one, merge them
16
+ last_token = merged_tokens[-1]
17
+ last_token['word'] += token['word'].replace('##', '')
18
+ last_token['end'] = token['end']
19
+ last_token['score'] = (last_token['score'] + token['score']) / 2
20
+ else:
21
+ # Otherwise, add the token to the list
22
+ merged_tokens.append(token)
23
+ return merged_tokens
24
+
25
+ def named_entity_recognition(input):
26
+ output = get_ner(input)
27
+ merged_output = merge_tokens(output)
28
+ return {"text": input, "entities": output}
29
+
30
+
31
+ interface_summarise = gr.Interface(fn=summarize_text,
32
+ inputs=[gr.Textbox(label="Text to summarise", lines=5)],
33
+ outputs=[gr.Textbox(label="Summary")],
34
+ title="Text Summarizer",
35
+ description="Summary of text via `distillBART-CNN` model!")
36
+
37
+ interface_ner = gr.Interface(fn=named_entity_recognition,
38
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
39
+ outputs=[gr.HighlightedText(label="Text with entities")],
40
+ title="NER with dslim/bert-base-NER",
41
+ description="Find entities using the `dslim/bert-base-NER` model under the hood!",
42
+ allow_flagging="never",
43
+ examples=[
44
+ "Tim Cook is the CEO of Apple, stays in California and makes iPhones ",
45
+ "My name is Bose and I am a physicist living in Delhi"
46
+ ])
47
+
48
+ demo = gr.TabbedInterface([
49
+ interface_summarise,
50
+ interface_ner],
51
+ ["Text Summary ",
52
+ "Named Entity Recognition"
53
+ ])
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch(enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ altair==5.1.2
3
+ annotated-types==0.6.0
4
+ anyio==3.7.1
5
+ attrs==23.1.0
6
+ certifi==2023.7.22
7
+ charset-normalizer==3.3.1
8
+ click==8.1.7
9
+ contourpy==1.1.1
10
+ cycler==0.12.1
11
+ exceptiongroup==1.1.3
12
+ fastapi==0.104.0
13
+ ffmpy==0.3.1
14
+ filelock==3.13.0
15
+ fonttools==4.43.1
16
+ fsspec==2023.10.0
17
+ gradio==3.50.2
18
+ gradio_client==0.6.1
19
+ h11==0.14.0
20
+ httpcore==0.18.0
21
+ httpx==0.25.0
22
+ huggingface-hub==0.17.3
23
+ idna==3.4
24
+ importlib-resources==6.1.0
25
+ Jinja2==3.1.2
26
+ jsonschema==4.19.1
27
+ jsonschema-specifications==2023.7.1
28
+ kiwisolver==1.4.5
29
+ MarkupSafe==2.1.3
30
+ matplotlib==3.8.0
31
+ mpmath==1.3.0
32
+ networkx==3.2.1
33
+ numpy==1.26.1
34
+ orjson==3.9.10
35
+ packaging==23.2
36
+ pandas==2.1.2
37
+ Pillow==10.1.0
38
+ pydantic==2.4.2
39
+ pydantic_core==2.10.1
40
+ pydub==0.25.1
41
+ pyparsing==3.1.1
42
+ python-dateutil==2.8.2
43
+ python-multipart==0.0.6
44
+ pytz==2023.3.post1
45
+ PyYAML==6.0.1
46
+ referencing==0.30.2
47
+ regex==2023.10.3
48
+ requests==2.31.0
49
+ rpds-py==0.10.6
50
+ safetensors==0.4.0
51
+ semantic-version==2.10.0
52
+ six==1.16.0
53
+ sniffio==1.3.0
54
+ starlette==0.27.0
55
+ sympy==1.12
56
+ tokenizers==0.14.1
57
+ toolz==0.12.0
58
+ torch==2.1.0
59
+ torchaudio==2.1.0
60
+ torchvision==0.16.0
61
+ tqdm==4.66.1
62
+ transformers==4.34.1
63
+ typing_extensions==4.8.0
64
+ tzdata==2023.3
65
+ urllib3==2.0.7
66
+ uvicorn==0.23.2
67
+ websockets==11.0.3