Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,105 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
def responder(message, history):
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
|
| 4 |
def responder(message, history):
|
| 5 |
+
resposta = f"Recebi a tua mensagem: '{message}'. Esta é uma resposta exemplo num chatbot em Gradio."
|
| 6 |
+
texto = ""
|
| 7 |
+
for palavra in resposta.split():
|
| 8 |
+
texto += palavra + " "
|
| 9 |
+
time.sleep(0.03)
|
| 10 |
+
yield texto
|
| 11 |
|
| 12 |
+
theme = gr.themes.Soft(
|
| 13 |
+
primary_hue="blue",
|
| 14 |
+
secondary_hue="slate",
|
| 15 |
+
neutral_hue="slate"
|
| 16 |
+
).set(
|
| 17 |
+
body_background_fill="#0f172a",
|
| 18 |
+
body_text_color="#f8fafc",
|
| 19 |
+
block_background_fill="#111827",
|
| 20 |
+
block_border_color="#1f2937",
|
| 21 |
+
button_primary_background_fill="#2563eb",
|
| 22 |
+
button_primary_background_fill_hover="#1d4ed8",
|
| 23 |
+
input_background_fill="#0b1220"
|
| 24 |
)
|
| 25 |
|
| 26 |
+
custom_css = """
|
| 27 |
+
#title-box {
|
| 28 |
+
text-align: center;
|
| 29 |
+
padding: 20px 10px 10px 10px;
|
| 30 |
+
}
|
| 31 |
+
#title-box h1 {
|
| 32 |
+
font-size: 2.2rem;
|
| 33 |
+
margin-bottom: 8px;
|
| 34 |
+
}
|
| 35 |
+
#title-box p {
|
| 36 |
+
color: #cbd5e1;
|
| 37 |
+
font-size: 1rem;
|
| 38 |
+
}
|
| 39 |
+
footer {display: none !important;}
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
with gr.Blocks(theme=theme, css=custom_css, fill_height=True) as demo:
|
| 43 |
+
gr.HTML("""
|
| 44 |
+
<div id="title-box">
|
| 45 |
+
<h1>🤖 Meu Chatbot</h1>
|
| 46 |
+
<p>Um chatbot feito em Gradio com visual melhor e estrutura mais moderna.</p>
|
| 47 |
+
</div>
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
chatbot = gr.Chatbot(
|
| 51 |
+
label="Conversa",
|
| 52 |
+
height=500,
|
| 53 |
+
bubble_full_width=False,
|
| 54 |
+
show_copy_button=True,
|
| 55 |
+
avatar_images=(None, None),
|
| 56 |
+
type="messages"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
msg = gr.Textbox(
|
| 61 |
+
placeholder="Escreve a tua mensagem aqui...",
|
| 62 |
+
show_label=False,
|
| 63 |
+
scale=8
|
| 64 |
+
)
|
| 65 |
+
send = gr.Button("Enviar", variant="primary", scale=1)
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
clear = gr.ClearButton([msg, chatbot], value="Nova conversa")
|
| 69 |
+
|
| 70 |
+
gr.Examples(
|
| 71 |
+
examples=[
|
| 72 |
+
"Olá, quem és tu?",
|
| 73 |
+
"Explica-me o que é Gradio.",
|
| 74 |
+
"Dá-me ideias para um projeto em Python.",
|
| 75 |
+
"Como posso criar um chatbot mais bonito?"
|
| 76 |
+
],
|
| 77 |
+
inputs=msg
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
def user_message(message, history):
|
| 81 |
+
history = history or []
|
| 82 |
+
history.append({"role": "user", "content": message})
|
| 83 |
+
return "", history
|
| 84 |
+
|
| 85 |
+
def bot_response(history):
|
| 86 |
+
user_msg = history[-1]["content"]
|
| 87 |
+
resposta = f"Recebi a tua mensagem: '{user_msg}'. Esta é uma resposta exemplo num chatbot em Gradio."
|
| 88 |
+
history.append({"role": "assistant", "content": ""})
|
| 89 |
+
|
| 90 |
+
texto = ""
|
| 91 |
+
for palavra in resposta.split():
|
| 92 |
+
texto += palavra + " "
|
| 93 |
+
history[-1]["content"] = texto
|
| 94 |
+
time.sleep(0.03)
|
| 95 |
+
yield history
|
| 96 |
+
|
| 97 |
+
msg.submit(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 98 |
+
bot_response, chatbot, chatbot
|
| 99 |
+
)
|
| 100 |
+
send.click(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 101 |
+
bot_response, chatbot, chatbot
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
|