File size: 9,228 Bytes
07db68b
26c4ece
 
 
e03a30f
 
 
 
 
cd5c11b
e03a30f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26c4ece
e03a30f
 
 
 
 
cd5c11b
e03a30f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd5c11b
e03a30f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd5c11b
e03a30f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

import gradio as gr
from transformers import pipeline

# Define the necessary pipelines
def load_qa_model():
    return pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")

def load_classifier_model():
    return pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-base-zeroshot-v1.1-all-33")

def load_translator_model(target_language):
    try:
        model_name = f"Helsinki-NLP/opus-mt-en-{target_language}"
        return pipeline("translation", model=model_name)
    except Exception as e:
        print(f"Error loading translation model: {e}")
        return None

def load_generator_model():
    try:
        return pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B", tokenizer="EleutherAI/gpt-neo-2.7B")
    except Exception as e:
        print(f"Error loading text generation model: {e}")
        return None

def load_summarizer_model():
    try:
        return pipeline("summarization", model="facebook/bart-large-cnn")
    except Exception as e:
        print(f"Error loading summarization model: {e}")
        return None

# Define the functions for processing
def process_qa(context, question):
    qa_model = load_qa_model()
    try:
        return qa_model(context=context, question=question)["answer"]
    except Exception as e:
        print(f"Error during question answering: {e}")
        return "Error during question answering"

def process_classifier(text, labels):
    classifier_model = load_classifier_model()
    try:
        return classifier_model(text, labels)["labels"][0]
    except Exception as e:
        print(f"Error during classification: {e}")
        return "Error during classification"

def process_translation(text, target_language):
    translator_model = load_translator_model(target_language)
    if translator_model:
        try:
            return translator_model(text)[0]["translation_text"]
        except Exception as e:
            print(f"Error during translation: {e}")
            return "Translation error"
    return "Model loading error"

def process_generation(prompt):
    generator_model = load_generator_model()
    if generator_model:
        if prompt.strip():
            try:
                return generator_model(prompt, max_length=50)[0]["generated_text"]
            except Exception as e:
                print(f"Error during text generation: {e}")
                return "Text generation error"
        else:
            return "Prompt is empty"
    return "Model loading error"

def process_summarization(text):
    summarizer_model = load_summarizer_model()
    if summarizer_model:
        if text.strip():
            try:
                return summarizer_model(text, max_length=150, min_length=40, do_sample=False)[0]["summary_text"]
            except Exception as e:
                print(f"Error during summarization: {e}")
                return "Summarization error"
        else:
            return "Text is empty"
    return "Model loading error"

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("Choose an NLP task and input the required text.")

    with gr.Tab("Single-Models"):
        gr.Markdown("This tab is for single models demonstration.")

        task_select_single = gr.Dropdown(["Question Answering", "Zero-Shot Classification", "Translation", "Text Generation", "Summarization"], label="Select Task")
        input_text_single = gr.Textbox(label="Input Text")

        # Additional inputs for specific tasks
        context_input_single = gr.Textbox(label="Context", visible=False)
        label_input_single = gr.CheckboxGroup(["positive", "negative", "neutral"], label="Labels", visible=False)
        target_language_input_single = gr.Dropdown(["nl", "fr", "es", "de"], label="Target Language", visible=False)

        output_text_single = gr.Textbox(label="Output")
        execute_button_single = gr.Button("Execute")

        def update_inputs(task):
            if task == "Question Answering":
                return {
                    context_input_single: gr.update(visible=True),
                    label_input_single: gr.update(visible=False),
                    target_language_input_single: gr.update(visible=False)
                }
            elif task == "Zero-Shot Classification":
                return {
                    context_input_single: gr.update(visible=False),
                    label_input_single: gr.update(visible=True),
                    target_language_input_single: gr.update(visible=False)
                }
            elif task == "Translation":
                return {
                    context_input_single: gr.update(visible=False),
                    label_input_single: gr.update(visible=False),
                    target_language_input_single: gr.update(visible=True)
                }
            elif task == "Text Generation":
                return {
                    context_input_single: gr.update(visible=False),
                    label_input_single: gr.update(visible=False),
                    target_language_input_single: gr.update(visible=False)
                }
            elif task == "Summarization":
                return {
                    context_input_single: gr.update(visible=False),
                    label_input_single: gr.update(visible=False),
                    target_language_input_single: gr.update(visible=False)
                }
            else:
                return {
                    context_input_single: gr.update(visible=False),
                    label_input_single: gr.update(visible=False),
                    target_language_input_single: gr.update(visible=False)
                }

        task_select_single.change(fn=update_inputs, inputs=task_select_single,
                                  outputs=[context_input_single, label_input_single, target_language_input_single])

        def execute_task_single(task, input_text, context, labels, target_language):
            if task == "Question Answering":
                return process_qa(context=context, question=input_text)
            elif task == "Zero-Shot Classification":
                if not labels:
                    return "Please provide labels for classification."
                return process_classifier(text=input_text, labels=labels)
            elif task == "Translation":
                if not target_language:
                    return "Please select a target language for translation."
                return process_translation(text=input_text, target_language=target_language)
            elif task == "Text Generation":
                return process_generation(prompt=input_text)
            elif task == "Summarization":
                return process_summarization(text=input_text)
            else:
                return "Invalid task selected."

        execute_button_single.click(
            execute_task_single,
            inputs=[task_select_single, input_text_single, context_input_single, label_input_single, target_language_input_single],
            outputs=output_text_single
        )

    with gr.Tab("Multi-Model Task"):
        gr.Markdown("This tab allows you to execute all tasks sequentially.")

        # Inputs for all tasks
        input_text_multi = gr.Textbox(label="Input Text")
        context_input_multi = gr.Textbox(label="Context (for QA)")
        label_input_multi = gr.CheckboxGroup(["positive", "negative", "neutral"], label="Labels (for Classification)")
        target_language_input_multi = gr.Dropdown(["nl", "fr", "es", "de"], label="Target Language (for Translation)")

        # Outputs for all tasks
        output_qa = gr.Textbox(label="QA Output")
        output_classification = gr.Textbox(label="Classification Output")
        output_translation = gr.Textbox(label="Translation Output")
        output_generation = gr.Textbox(label="Text Generation Output")
        output_summarization = gr.Textbox(label="Summarization Output")

        execute_button_multi = gr.Button("Execute All Tasks")

        def execute_all_tasks(input_text, context, labels, target_language):
            # Process Question Answering
            qa_output = process_qa(context=context, question=input_text)

            # Process Classification
            classification_output = process_classifier(text=input_text, labels=labels)

            # Process Translation
            translation_output = process_translation(text=input_text, target_language=target_language)

            # Process Text Generation using QA output
            generation_output = process_generation(prompt=qa_output)

            # Process Summarization using Text Generation output
            summarization_output = process_summarization(text=generation_output)

            # Return all outputs
            return qa_output, classification_output, translation_output, generation_output, summarization_output

        execute_button_multi.click(
            execute_all_tasks,
            inputs=[input_text_multi, context_input_multi, label_input_multi, target_language_input_multi],
            outputs=[output_qa, output_classification, output_translation, output_generation, output_summarization]
        )

demo.launch()