CarlosUFPE commited on
Commit
aef99f3
·
verified ·
1 Parent(s): 3b37f6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -49
app.py CHANGED
@@ -1,49 +1,49 @@
1
- #Esboço de App de Tradução
2
- import gradio as gr
3
- from transformers import pipeline
4
-
5
- # Carregar modelos de tradução
6
- pt_en_translator = pipeline("translation", model="unicamp-dl/translation-pt-en-t5-large-v2")
7
- en_pt_translator = pipeline("translation", model="unicamp-dl/translation-en-pt-t5-large-v2")
8
-
9
- def traduzir_pt_en(texto):
10
- return pt_en_translator(texto, max_length=512)[0]["translation_text"]
11
-
12
- def traduzir_en_pt(texto):
13
- return en_pt_translator(texto, max_length=512)[0]["translation_text"]
14
-
15
- def enviar_funcionario(msg_pt, history):
16
- traducao = traduzir_pt_en(msg_pt)
17
- resposta = f"Funcionário (PT): {msg_pt}\n*(Translation: {traducao})*"
18
- history = history + [(msg_pt, resposta)]
19
- return "", history
20
-
21
- def enviar_estudante(msg_en, history):
22
- traducao = traduzir_en_pt(msg_en)
23
- resposta = f"Student (EN): {msg_en}\n*(Tradução: {traducao})*"
24
- history = history + [(msg_en, resposta)]
25
- return "", history
26
-
27
- with gr.Blocks() as demo:
28
- gr.Markdown("## 🎓 Chat PT-EN com Tradução Simultânea")
29
- gr.Markdown("Dois usuários podem conversar: **Funcionário (PT)** ↔ **Estudante (EN)**. Cada fala aparece com tradução automática.")
30
-
31
- chatbot = gr.Chatbot(label="Chat Funcionário ↔ Estudante", bubble_full_width=False)
32
-
33
- with gr.Row():
34
- with gr.Column():
35
- msg_func = gr.Textbox(label="Mensagem do Funcionário (PT)", placeholder="Digite em Português...")
36
- btn_func = gr.Button("Enviar Funcionário")
37
- with gr.Column():
38
- msg_est = gr.Textbox(label="Mensagem do Estudante (EN)", placeholder="Digite em Inglês...")
39
- btn_est = gr.Button("Enviar Estudante")
40
-
41
- clear = gr.Button("Limpar conversa")
42
-
43
- btn_func.click(enviar_funcionario, [msg_func, chatbot], [msg_func, chatbot])
44
- btn_est.click(enviar_estudante, [msg_est, chatbot], [msg_est, chatbot])
45
- clear.click(lambda: None, None, chatbot, queue=False)
46
-
47
- if __name__ == "__main__":
48
- demo.launch()
49
-
 
1
+ #Esboço de App de Tradução
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # Carregar modelos de tradução
6
+ pt_en_translator = pipeline("translation", model="unicamp-dl/translation-pt-en-t5")
7
+ en_pt_translator = pipeline("translation", model="unicamp-dl/translation-en-pt-t5")
8
+
9
+ def traduzir_pt_en(texto):
10
+ return pt_en_translator(texto, max_length=512)[0]["translation_text"]
11
+
12
+ def traduzir_en_pt(texto):
13
+ return en_pt_translator(texto, max_length=512)[0]["translation_text"]
14
+
15
+ def enviar_funcionario(msg_pt, history):
16
+ traducao = traduzir_pt_en(msg_pt)
17
+ resposta = f"Funcionário (PT): {msg_pt}\n*(Translation: {traducao})*"
18
+ history = history + [(msg_pt, resposta)]
19
+ return "", history
20
+
21
+ def enviar_estudante(msg_en, history):
22
+ traducao = traduzir_en_pt(msg_en)
23
+ resposta = f"Student (EN): {msg_en}\n*(Tradução: {traducao})*"
24
+ history = history + [(msg_en, resposta)]
25
+ return "", history
26
+
27
+ with gr.Blocks() as demo:
28
+ gr.Markdown("## 🎓 Chat PT-EN com Tradução Simultânea")
29
+ gr.Markdown("Dois usuários podem conversar: **Funcionário (PT)** ↔ **Estudante (EN)**. Cada fala aparece com tradução automática.")
30
+
31
+ chatbot = gr.Chatbot(label="Chat Funcionário ↔ Estudante", bubble_full_width=False)
32
+
33
+ with gr.Row():
34
+ with gr.Column():
35
+ msg_func = gr.Textbox(label="Mensagem do Funcionário (PT)", placeholder="Digite em Português...")
36
+ btn_func = gr.Button("Enviar Funcionário")
37
+ with gr.Column():
38
+ msg_est = gr.Textbox(label="Mensagem do Estudante (EN)", placeholder="Digite em Inglês...")
39
+ btn_est = gr.Button("Enviar Estudante")
40
+
41
+ clear = gr.Button("Limpar conversa")
42
+
43
+ btn_func.click(enviar_funcionario, [msg_func, chatbot], [msg_func, chatbot])
44
+ btn_est.click(enviar_estudante, [msg_est, chatbot], [msg_est, chatbot])
45
+ clear.click(lambda: None, None, chatbot, queue=False)
46
+
47
+ if __name__ == "__main__":
48
+ demo.launch()
49
+