File size: 7,961 Bytes
c357e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
import torch
import warnings
import numpy as np
from PIL import Image
warnings.filterwarnings('ignore')

device = "cuda" if torch.cuda.is_available() else "cpu"

# Dicionário expandido de modelos
models = {
    'transcription': pipeline("automatic-speech-recognition", model="openai/whisper-small", device=device),
    'translation': pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt", device=device),
    'summarization': pipeline("summarization", model="facebook/bart-large-cnn", device=device),
    'sentiment': pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment", device=device),
    'question_answering': pipeline("question-answering", model="deepset/roberta-base-squad2", device=device),
    'chat': pipeline("text-generation", model="facebook/opt-125m", device=device),
    'image_caption': pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device=device),
    'text_to_speech': pipeline("text-to-audio", model="facebook/mms-tts-eng", device=device),
    'zero_shot': pipeline("zero-shot-classification", model="facebook/bart-large-mnli", device=device),
    'ner': pipeline("token-classification", model="dslim/bert-base-NER", device=device)
}

def process_transcription(audio):
    if not audio:
        return "Por favor, forneça um arquivo de áudio."
    return models['transcription'](audio)["text"]

def process_translation(text, direction):
    if not text:
        return "Por favor, forneça um texto para tradução."
    return models['translation'](text, 
        src_lang="pt" if direction=="pt_en" else "en", 
        tgt_lang="en" if direction=="pt_en" else "pt")[0]['translation_text']

def process_summarization(text):
    if not text:
        return "Por favor, forneça um texto para resumir."
    return models['summarization'](text, max_length=130, min_length=30)[0]['summary_text']

def process_sentiment(text):
    if not text:
        return "Por favor, forneça um texto para análise."
    result = models['sentiment'](text)[0]
    return f"Sentimento: {result['label']} (Score: {result['score']:.2f})"

def process_qa(question, context):
    if not question or not context:
        return "Por favor, forneça tanto a pergunta quanto o contexto."
    return models['question_answering'](question=question, context=context)['answer']

def process_chat(message, history):
    if not message:
        return "", history
    response = models['chat'](message, max_length=100, do_sample=True)[0]['generated_text']
    history = history + [(message, response)]
    return "", history

def process_image_caption(image):
    if image is None:
        return "Por favor, forneça uma imagem."
    return models['image_caption'](image)[0]['generated_text']

def process_tts(text):
    if not text:
        return None
    return models['text_to_speech'](text)

def process_zero_shot(text, labels):
    if not text or not labels:
        return "Por favor, forneça texto e categorias."
    result = models['zero_shot'](text, labels.split(','))
    return f"Classificação: {result['labels'][0]} (Confiança: {result['scores'][0]:.2f})"

def process_ner(text):
    if not text:
        return "Por favor, forneça um texto para análise."
    results = models['ner'](text)
    entities = []
    for result in results:
        entities.append(f"{result['word']}: {result['entity']}")
    return "\n".join(entities)

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.HTML(open("index.html").read())
    
    with gr.Tabs():
        # Aba de Processamento de Áudio
        with gr.TabItem("🎤 Áudio"):
            with gr.Tabs():
                with gr.TabItem("Transcrição"):
                    audio_input = gr.Audio(type="filepath")
                    transcribe_button = gr.Button("Transcrever")
                    transcription_output = gr.Textbox(label="Resultado")
                    transcribe_button.click(process_transcription, inputs=audio_input, outputs=transcription_output)
                
                with gr.TabItem("Texto para Fala"):
                    tts_input = gr.Textbox(label="Texto para Converter")
                    tts_button = gr.Button("Converter para Áudio")
                    tts_output = gr.Audio(label="Áudio Gerado")
                    tts_button.click(process_tts, inputs=tts_input, outputs=tts_output)
        
        # Aba de Processamento de Texto
        with gr.TabItem("📝 Texto"):
            with gr.Tabs():
                with gr.TabItem("Tradução"):
                    with gr.Row():
                        text_to_translate = gr.Textbox(label="Texto")
                        translation_direction = gr.Radio(["en_pt", "pt_en"], value="en_pt", label="Direção")
                    translate_button = gr.Button("Traduzir")
                    translation_output = gr.Textbox(label="Resultado")
                    translate_button.click(process_translation, inputs=[text_to_translate, translation_direction], outputs=translation_output)
                
                with gr.TabItem("Resumo"):
                    text_sum = gr.Textbox(label="Texto", lines=5)
                    sum_button = gr.Button("Resumir")
                    sum_output = gr.Textbox(label="Resultado")
                    sum_button.click(process_summarization, inputs=text_sum, outputs=sum_output)
                
                with gr.TabItem("Perguntas e Respostas"):
                    qa_context = gr.Textbox(label="Contexto", lines=5)
                    qa_question = gr.Textbox(label="Pergunta")
                    qa_button = gr.Button("Obter Resposta")
                    qa_output = gr.Textbox(label="Resposta")
                    qa_button.click(process_qa, inputs=[qa_question, qa_context], outputs=qa_output)

        # Aba de Análise
        with gr.TabItem("📊 Análise"):
            with gr.Tabs():
                with gr.TabItem("Sentimento"):
                    text_sent = gr.Textbox(label="Texto")
                    sent_button = gr.Button("Analisar Sentimento")
                    sent_output = gr.Textbox(label="Resultado")
                    sent_button.click(process_sentiment, inputs=text_sent, outputs=sent_output)
                
                with gr.TabItem("Entidades Nomeadas"):
                    ner_input = gr.Textbox(label="Texto")
                    ner_button = gr.Button("Identificar Entidades")
                    ner_output = gr.Textbox(label="Entidades Identificadas")
                    ner_button.click(process_ner, inputs=ner_input, outputs=ner_output)
                
                with gr.TabItem("Classificação Zero-Shot"):
                    zero_text = gr.Textbox(label="Texto")
                    zero_labels = gr.Textbox(label="Categorias (separadas por vírgula)")
                    zero_button = gr.Button("Classificar")
                    zero_output = gr.Textbox(label="Resultado")
                    zero_button.click(process_zero_shot, inputs=[zero_text, zero_labels], outputs=zero_output)

        # Aba de IA Avançada
        with gr.TabItem("🤖 IA Avançada"):
            with gr.Tabs():
                with gr.TabItem("Chat"):
                    chatbot = gr.Chatbot()
                    msg = gr.Textbox(label="Mensagem")
                    clear = gr.Button("Limpar Conversa")
                    msg.submit(process_chat, inputs=[msg, chatbot], outputs=[msg, chatbot])
                    clear.click(lambda: None, None, chatbot, queue=False)
                
                with gr.TabItem("Descrição de Imagens"):
                    image_input = gr.Image()
                    caption_button = gr.Button("Gerar Descrição")
                    caption_output = gr.Textbox(label="Descrição")
                    caption_button.click(process_image_caption, inputs=image_input, outputs=caption_output)

demo.launch(share=True)