File size: 2,024 Bytes
5688993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#Esboço de App de Tradução
import gradio as gr
from transformers import pipeline

# Carregar modelos de tradução
pt_en_translator = pipeline("translation", model="unicamp-dl/translation-pt-en-t5-large-v2")
en_pt_translator = pipeline("translation", model="unicamp-dl/translation-en-pt-t5-large-v2")

def traduzir_pt_en(texto):
    return pt_en_translator(texto, max_length=512)[0]["translation_text"]

def traduzir_en_pt(texto):
    return en_pt_translator(texto, max_length=512)[0]["translation_text"]

def enviar_funcionario(msg_pt, history):
    traducao = traduzir_pt_en(msg_pt)
    resposta = f"Funcionário (PT): {msg_pt}\n*(Translation: {traducao})*"
    history = history + [(msg_pt, resposta)]
    return "", history

def enviar_estudante(msg_en, history):
    traducao = traduzir_en_pt(msg_en)
    resposta = f"Student (EN): {msg_en}\n*(Tradução: {traducao})*"
    history = history + [(msg_en, resposta)]
    return "", history

with gr.Blocks() as demo:
    gr.Markdown("## 🎓 Chat PT-EN com Tradução Simultânea")
    gr.Markdown("Dois usuários podem conversar: **Funcionário (PT)** ↔ **Estudante (EN)**. Cada fala aparece com tradução automática.")

    chatbot = gr.Chatbot(label="Chat Funcionário ↔ Estudante", bubble_full_width=False)

    with gr.Row():
        with gr.Column():
            msg_func = gr.Textbox(label="Mensagem do Funcionário (PT)", placeholder="Digite em Português...")
            btn_func = gr.Button("Enviar Funcionário")
        with gr.Column():
            msg_est = gr.Textbox(label="Mensagem do Estudante (EN)", placeholder="Digite em Inglês...")
            btn_est = gr.Button("Enviar Estudante")

    clear = gr.Button("Limpar conversa")

    btn_func.click(enviar_funcionario, [msg_func, chatbot], [msg_func, chatbot])
    btn_est.click(enviar_estudante, [msg_est, chatbot], [msg_est, chatbot])
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch()