import gradio as gr from transformers import pipeline from resources import * bellamy_bowie_classifier_candidate_labels = ["manager", "engineer", "technician", "politician", "scientist", "student", "journalist", "marketeer", "spokesperson", "other"] bellamy_bowie_classifier_candidate_labels_preselection = ["manager", "engineer", "technician", "politician", "scientist", "student", "journalist"] bellamy_bowie_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") def bellamy_bowie_predict(candidate_labels_selected, sequence): outputs = bellamy_bowie_classifier(sequence, candidate_labels_selected) return dict(zip(outputs['labels'], outputs['scores'])) # Extract labels and scores from the outputs dictionary def ellis_update(name, age): return f"Welcome to Gradio, {name}! Are your really good {age} years old?" # Ellis Cappy stuff ellis_cappy_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", max_new_tokens=40) def ellis_cappy_captionizer(img): captions = ellis_cappy_captioner(img) return captions[0]["generated_text"] def marvin_update(origin, name): # origin = type(origin) return f"Welcome to Gradio, {name}! Are your really from {origin[0]}?" with gr.Blocks() as demo: gr.Markdown("Start typing below and then click **Run** to see the output.") with gr.Tab("Bellamy Bowie"): with gr.Row(): with gr.Column(scale=3): gr.HTML(bellamy_bowie_description) with gr.Column(scale=1): gr.Image(bellamy_bowie_hero, label=None) with gr.Row(): with gr.Column(scale=1): bellamy_bowie_checkbox_input = gr.CheckboxGroup(choices=bellamy_bowie_classifier_candidate_labels, value=bellamy_bowie_classifier_candidate_labels_preselection, label="Target personas of your message", info="Recommendation: Don't change the preselection for your first analysis.") bellamy_bowie_textbox_input = gr.Textbox(lines=10, placeholder="Your text goes here", label="Write or paste your message to classify") with gr.Row(): bellamy_bowie_clear_button = gr.ClearButton(components=bellamy_bowie_textbox_input, value="Clear") bellamy_bowie_submit_button = gr.Button("Submit", variant="primary") with gr.Column(scale=1): bellamy_bowie_outputs = gr.Label(label="Matching scores by personas") gr.HTML(bellamy_bowie_note_quality) with gr.Row(): with gr.Column(scale=1): gr.Examples(bellamy_bowie_examples, inputs=[bellamy_bowie_textbox_input]) gr.HTML(bellamy_bowie_article) bellamy_bowie_submit_button.click(fn=bellamy_bowie_predict, inputs=[bellamy_bowie_checkbox_input, bellamy_bowie_textbox_input], outputs=bellamy_bowie_outputs) with gr.Tab("Urly & Murly Simmy"): with gr.Row(): with gr.Column(scale=3): gr.HTML(ellis_cappy_description) with gr.Column(scale=1): gr.Image(ellis_cappy_hero) with gr.Row(): inp_01 = gr.Textbox(placeholder="What is your name?") inp_02 = gr.Textbox(placeholder="What is your age?") out_0 = gr.Textbox() btn_0 = gr.Button("Run") btn_0.click(fn=ellis_update, inputs=[inp_01, inp_02], outputs=out_0) with gr.Tab("Ellis Cappy"): with gr.Row(): with gr.Column(scale=3): gr.HTML(ellis_cappy_description) with gr.Column(scale=1): gr.Image(ellis_cappy_hero) with gr.Row(): with gr.Column(scale=1): ellis_cappy_image_input = gr.Image(type="pil", label=None) ellis_cappy_submit_button = gr.Button("Submit") with gr.Column(scale=1): ellis_cappy_textbox_output = gr.Textbox(label="Suggested caption", lines=2) gr.HTML(ellis_cappy_note_quality) with gr.Row(): with gr.Column(scale=1): gr.Examples(ellis_cappy_examples, inputs=[ellis_cappy_image_input]) gr.HTML(ellis_cappy_article) ellis_cappy_submit_button.click(fn=ellis_cappy_captionizer, inputs=ellis_cappy_image_input, outputs=ellis_cappy_textbox_output, api_name="captionizer") demo.launch()