File size: 9,374 Bytes
4389dd9
4f390ee
4389dd9
 
ab66837
d0bce20
b2ae370
 
aa638de
ad69aa3
d0bce20
 
 
aa638de
ad69aa3
 
 
 
 
 
 
 
 
 
b2ae370
ad69aa3
 
 
 
3313aa0
 
5753c7d
 
 
 
 
 
 
b2ae370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4389dd9
 
d0bce20
4f390ee
4389dd9
 
 
 
4f390ee
4389dd9
 
aa638de
 
 
 
 
 
 
 
 
 
 
 
 
 
9b8b546
aa638de
 
 
4f390ee
4389dd9
 
 
4f390ee
aa638de
 
 
 
 
 
 
 
 
a3186b0
c836a14
ad69aa3
4f390ee
 
 
6064589
 
d476e13
aa638de
d476e13
4389dd9
ad69aa3
 
 
 
 
 
 
 
 
 
387abf0
3313aa0
ad69aa3
 
 
ab66837
ad69aa3
5296cfd
ab66837
5296cfd
 
 
 
 
 
48a7b74
3313aa0
 
5753c7d
ad69aa3
4389dd9
2226656
 
 
 
 
 
 
 
 
5753c7d
 
2226656
 
 
 
b24c709
 
 
 
 
 
ab66837
2226656
 
7ec88b0
b24c709
7ec88b0
5753c7d
4389dd9
 
 
7ec88b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import gradio as gr
from transformers import pipeline

playground = gr.Blocks()

image_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
summary_pipe = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
ner_pipe = pipeline("ner", model="dslim/bert-base-NER")


def launch_image_pipe(input):
    out = image_pipe(input)
    return out[0]['generated_text']
   
def translate(input_text, source, target):
    try:
      model = f"Helsinki-NLP/opus-mt-{source}-{target}"
      pipe = pipeline("translation", model=model)
      translation = pipe(input_text)
      return translation[0]['translation_text'], ""
    except KeyError:
      return "", f"Error: Translation direction {source_readable} to {target} is not supported by Helsinki Translation Models"

def summarize(input):
    output = summary_pipe(input)
    summary_origin = output[0]['summary_text']
    summary_translated = translate(summary_origin,'en','fr')
    return summary_origin, summary_translated[0]

def reset_input():
    return ""

def text_reset():
    return "","",""

def ner_reset():
    return "",""

def merge_tokens(tokens):
    merged_tokens = []
    for token in tokens:
        if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
            # If current token continues the entity of the last one, merge them
            last_token = merged_tokens[-1]
            last_token['word'] += token['word'].replace('##', '')
            last_token['end'] = token['end']
            last_token['score'] = (last_token['score'] + token['score']) / 2
        else:
            # Otherwise, add the token to the list
            merged_tokens.append(token)

    return merged_tokens

def ner(input):
    output = ner_pipe(input)
    merged_tokens = merge_tokens(output)
    return {"text": input, "entities": merged_tokens}
    
def create_playground_header():
    gr.Markdown("""
                # 🤗 Hugging Face Labs
                **Explore different LLM on Hugging Face platform. Just play and enjoy**
                """)

def create_playground_footer():
    gr.Markdown("""
                **To Learn More about 🤗 Hugging Face, [Click Here](https://huggingface.co/docs)**
                """)

# def create_tabs_header(topic, description, references):
#     with gr.Row():
#         with gr.Column(scale=4):
#             # reference_list = "> " + "\n> ".join(references)
#             # content  = f"## {topic}\n"
#             # content += f"### {description}\n"
#             # for ref in references:
#             #     content += f"> {ref}\n"
#             # gr.Markdown(content)
#             gr.Markdown("""
#                         ## Image Captioning
#                         ### Upload a image, check what AI understand and have vision on it.
#                         > category: Image-to-Text, model: [Salesforce/blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base)
#                         """)
            
#         with gr.Column(scale=1):
#             test_pipeline_button = gr.Button(value="Start Process", variant="primary")
#         return test_pipeline_button

with playground:
    create_playground_header()
    with gr.Tabs():
        with gr.TabItem("Image"):
            with gr.Row():
                with gr.Column(scale=4):
                    gr.Markdown("""
                                ## Image Captioning
                                ### Upload a image, check what AI understand and have vision on it.
                                > category: Image-to-Text, model: [Salesforce/blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base)
                                """)
                with gr.Column(scale=1):
                    ITT_button = gr.Button(value="Start Process", variant="primary")
                    
            
            with gr.Row():
                with gr.Column():
                    img = gr.Image(type='pil')
                with gr.Column():
                    generated_textbox = gr.Textbox(lines=2, placeholder="", label="Generated Text")
                    ITT_Clear_button = gr.ClearButton(components=[img, generated_textbox], value="Clear")
                        
            ITT_button.click(launch_image_pipe, inputs=[img], outputs=[generated_textbox])
        
        with gr.TabItem("Text"):
            with gr.Row():
                with gr.Column(scale=4):
                    gr.Markdown("""
                                ## Text Summarization and Translation
                                ### Summarize the paragraph and translate it into other language.
                                > pipeline: summarization, model: [sshleifer/distilbart-cnn-12-6](https://huggingface.co/sshleifer/distilbart-cnn-12-6)
                                > pipeline: translation, model: [Helsinki-NLP/opus-mt-en-fr](https://huggingface.co/Helsinki-NLP/opus-mt-en-fr)
                                """)
                    
                with gr.Column(scale=1):
                    text_pipeline_button = gr.Button(value="Start Process", variant="primary")
                    text_reset_button = gr.Button(value="Clear")

            with gr.Row():
                with gr.Column():
                    source_text = gr.Textbox(label="Text to summarize", lines=10)                    
                with gr.Column():
                    summary_textoutput = gr.Textbox(lines=3, placeholder="", label="Text Summarization")
                    translated_textbox = gr.Textbox(lines=3, placeholder="", label="Translated Result")
            
            with gr.Row():
                with gr.Column():
                    gr.Examples(examples=[
                            "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.", 
                            "Tower Bridge is a Grade I listed combined bascule, suspension, and, until 1960, cantilever bridge[1] in London, built between 1886 and 1894, designed by Horace Jones and engineered by John Wolfe Barry with the help of Henry Marc Brunel.[2] It crosses the River Thames close to the Tower of London and is one of five London bridges owned and maintained by the City Bridge Foundation, a charitable trust founded in 1282. The bridge was constructed to connect the 39 per cent of London's population that lived east of London Bridge, while allowing shipping to access the Pool of London between the Tower of London and London Bridge. The bridge was opened by Edward, Prince of Wales and Alexandra, Princess of Wales on 30 June 1894."
                    ], inputs=[source_text], outputs=[summary_textoutput, translated_textbox], run_on_click=True, cache_examples=True, fn=summarize)
 
            text_pipeline_button.click(summarize, inputs=[source_text], outputs=[summary_textoutput, translated_textbox])
            text_reset_button.click(text_reset, outputs=[source_text,summary_textoutput,translated_textbox])
            
        with gr.TabItem("Name Entity"):
            with gr.Row():
                with gr.Column(scale=4):
                    gr.Markdown("""
                                ## Find entities
                                ### Entities involved Name, Organization, and Location.
                                > pipeline: ner, model: [dslim/bert-base-NER](https://huggingface.co/dslim/bert-base-NER)
                                """)
                    
                with gr.Column(scale=1):
                    ner_pipeline_button = gr.Button(value="Start Process", variant="primary")
                    ner_reset_button = gr.Button(value="Clear")
                    
            with gr.Row():
                with gr.Column():
                    ner_text_input = gr.Textbox(label="Text to find entities", lines=2)
                    
                with gr.Column():
                    ner_text_output = gr.HighlightedText(label="Text with entities")
 
            with gr.Row():
                with gr.Column():
                    gr.Examples(examples=[
                        "My name is Ray, I'm learning through Hugging Face and DeepLearning.AI and I live in Caversham, Reading", 
                        "My name is Raymond, I work at A&O IT Group"
                    ], inputs=[ner_text_input], outputs=[ner_text_output], run_on_click=True, cache_examples=True, fn=ner)
 
            ner_pipeline_button.click(ner, inputs=[ner_text_input], outputs=[ner_text_output])
            ner_reset_button.click(ner_reset, outputs=[ner_text_input, ner_text_output])
            
    create_playground_footer()

playground.launch()