File size: 2,511 Bytes
3d97b57
6881041
3d97b57
c9904d1
 
 
 
 
 
 
 
 
 
6881041
 
 
c9904d1
6881041
c9904d1
3d97b57
6881041
 
 
 
 
 
 
 
 
 
 
 
3d97b57
 
6881041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9904d1
6881041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b98cef1
6881041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9904d1
 
 
 
 
 
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
import gradio as gr
import time

def user_message(message, history):
    history = history or []
    history.append({"role": "user", "content": message})
    return "", history

def bot_response(history):
    user_msg = history[-1]["content"]
    resposta = f"Recebi a tua mensagem: '{user_msg}'. Esta é uma resposta exemplo num chatbot em Gradio."
    history.append({"role": "assistant", "content": ""})

    texto = ""
    for palavra in resposta.split():
        texto += palavra + " "
        history[-1]["content"] = texto
        time.sleep(0.03)
        yield history

theme = gr.themes.Soft(
    primary_hue="blue",
    secondary_hue="slate",
    neutral_hue="slate"
).set(
    body_background_fill="#0f172a",
    body_text_color="#f8fafc",
    block_background_fill="#111827",
    block_border_color="#1f2937",
    button_primary_background_fill="#2563eb",
    button_primary_background_fill_hover="#1d4ed8",
    input_background_fill="#0b1220"
)

custom_css = """
#title-box {
    text-align: center;
    padding: 20px 10px 10px 10px;
}
#title-box h1 {
    font-size: 2.2rem;
    margin-bottom: 8px;
}
#title-box p {
    color: #cbd5e1;
    font-size: 1rem;
}
footer {display: none !important;}
"""

with gr.Blocks(fill_height=True) as demo:
    gr.HTML("""
        <div id="title-box">
            <h1>🤖 Meu Chatbot</h1>
            <p>Um chatbot feito em Gradio com visual melhor e estrutura mais moderna.</p>
        </div>
    """)

    chatbot = gr.Chatbot(
        label="Conversa",
        height=500,
        type="messages"
    )

    with gr.Row():
        msg = gr.Textbox(
            placeholder="Escreve a tua mensagem aqui...",
            show_label=False,
            scale=8
        )
        send = gr.Button("Enviar", variant="primary", scale=1)

    clear = gr.ClearButton([msg, chatbot], value="Nova conversa")

    gr.Examples(
        examples=[
            "Olá, quem és tu?",
            "Explica-me o que é Gradio.",
            "Dá-me ideias para um projeto em Python.",
            "Como posso criar um chatbot mais bonito?"
        ],
        inputs=msg
    )

    msg.submit(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot_response, chatbot, chatbot
    )
    send.click(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot_response, chatbot, chatbot
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        theme=theme,
        css=custom_css
    )