Spaces:
Sleeping
Sleeping
Adding image captioning
Browse files
app.py
CHANGED
@@ -1,35 +1,45 @@
|
|
1 |
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# text summarizer
|
5 |
summarizer = pipeline("summarization", model = "facebook/bart-large-cnn")
|
6 |
-
|
7 |
def get_summary(text):
|
8 |
output = summarizer(text)
|
9 |
return output[0]["summary_text"]
|
10 |
|
11 |
-
# named entity recognition
|
12 |
ner_model = pipeline("ner", model = "dslim/bert-large-NER")
|
13 |
-
|
14 |
def get_ner(text):
|
15 |
output = ner_model(text)
|
16 |
return {"text":text, "entities":output}
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
demo = gr.Blocks()
|
19 |
with demo:
|
20 |
gr.Markdown("# Try out some cool tasks!")
|
21 |
-
with gr.Tab("Text
|
22 |
sum_input = [gr.Textbox(label="Text to Summarize", placeholder="Enter text to summarize...", lines=4)]
|
23 |
-
sum_output = [gr.Textbox(label="Summarized Text")]
|
24 |
sum_btn = gr.Button("Summarize text")
|
|
|
25 |
sum_btn.click(get_summary, sum_input, sum_output)
|
26 |
with gr.Tab("Named Entity Recognition"):
|
27 |
ner_input = [gr.Textbox(label="Text to find Entities", placeholder = "Enter text...", lines = 4)]
|
28 |
# ner_output = gr.Textbox()
|
29 |
ner_output = [gr.HighlightedText(label="Text with entities")]
|
30 |
-
allow_flagging = "never"
|
31 |
ner_btn = gr.Button("Generate entities")
|
|
|
32 |
ner_btn.click(get_ner, ner_input, ner_output)
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
demo.launch()
|
|
|
1 |
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# 1. text summarizer
|
5 |
summarizer = pipeline("summarization", model = "facebook/bart-large-cnn")
|
|
|
6 |
def get_summary(text):
|
7 |
output = summarizer(text)
|
8 |
return output[0]["summary_text"]
|
9 |
|
10 |
+
# 2. named entity recognition
|
11 |
ner_model = pipeline("ner", model = "dslim/bert-large-NER")
|
|
|
12 |
def get_ner(text):
|
13 |
output = ner_model(text)
|
14 |
return {"text":text, "entities":output}
|
15 |
|
16 |
+
# 3. Image Captioning
|
17 |
+
caption_model = pipeline("image-to-text", model = "Salesforce/blip-image-captioning-base")
|
18 |
+
# processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
19 |
+
def get_caption(img):
|
20 |
+
output = caption_model(img)
|
21 |
+
return output[0]["generated_text"]
|
22 |
+
|
23 |
+
|
24 |
demo = gr.Blocks()
|
25 |
with demo:
|
26 |
gr.Markdown("# Try out some cool tasks!")
|
27 |
+
with gr.Tab("Text Summarization"):
|
28 |
sum_input = [gr.Textbox(label="Text to Summarize", placeholder="Enter text to summarize...", lines=4)]
|
|
|
29 |
sum_btn = gr.Button("Summarize text")
|
30 |
+
sum_output = [gr.Textbox(label="Summarized Text")]
|
31 |
sum_btn.click(get_summary, sum_input, sum_output)
|
32 |
with gr.Tab("Named Entity Recognition"):
|
33 |
ner_input = [gr.Textbox(label="Text to find Entities", placeholder = "Enter text...", lines = 4)]
|
34 |
# ner_output = gr.Textbox()
|
35 |
ner_output = [gr.HighlightedText(label="Text with entities")]
|
|
|
36 |
ner_btn = gr.Button("Generate entities")
|
37 |
+
# allow_flagging = "never"
|
38 |
ner_btn.click(get_ner, ner_input, ner_output)
|
39 |
+
with gr.Tab("Image Captioning"):
|
40 |
+
cap_input = [gr.Image(label="Upload Image"), type="pil"]
|
41 |
+
cap_btn = gr.Button("Generate Caption")
|
42 |
+
cap_output = [gr.Textbox(label="Caption")]
|
43 |
+
cap_btn.click(get_caption, cap_input, cap_output)
|
44 |
|
45 |
demo.launch()
|