Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ from typing import List, Dict
|
|
3 |
from gradio_client import Client
|
4 |
|
5 |
def create_chat_app():
|
6 |
-
# Language configurations
|
7 |
TRANSLATIONS = {
|
8 |
"en": {
|
9 |
"title": "🤖 Chat with Llama 3.3 70B",
|
@@ -11,7 +10,7 @@ def create_chat_app():
|
|
11 |
This is a chatbot based on the Llama 3.3 70B model. To use:
|
12 |
1. Type your message in the field below
|
13 |
2. Adjust parameters as needed
|
14 |
-
3.
|
15 |
""",
|
16 |
"system_message": "You are a helpful and friendly assistant based on the Llama 3.3 70B model.",
|
17 |
"system_message_label": "System Message",
|
@@ -19,13 +18,13 @@ def create_chat_app():
|
|
19 |
"temperature_label": "Temperature",
|
20 |
"top_p_label": "Top-p (Nucleus Sampling)",
|
21 |
"message_placeholder": "Type your message here...",
|
|
|
|
|
22 |
"info_section": """
|
23 |
### ℹ️ Information
|
24 |
- Model: Llama 3.3 70B Instruct
|
25 |
- Language: English/Portuguese
|
26 |
- Hosting: Hugging Face Spaces
|
27 |
-
|
28 |
-
For best performance, adjust the parameters according to your needs.
|
29 |
""",
|
30 |
"error_message": "Sorry, an error occurred: {}\nPlease check your connection and settings.",
|
31 |
"examples": [
|
@@ -41,7 +40,7 @@ def create_chat_app():
|
|
41 |
Este é um chatbot baseado no modelo Llama 3.3 70B. Para usar:
|
42 |
1. Digite sua mensagem no campo abaixo
|
43 |
2. Ajuste os parâmetros conforme necessário
|
44 |
-
3.
|
45 |
""",
|
46 |
"system_message": "Você é um assistente amigável e prestativo que responde em português. Você é baseado no modelo Llama 3.3 70B.",
|
47 |
"system_message_label": "Mensagem do Sistema",
|
@@ -49,13 +48,13 @@ def create_chat_app():
|
|
49 |
"temperature_label": "Temperatura",
|
50 |
"top_p_label": "Top-p (Amostragem Nucleus)",
|
51 |
"message_placeholder": "Digite sua mensagem aqui...",
|
|
|
|
|
52 |
"info_section": """
|
53 |
### ℹ️ Informações
|
54 |
- Modelo: Llama 3.3 70B Instruct
|
55 |
- Idioma: Português/Inglês
|
56 |
- Hospedagem: Hugging Face Spaces
|
57 |
-
|
58 |
-
Para melhor desempenho, ajuste os parâmetros de acordo com suas necessidades.
|
59 |
""",
|
60 |
"error_message": "Desculpe, ocorreu um erro: {}\nPor favor, verifique sua conexão e configurações.",
|
61 |
"examples": [
|
@@ -76,10 +75,12 @@ def create_chat_app():
|
|
76 |
top_p: float,
|
77 |
language: str,
|
78 |
):
|
|
|
|
|
|
|
79 |
try:
|
80 |
client = Client("aifeifei798/feifei-chat")
|
81 |
|
82 |
-
# Format conversation history
|
83 |
formatted_message = f"{system_message}\n\nConversation history:\n"
|
84 |
for msg in chat_history:
|
85 |
formatted_message += f"{msg['role']}: {msg['content']}\n"
|
@@ -99,7 +100,6 @@ def create_chat_app():
|
|
99 |
api_name="/chat"
|
100 |
)
|
101 |
|
102 |
-
# Update chat history in the new format
|
103 |
chat_history.extend([
|
104 |
{"role": "user", "content": message},
|
105 |
{"role": "assistant", "content": response}
|
@@ -122,15 +122,22 @@ def create_chat_app():
|
|
122 |
chatbot = gr.Chatbot(
|
123 |
value=[],
|
124 |
height=400,
|
125 |
-
type="messages"
|
126 |
)
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
-
with gr.Accordion("Settings", open=False):
|
134 |
system_message = gr.Textbox(
|
135 |
value=TRANSLATIONS["en"]["system_message"],
|
136 |
label=TRANSLATIONS["en"]["system_message_label"]
|
@@ -167,7 +174,7 @@ def create_chat_app():
|
|
167 |
interactive=True
|
168 |
)
|
169 |
|
170 |
-
|
171 |
|
172 |
gr.Markdown(TRANSLATIONS["en"]["info_section"])
|
173 |
|
@@ -177,15 +184,20 @@ def create_chat_app():
|
|
177 |
)
|
178 |
|
179 |
# Event handlers
|
180 |
-
|
181 |
respond,
|
182 |
[message, chatbot, system_message, max_tokens, temperature, top_p, language_selector],
|
183 |
[chatbot, message]
|
184 |
)
|
185 |
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
-
# Update interface text when language changes
|
189 |
def update_language(lang):
|
190 |
trans = TRANSLATIONS[lang]
|
191 |
return (
|
@@ -194,7 +206,9 @@ def create_chat_app():
|
|
194 |
trans["system_message_label"],
|
195 |
trans["max_tokens_label"],
|
196 |
trans["temperature_label"],
|
197 |
-
trans["top_p_label"]
|
|
|
|
|
198 |
)
|
199 |
|
200 |
language_selector.change(
|
@@ -203,10 +217,12 @@ def create_chat_app():
|
|
203 |
outputs=[
|
204 |
message,
|
205 |
system_message,
|
206 |
-
system_message,
|
207 |
-
max_tokens,
|
208 |
-
temperature,
|
209 |
-
top_p
|
|
|
|
|
210 |
]
|
211 |
)
|
212 |
|
|
|
3 |
from gradio_client import Client
|
4 |
|
5 |
def create_chat_app():
|
|
|
6 |
TRANSLATIONS = {
|
7 |
"en": {
|
8 |
"title": "🤖 Chat with Llama 3.3 70B",
|
|
|
10 |
This is a chatbot based on the Llama 3.3 70B model. To use:
|
11 |
1. Type your message in the field below
|
12 |
2. Adjust parameters as needed
|
13 |
+
3. Click Send or press Enter
|
14 |
""",
|
15 |
"system_message": "You are a helpful and friendly assistant based on the Llama 3.3 70B model.",
|
16 |
"system_message_label": "System Message",
|
|
|
18 |
"temperature_label": "Temperature",
|
19 |
"top_p_label": "Top-p (Nucleus Sampling)",
|
20 |
"message_placeholder": "Type your message here...",
|
21 |
+
"send_button": "Send",
|
22 |
+
"clear_button": "Clear Chat",
|
23 |
"info_section": """
|
24 |
### ℹ️ Information
|
25 |
- Model: Llama 3.3 70B Instruct
|
26 |
- Language: English/Portuguese
|
27 |
- Hosting: Hugging Face Spaces
|
|
|
|
|
28 |
""",
|
29 |
"error_message": "Sorry, an error occurred: {}\nPlease check your connection and settings.",
|
30 |
"examples": [
|
|
|
40 |
Este é um chatbot baseado no modelo Llama 3.3 70B. Para usar:
|
41 |
1. Digite sua mensagem no campo abaixo
|
42 |
2. Ajuste os parâmetros conforme necessário
|
43 |
+
3. Clique em Enviar ou pressione Enter
|
44 |
""",
|
45 |
"system_message": "Você é um assistente amigável e prestativo que responde em português. Você é baseado no modelo Llama 3.3 70B.",
|
46 |
"system_message_label": "Mensagem do Sistema",
|
|
|
48 |
"temperature_label": "Temperatura",
|
49 |
"top_p_label": "Top-p (Amostragem Nucleus)",
|
50 |
"message_placeholder": "Digite sua mensagem aqui...",
|
51 |
+
"send_button": "Enviar",
|
52 |
+
"clear_button": "Limpar Chat",
|
53 |
"info_section": """
|
54 |
### ℹ️ Informações
|
55 |
- Modelo: Llama 3.3 70B Instruct
|
56 |
- Idioma: Português/Inglês
|
57 |
- Hospedagem: Hugging Face Spaces
|
|
|
|
|
58 |
""",
|
59 |
"error_message": "Desculpe, ocorreu um erro: {}\nPor favor, verifique sua conexão e configurações.",
|
60 |
"examples": [
|
|
|
75 |
top_p: float,
|
76 |
language: str,
|
77 |
):
|
78 |
+
if not message.strip(): # Não processa mensagens vazias
|
79 |
+
return chat_history, ""
|
80 |
+
|
81 |
try:
|
82 |
client = Client("aifeifei798/feifei-chat")
|
83 |
|
|
|
84 |
formatted_message = f"{system_message}\n\nConversation history:\n"
|
85 |
for msg in chat_history:
|
86 |
formatted_message += f"{msg['role']}: {msg['content']}\n"
|
|
|
100 |
api_name="/chat"
|
101 |
)
|
102 |
|
|
|
103 |
chat_history.extend([
|
104 |
{"role": "user", "content": message},
|
105 |
{"role": "assistant", "content": response}
|
|
|
122 |
chatbot = gr.Chatbot(
|
123 |
value=[],
|
124 |
height=400,
|
125 |
+
type="messages"
|
126 |
)
|
127 |
|
128 |
+
with gr.Row():
|
129 |
+
message = gr.Textbox(
|
130 |
+
placeholder=TRANSLATIONS["en"]["message_placeholder"],
|
131 |
+
lines=3,
|
132 |
+
scale=9 # Ocupa 90% do espaço
|
133 |
+
)
|
134 |
+
send_btn = gr.Button(
|
135 |
+
TRANSLATIONS["en"]["send_button"],
|
136 |
+
variant="primary",
|
137 |
+
scale=1 # Ocupa 10% do espaço
|
138 |
+
)
|
139 |
|
140 |
+
with gr.Accordion("Settings/Configurações", open=False):
|
141 |
system_message = gr.Textbox(
|
142 |
value=TRANSLATIONS["en"]["system_message"],
|
143 |
label=TRANSLATIONS["en"]["system_message_label"]
|
|
|
174 |
interactive=True
|
175 |
)
|
176 |
|
177 |
+
clear_btn = gr.Button(TRANSLATIONS["en"]["clear_button"])
|
178 |
|
179 |
gr.Markdown(TRANSLATIONS["en"]["info_section"])
|
180 |
|
|
|
184 |
)
|
185 |
|
186 |
# Event handlers
|
187 |
+
send_btn.click(
|
188 |
respond,
|
189 |
[message, chatbot, system_message, max_tokens, temperature, top_p, language_selector],
|
190 |
[chatbot, message]
|
191 |
)
|
192 |
|
193 |
+
message.submit( # Permite também enviar com Enter
|
194 |
+
respond,
|
195 |
+
[message, chatbot, system_message, max_tokens, temperature, top_p, language_selector],
|
196 |
+
[chatbot, message]
|
197 |
+
)
|
198 |
+
|
199 |
+
clear_btn.click(lambda: ([], ""), outputs=[chatbot, message])
|
200 |
|
|
|
201 |
def update_language(lang):
|
202 |
trans = TRANSLATIONS[lang]
|
203 |
return (
|
|
|
206 |
trans["system_message_label"],
|
207 |
trans["max_tokens_label"],
|
208 |
trans["temperature_label"],
|
209 |
+
trans["top_p_label"],
|
210 |
+
trans["send_button"],
|
211 |
+
trans["clear_button"]
|
212 |
)
|
213 |
|
214 |
language_selector.change(
|
|
|
217 |
outputs=[
|
218 |
message,
|
219 |
system_message,
|
220 |
+
system_message,
|
221 |
+
max_tokens,
|
222 |
+
temperature,
|
223 |
+
top_p,
|
224 |
+
send_btn,
|
225 |
+
clear_btn
|
226 |
]
|
227 |
)
|
228 |
|