Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,34 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
-
from transformers import pipeline
|
4 |
import torch
|
5 |
import warnings
|
6 |
warnings.filterwarnings('ignore')
|
7 |
|
8 |
-
# Configuração de dispositivo
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
try:
|
12 |
-
# Modelos para diferentes tarefas
|
13 |
models = {
|
14 |
-
'transcription': pipeline("automatic-speech-recognition",
|
15 |
-
|
16 |
-
|
17 |
-
'summarization': pipeline("summarization",
|
18 |
-
|
19 |
-
|
20 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
|
|
|
|
|
|
|
|
|
|
22 |
except Exception as e:
|
23 |
print(f"Erro ao carregar modelos: {str(e)}")
|
24 |
|
@@ -30,43 +40,30 @@ def safe_process(func):
|
|
30 |
return f"Erro ao processar: {str(e)}"
|
31 |
return wrapper
|
32 |
|
33 |
-
@safe_process
|
34 |
-
def transcribe(audio):
|
35 |
-
if not audio:
|
36 |
-
return "Por favor, forneça um arquivo de áudio."
|
37 |
-
return models['transcription'](audio)["text"]
|
38 |
-
|
39 |
@safe_process
|
40 |
def translate(text, direction):
|
41 |
if not text:
|
42 |
return "Por favor, insira um texto para tradução."
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
def analyze_sentiment(text):
|
54 |
-
if not text:
|
55 |
-
return "Por favor, insira um texto para análise."
|
56 |
-
return models['sentiment'](text)[0]['label']
|
57 |
-
|
58 |
-
@safe_process
|
59 |
-
def answer_question(question, context):
|
60 |
-
if not question or not context:
|
61 |
-
return "Por favor, forneça tanto a pergunta quanto o contexto."
|
62 |
-
return models['question_answering'](question=question, context=context)['answer']
|
63 |
|
64 |
@safe_process
|
65 |
def chat_response(message, history):
|
66 |
if not message:
|
67 |
-
return
|
68 |
response = models['chat'](message, max_length=100, do_sample=True, temperature=0.7)
|
69 |
-
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
72 |
with gr.Tab("Início"):
|
@@ -94,35 +91,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
94 |
outputs=translation_output
|
95 |
)
|
96 |
|
97 |
-
with gr.Tab("Resumo"):
|
98 |
-
text_to_summarize = gr.Textbox(label="Texto para Resumir", lines=5)
|
99 |
-
summarize_button = gr.Button("Resumir")
|
100 |
-
summary_output = gr.Textbox(label="Resumo", lines=3)
|
101 |
-
summarize_button.click(summarize, inputs=text_to_summarize, outputs=summary_output)
|
102 |
-
|
103 |
-
with gr.Tab("Análise de Sentimento"):
|
104 |
-
sentiment_text = gr.Textbox(label="Texto para Análise", lines=3)
|
105 |
-
sentiment_button = gr.Button("Analisar")
|
106 |
-
sentiment_output = gr.Textbox(label="Sentimento")
|
107 |
-
sentiment_button.click(analyze_sentiment, inputs=sentiment_text, outputs=sentiment_output)
|
108 |
-
|
109 |
-
with gr.Tab("Perguntas e Respostas"):
|
110 |
-
question_input = gr.Textbox(label="Pergunta")
|
111 |
-
context_input = gr.Textbox(label="Contexto", lines=5)
|
112 |
-
qa_button = gr.Button("Responder")
|
113 |
-
qa_output = gr.Textbox(label="Resposta", lines=2)
|
114 |
-
qa_button.click(
|
115 |
-
answer_question,
|
116 |
-
inputs=[question_input, context_input],
|
117 |
-
outputs=qa_output
|
118 |
-
)
|
119 |
-
|
120 |
with gr.Tab("Chat"):
|
121 |
chatbot = gr.Chatbot()
|
122 |
msg = gr.Textbox(label="Mensagem")
|
123 |
clear = gr.Button("Limpar")
|
124 |
|
125 |
-
msg.submit(chat_response, [msg, chatbot], [msg, chatbot])
|
126 |
clear.click(lambda: None, None, chatbot, queue=False)
|
127 |
|
128 |
if __name__ == "__main__":
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqGeneration
|
3 |
import torch
|
4 |
import warnings
|
5 |
warnings.filterwarnings('ignore')
|
6 |
|
|
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
try:
|
|
|
10 |
models = {
|
11 |
+
'transcription': pipeline("automatic-speech-recognition",
|
12 |
+
model="openai/whisper-small",
|
13 |
+
device=device),
|
14 |
+
'summarization': pipeline("summarization",
|
15 |
+
model="facebook/bart-large-cnn",
|
16 |
+
device=device),
|
17 |
+
'sentiment': pipeline("sentiment-analysis",
|
18 |
+
model="nlptown/bert-base-multilingual-uncased-sentiment",
|
19 |
+
device=device),
|
20 |
+
'question_answering': pipeline("question-answering",
|
21 |
+
model="deepset/roberta-base-squad2",
|
22 |
+
device=device),
|
23 |
+
'chat': pipeline("text-generation",
|
24 |
+
model="facebook/opt-125m",
|
25 |
+
device=device)
|
26 |
}
|
27 |
+
|
28 |
+
# Carregando modelos de tradução
|
29 |
+
tokenizer_en_pt = AutoTokenizer.from_pretrained("unicamp-dl/translation-en-pt-t5")
|
30 |
+
model_en_pt = AutoModelForSeq2SeqGeneration.from_pretrained("unicamp-dl/translation-en-pt-t5")
|
31 |
+
|
32 |
except Exception as e:
|
33 |
print(f"Erro ao carregar modelos: {str(e)}")
|
34 |
|
|
|
40 |
return f"Erro ao processar: {str(e)}"
|
41 |
return wrapper
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
@safe_process
|
44 |
def translate(text, direction):
|
45 |
if not text:
|
46 |
return "Por favor, insira um texto para tradução."
|
47 |
+
|
48 |
+
input_text = text
|
49 |
+
if direction == "pt_en":
|
50 |
+
input_text = f"translate Portuguese to English: {text}"
|
51 |
+
else:
|
52 |
+
input_text = f"translate English to Portuguese: {text}"
|
53 |
+
|
54 |
+
inputs = tokenizer_en_pt(input_text, return_tensors="pt", max_length=512, truncation=True)
|
55 |
+
outputs = model_en_pt.generate(**inputs)
|
56 |
+
return tokenizer_en_pt.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
@safe_process
|
59 |
def chat_response(message, history):
|
60 |
if not message:
|
61 |
+
return [], history
|
62 |
response = models['chat'](message, max_length=100, do_sample=True, temperature=0.7)
|
63 |
+
history.append((message, response[0]['generated_text']))
|
64 |
+
return "", history
|
65 |
+
|
66 |
+
# [Resto das funções permanecem iguais]
|
67 |
|
68 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
69 |
with gr.Tab("Início"):
|
|
|
91 |
outputs=translation_output
|
92 |
)
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
with gr.Tab("Chat"):
|
95 |
chatbot = gr.Chatbot()
|
96 |
msg = gr.Textbox(label="Mensagem")
|
97 |
clear = gr.Button("Limpar")
|
98 |
|
99 |
+
msg.submit(chat_response, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
100 |
clear.click(lambda: None, None, chatbot, queue=False)
|
101 |
|
102 |
if __name__ == "__main__":
|