Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,166 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import warnings
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
warnings.filterwarnings('ignore')
|
8 |
+
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
|
11 |
+
# Dicionário expandido de modelos
|
12 |
+
models = {
|
13 |
+
'transcription': pipeline("automatic-speech-recognition", model="openai/whisper-small", device=device),
|
14 |
+
'translation': pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt", device=device),
|
15 |
+
'summarization': pipeline("summarization", model="facebook/bart-large-cnn", device=device),
|
16 |
+
'sentiment': pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment", device=device),
|
17 |
+
'question_answering': pipeline("question-answering", model="deepset/roberta-base-squad2", device=device),
|
18 |
+
'chat': pipeline("text-generation", model="facebook/opt-125m", device=device),
|
19 |
+
'image_caption': pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device=device),
|
20 |
+
'text_to_speech': pipeline("text-to-audio", model="facebook/mms-tts-eng", device=device),
|
21 |
+
'zero_shot': pipeline("zero-shot-classification", model="facebook/bart-large-mnli", device=device),
|
22 |
+
'ner': pipeline("token-classification", model="dslim/bert-base-NER", device=device)
|
23 |
+
}
|
24 |
+
|
25 |
+
def process_transcription(audio):
|
26 |
+
if not audio:
|
27 |
+
return "Por favor, forneça um arquivo de áudio."
|
28 |
+
return models['transcription'](audio)["text"]
|
29 |
+
|
30 |
+
def process_translation(text, direction):
|
31 |
+
if not text:
|
32 |
+
return "Por favor, forneça um texto para tradução."
|
33 |
+
return models['translation'](text,
|
34 |
+
src_lang="pt" if direction=="pt_en" else "en",
|
35 |
+
tgt_lang="en" if direction=="pt_en" else "pt")[0]['translation_text']
|
36 |
+
|
37 |
+
def process_summarization(text):
|
38 |
+
if not text:
|
39 |
+
return "Por favor, forneça um texto para resumir."
|
40 |
+
return models['summarization'](text, max_length=130, min_length=30)[0]['summary_text']
|
41 |
+
|
42 |
+
def process_sentiment(text):
|
43 |
+
if not text:
|
44 |
+
return "Por favor, forneça um texto para análise."
|
45 |
+
result = models['sentiment'](text)[0]
|
46 |
+
return f"Sentimento: {result['label']} (Score: {result['score']:.2f})"
|
47 |
+
|
48 |
+
def process_qa(question, context):
|
49 |
+
if not question or not context:
|
50 |
+
return "Por favor, forneça tanto a pergunta quanto o contexto."
|
51 |
+
return models['question_answering'](question=question, context=context)['answer']
|
52 |
+
|
53 |
+
def process_chat(message, history):
|
54 |
+
if not message:
|
55 |
+
return "", history
|
56 |
+
response = models['chat'](message, max_length=100, do_sample=True)[0]['generated_text']
|
57 |
+
history = history + [(message, response)]
|
58 |
+
return "", history
|
59 |
+
|
60 |
+
def process_image_caption(image):
|
61 |
+
if image is None:
|
62 |
+
return "Por favor, forneça uma imagem."
|
63 |
+
return models['image_caption'](image)[0]['generated_text']
|
64 |
+
|
65 |
+
def process_tts(text):
|
66 |
+
if not text:
|
67 |
+
return None
|
68 |
+
return models['text_to_speech'](text)
|
69 |
+
|
70 |
+
def process_zero_shot(text, labels):
|
71 |
+
if not text or not labels:
|
72 |
+
return "Por favor, forneça texto e categorias."
|
73 |
+
result = models['zero_shot'](text, labels.split(','))
|
74 |
+
return f"Classificação: {result['labels'][0]} (Confiança: {result['scores'][0]:.2f})"
|
75 |
+
|
76 |
+
def process_ner(text):
|
77 |
+
if not text:
|
78 |
+
return "Por favor, forneça um texto para análise."
|
79 |
+
results = models['ner'](text)
|
80 |
+
entities = []
|
81 |
+
for result in results:
|
82 |
+
entities.append(f"{result['word']}: {result['entity']}")
|
83 |
+
return "\n".join(entities)
|
84 |
+
|
85 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
86 |
+
gr.HTML(open("index.html").read())
|
87 |
+
|
88 |
+
with gr.Tabs():
|
89 |
+
# Aba de Processamento de Áudio
|
90 |
+
with gr.TabItem("🎤 Áudio"):
|
91 |
+
with gr.Tabs():
|
92 |
+
with gr.TabItem("Transcrição"):
|
93 |
+
audio_input = gr.Audio(type="filepath")
|
94 |
+
transcribe_button = gr.Button("Transcrever")
|
95 |
+
transcription_output = gr.Textbox(label="Resultado")
|
96 |
+
transcribe_button.click(process_transcription, inputs=audio_input, outputs=transcription_output)
|
97 |
+
|
98 |
+
with gr.TabItem("Texto para Fala"):
|
99 |
+
tts_input = gr.Textbox(label="Texto para Converter")
|
100 |
+
tts_button = gr.Button("Converter para Áudio")
|
101 |
+
tts_output = gr.Audio(label="Áudio Gerado")
|
102 |
+
tts_button.click(process_tts, inputs=tts_input, outputs=tts_output)
|
103 |
+
|
104 |
+
# Aba de Processamento de Texto
|
105 |
+
with gr.TabItem("📝 Texto"):
|
106 |
+
with gr.Tabs():
|
107 |
+
with gr.TabItem("Tradução"):
|
108 |
+
with gr.Row():
|
109 |
+
text_to_translate = gr.Textbox(label="Texto")
|
110 |
+
translation_direction = gr.Radio(["en_pt", "pt_en"], value="en_pt", label="Direção")
|
111 |
+
translate_button = gr.Button("Traduzir")
|
112 |
+
translation_output = gr.Textbox(label="Resultado")
|
113 |
+
translate_button.click(process_translation, inputs=[text_to_translate, translation_direction], outputs=translation_output)
|
114 |
+
|
115 |
+
with gr.TabItem("Resumo"):
|
116 |
+
text_sum = gr.Textbox(label="Texto", lines=5)
|
117 |
+
sum_button = gr.Button("Resumir")
|
118 |
+
sum_output = gr.Textbox(label="Resultado")
|
119 |
+
sum_button.click(process_summarization, inputs=text_sum, outputs=sum_output)
|
120 |
+
|
121 |
+
with gr.TabItem("Perguntas e Respostas"):
|
122 |
+
qa_context = gr.Textbox(label="Contexto", lines=5)
|
123 |
+
qa_question = gr.Textbox(label="Pergunta")
|
124 |
+
qa_button = gr.Button("Obter Resposta")
|
125 |
+
qa_output = gr.Textbox(label="Resposta")
|
126 |
+
qa_button.click(process_qa, inputs=[qa_question, qa_context], outputs=qa_output)
|
127 |
+
|
128 |
+
# Aba de Análise
|
129 |
+
with gr.TabItem("📊 Análise"):
|
130 |
+
with gr.Tabs():
|
131 |
+
with gr.TabItem("Sentimento"):
|
132 |
+
text_sent = gr.Textbox(label="Texto")
|
133 |
+
sent_button = gr.Button("Analisar Sentimento")
|
134 |
+
sent_output = gr.Textbox(label="Resultado")
|
135 |
+
sent_button.click(process_sentiment, inputs=text_sent, outputs=sent_output)
|
136 |
+
|
137 |
+
with gr.TabItem("Entidades Nomeadas"):
|
138 |
+
ner_input = gr.Textbox(label="Texto")
|
139 |
+
ner_button = gr.Button("Identificar Entidades")
|
140 |
+
ner_output = gr.Textbox(label="Entidades Identificadas")
|
141 |
+
ner_button.click(process_ner, inputs=ner_input, outputs=ner_output)
|
142 |
+
|
143 |
+
with gr.TabItem("Classificação Zero-Shot"):
|
144 |
+
zero_text = gr.Textbox(label="Texto")
|
145 |
+
zero_labels = gr.Textbox(label="Categorias (separadas por vírgula)")
|
146 |
+
zero_button = gr.Button("Classificar")
|
147 |
+
zero_output = gr.Textbox(label="Resultado")
|
148 |
+
zero_button.click(process_zero_shot, inputs=[zero_text, zero_labels], outputs=zero_output)
|
149 |
+
|
150 |
+
# Aba de IA Avançada
|
151 |
+
with gr.TabItem("🤖 IA Avançada"):
|
152 |
+
with gr.Tabs():
|
153 |
+
with gr.TabItem("Chat"):
|
154 |
+
chatbot = gr.Chatbot()
|
155 |
+
msg = gr.Textbox(label="Mensagem")
|
156 |
+
clear = gr.Button("Limpar Conversa")
|
157 |
+
msg.submit(process_chat, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
158 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
159 |
+
|
160 |
+
with gr.TabItem("Descrição de Imagens"):
|
161 |
+
image_input = gr.Image()
|
162 |
+
caption_button = gr.Button("Gerar Descrição")
|
163 |
+
caption_output = gr.Textbox(label="Descrição")
|
164 |
+
caption_button.click(process_image_caption, inputs=image_input, outputs=caption_output)
|
165 |
+
|
166 |
+
demo.launch(share=True)
|