Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,74 @@ from typing import List, Tuple
|
|
3 |
from gradio_client import Client
|
4 |
|
5 |
def create_chat_app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def respond(
|
7 |
message: str,
|
8 |
history: List[Tuple[str, str]],
|
@@ -10,15 +78,14 @@ def create_chat_app():
|
|
10 |
max_tokens: int,
|
11 |
temperature: float,
|
12 |
top_p: float,
|
|
|
13 |
) -> str:
|
14 |
"""
|
15 |
Process user message and generate a response using the Llama 3.3 70B model.
|
16 |
"""
|
17 |
try:
|
18 |
-
# Initialize client for the newer API
|
19 |
client = Client("aifeifei798/feifei-chat")
|
20 |
|
21 |
-
# Format the conversation history and current message
|
22 |
formatted_message = f"{system_message}\n\nConversation history:\n"
|
23 |
for user, assistant in history:
|
24 |
if user:
|
@@ -28,13 +95,11 @@ def create_chat_app():
|
|
28 |
|
29 |
formatted_message += f"User: {message}"
|
30 |
|
31 |
-
# Prepare the message payload
|
32 |
message_payload = {
|
33 |
"text": formatted_message,
|
34 |
"files": []
|
35 |
}
|
36 |
|
37 |
-
# Get response from the model with the specified parameters
|
38 |
response = client.predict(
|
39 |
message=message_payload,
|
40 |
feifei_select=True,
|
@@ -46,68 +111,106 @@ def create_chat_app():
|
|
46 |
return response
|
47 |
|
48 |
except Exception as e:
|
49 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
# Interface configuration
|
52 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
""")
|
61 |
|
62 |
chatbot = gr.ChatInterface(
|
63 |
respond,
|
64 |
additional_inputs=[
|
65 |
gr.Textbox(
|
66 |
-
|
67 |
-
|
68 |
),
|
69 |
gr.Slider(
|
70 |
minimum=1,
|
71 |
maximum=4096,
|
72 |
value=2048,
|
73 |
step=1,
|
74 |
-
label="
|
75 |
),
|
76 |
gr.Slider(
|
77 |
minimum=0.1,
|
78 |
maximum=2.0,
|
79 |
value=0.7,
|
80 |
step=0.1,
|
81 |
-
label="
|
82 |
),
|
83 |
gr.Slider(
|
84 |
minimum=0.1,
|
85 |
maximum=1.0,
|
86 |
value=0.95,
|
87 |
step=0.05,
|
88 |
-
label="Top-p
|
89 |
),
|
|
|
90 |
],
|
91 |
-
|
92 |
-
description="Um chatbot interativo usando o modelo Llama 3.3 70B Instruct.",
|
93 |
-
examples=[
|
94 |
-
["Olá! Como você está?"],
|
95 |
-
["Pode me explicar o que é inteligência artificial?"],
|
96 |
-
["Qual é a capital do Brasil?"],
|
97 |
-
["Me ajude a escrever um código em Python para calcular fibonacci."]
|
98 |
-
]
|
99 |
)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
return demo
|
112 |
|
113 |
if __name__ == "__main__":
|
|
|
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",
|
10 |
+
"description": """
|
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. Press Enter to send
|
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",
|
18 |
+
"max_tokens_label": "Maximum Tokens",
|
19 |
+
"temperature_label": "Temperature",
|
20 |
+
"top_p_label": "Top-p (Nucleus Sampling)",
|
21 |
+
"chat_title": "Chat with Llama 3.3 70B",
|
22 |
+
"chat_description": "An interactive chatbot using the Llama 3.3 70B Instruct model.",
|
23 |
+
"info_section": """
|
24 |
+
### ℹ️ Information
|
25 |
+
- Model: Llama 3.3 70B Instruct
|
26 |
+
- Language: English/Portuguese
|
27 |
+
- Hosting: Hugging Face Spaces
|
28 |
+
|
29 |
+
For best performance, adjust the parameters according to your needs.
|
30 |
+
This model has advanced natural language processing capabilities.
|
31 |
+
""",
|
32 |
+
"error_message": "Sorry, an error occurred: {}\nPlease check your connection and settings.",
|
33 |
+
"examples": [
|
34 |
+
["Hello! How are you?"],
|
35 |
+
["Can you explain what artificial intelligence is?"],
|
36 |
+
["What is the capital of Brazil?"],
|
37 |
+
["Help me write a Python code to calculate Fibonacci."]
|
38 |
+
]
|
39 |
+
},
|
40 |
+
"pt": {
|
41 |
+
"title": "🤖 Chat com Llama 3.3 70B em Português",
|
42 |
+
"description": """
|
43 |
+
Este é um chatbot baseado no modelo Llama 3.3 70B. Para usar:
|
44 |
+
1. Digite sua mensagem no campo abaixo
|
45 |
+
2. Ajuste os parâmetros conforme necessário
|
46 |
+
3. Pressione Enter para enviar
|
47 |
+
""",
|
48 |
+
"system_message": "Você é um assistente amigável e prestativo que responde em português. Você é baseado no modelo Llama 3.3 70B.",
|
49 |
+
"system_message_label": "Mensagem do Sistema",
|
50 |
+
"max_tokens_label": "Máximo de Tokens",
|
51 |
+
"temperature_label": "Temperatura",
|
52 |
+
"top_p_label": "Top-p (Amostragem Nucleus)",
|
53 |
+
"chat_title": "Chat com Llama 3.3 70B",
|
54 |
+
"chat_description": "Um chatbot interativo usando o modelo Llama 3.3 70B Instruct.",
|
55 |
+
"info_section": """
|
56 |
+
### ℹ️ Informações
|
57 |
+
- Modelo: Llama 3.3 70B Instruct
|
58 |
+
- Idioma: Português/Inglês
|
59 |
+
- Hospedagem: Hugging Face Spaces
|
60 |
+
|
61 |
+
Para melhor desempenho, ajuste os parâmetros de acordo com suas necessidades.
|
62 |
+
Este modelo possui capacidades avançadas de processamento de linguagem natural.
|
63 |
+
""",
|
64 |
+
"error_message": "Desculpe, ocorreu um erro: {}\nPor favor, verifique sua conexão e configurações.",
|
65 |
+
"examples": [
|
66 |
+
["Olá! Como você está?"],
|
67 |
+
["Pode me explicar o que é inteligência artificial?"],
|
68 |
+
["Qual é a capital do Brasil?"],
|
69 |
+
["Me ajude a escrever um código em Python para calcular fibonacci."]
|
70 |
+
]
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
def respond(
|
75 |
message: str,
|
76 |
history: List[Tuple[str, str]],
|
|
|
78 |
max_tokens: int,
|
79 |
temperature: float,
|
80 |
top_p: float,
|
81 |
+
language: str,
|
82 |
) -> str:
|
83 |
"""
|
84 |
Process user message and generate a response using the Llama 3.3 70B model.
|
85 |
"""
|
86 |
try:
|
|
|
87 |
client = Client("aifeifei798/feifei-chat")
|
88 |
|
|
|
89 |
formatted_message = f"{system_message}\n\nConversation history:\n"
|
90 |
for user, assistant in history:
|
91 |
if user:
|
|
|
95 |
|
96 |
formatted_message += f"User: {message}"
|
97 |
|
|
|
98 |
message_payload = {
|
99 |
"text": formatted_message,
|
100 |
"files": []
|
101 |
}
|
102 |
|
|
|
103 |
response = client.predict(
|
104 |
message=message_payload,
|
105 |
feifei_select=True,
|
|
|
111 |
return response
|
112 |
|
113 |
except Exception as e:
|
114 |
+
return TRANSLATIONS[language]["error_message"].format(str(e))
|
115 |
+
|
116 |
+
def update_interface(language: str):
|
117 |
+
"""
|
118 |
+
Update the interface language based on user selection
|
119 |
+
"""
|
120 |
+
trans = TRANSLATIONS[language]
|
121 |
+
return (
|
122 |
+
trans["title"],
|
123 |
+
trans["description"],
|
124 |
+
trans["system_message"],
|
125 |
+
trans["system_message_label"],
|
126 |
+
trans["max_tokens_label"],
|
127 |
+
trans["temperature_label"],
|
128 |
+
trans["top_p_label"],
|
129 |
+
trans["chat_title"],
|
130 |
+
trans["chat_description"],
|
131 |
+
trans["info_section"],
|
132 |
+
trans["examples"]
|
133 |
+
)
|
134 |
|
|
|
135 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
136 |
+
# Language selector
|
137 |
+
language = gr.Radio(
|
138 |
+
choices=["en", "pt"],
|
139 |
+
value="en",
|
140 |
+
label="Language/Idioma",
|
141 |
+
interactive=True
|
142 |
+
)
|
143 |
|
144 |
+
# Dynamic content containers
|
145 |
+
title_md = gr.Markdown()
|
146 |
+
description_md = gr.Markdown()
|
147 |
+
info_md = gr.Markdown()
|
|
|
148 |
|
149 |
chatbot = gr.ChatInterface(
|
150 |
respond,
|
151 |
additional_inputs=[
|
152 |
gr.Textbox(
|
153 |
+
label="System Message",
|
154 |
+
value=TRANSLATIONS["en"]["system_message"]
|
155 |
),
|
156 |
gr.Slider(
|
157 |
minimum=1,
|
158 |
maximum=4096,
|
159 |
value=2048,
|
160 |
step=1,
|
161 |
+
label="Maximum Tokens"
|
162 |
),
|
163 |
gr.Slider(
|
164 |
minimum=0.1,
|
165 |
maximum=2.0,
|
166 |
value=0.7,
|
167 |
step=0.1,
|
168 |
+
label="Temperature"
|
169 |
),
|
170 |
gr.Slider(
|
171 |
minimum=0.1,
|
172 |
maximum=1.0,
|
173 |
value=0.95,
|
174 |
step=0.05,
|
175 |
+
label="Top-p"
|
176 |
),
|
177 |
+
language # Pass the selected language to the respond function
|
178 |
],
|
179 |
+
examples=TRANSLATIONS["en"]["examples"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
)
|
181 |
|
182 |
+
# Update interface when language changes
|
183 |
+
language.change(
|
184 |
+
fn=update_interface,
|
185 |
+
inputs=[language],
|
186 |
+
outputs=[
|
187 |
+
title_md,
|
188 |
+
description_md,
|
189 |
+
chatbot.textbox,
|
190 |
+
*[input.label for input in chatbot.additional_inputs[:-1]], # Exclude language selector
|
191 |
+
chatbot.title,
|
192 |
+
chatbot.description,
|
193 |
+
info_md,
|
194 |
+
chatbot.examples
|
195 |
+
]
|
196 |
+
)
|
197 |
|
198 |
+
# Initialize interface with default language
|
199 |
+
demo.load(
|
200 |
+
fn=update_interface,
|
201 |
+
inputs=[language],
|
202 |
+
outputs=[
|
203 |
+
title_md,
|
204 |
+
description_md,
|
205 |
+
chatbot.textbox,
|
206 |
+
*[input.label for input in chatbot.additional_inputs[:-1]],
|
207 |
+
chatbot.title,
|
208 |
+
chatbot.description,
|
209 |
+
info_md,
|
210 |
+
chatbot.examples
|
211 |
+
]
|
212 |
+
)
|
213 |
+
|
214 |
return demo
|
215 |
|
216 |
if __name__ == "__main__":
|