Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,19 +10,27 @@ client = InferenceClient(
|
|
10 |
api_key=HF_TOKEN,
|
11 |
)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
history_log = []
|
14 |
-
show_history_flag = False
|
|
|
|
|
|
|
15 |
|
16 |
-
def chatbot_response(user_input):
|
17 |
messages = [{"role": "user", "content": entry["Pergunta"]} for entry in history_log[-2:]]
|
18 |
messages.append({"role": "user", "content": user_input})
|
19 |
|
20 |
start_time = time.time()
|
21 |
try:
|
22 |
completion = client.chat.completions.create(
|
23 |
-
model=
|
24 |
-
messages=messages,
|
25 |
-
max_tokens=800,
|
26 |
)
|
27 |
response = completion.choices[0].message['content']
|
28 |
except Exception as e:
|
@@ -30,37 +38,48 @@ def chatbot_response(user_input):
|
|
30 |
end_time = time.time()
|
31 |
|
32 |
history_log.append({
|
|
|
33 |
"Pergunta": user_input,
|
34 |
"Resposta": response,
|
35 |
"Tempo de Resposta (s)": round(end_time - start_time, 2)
|
36 |
})
|
37 |
-
|
38 |
-
print(history_log)
|
39 |
|
40 |
return response
|
41 |
|
42 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
43 |
-
gr.
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
chat_history.append((message, response))
|
53 |
return "", chat_history
|
54 |
-
|
55 |
-
btn.click(respond, [msg, chatbot], [msg, chatbot])
|
56 |
-
|
|
|
57 |
def toggle_history():
|
58 |
global show_history_flag
|
59 |
show_history_flag = not show_history_flag
|
60 |
return history_log if show_history_flag else {}
|
61 |
-
|
62 |
-
history_btn = gr.Button("Ver/Fechar Hist贸rico", variant="secondary")
|
63 |
-
history_output = gr.JSON()
|
64 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
65 |
|
66 |
if __name__ == "__main__":
|
|
|
10 |
api_key=HF_TOKEN,
|
11 |
)
|
12 |
|
13 |
+
# Modelos dispon铆veis
|
14 |
+
MODELS = {
|
15 |
+
"LLaMA 70B": "meta-llama/Llama-3.3-70B-Instruct",
|
16 |
+
"Qwen 32B": "Qwen/QwQ-32B"
|
17 |
+
}
|
18 |
+
|
19 |
history_log = []
|
20 |
+
show_history_flag = False
|
21 |
+
|
22 |
+
def chatbot_response(user_input, model_name):
|
23 |
+
model_id = MODELS[model_name]
|
24 |
|
|
|
25 |
messages = [{"role": "user", "content": entry["Pergunta"]} for entry in history_log[-2:]]
|
26 |
messages.append({"role": "user", "content": user_input})
|
27 |
|
28 |
start_time = time.time()
|
29 |
try:
|
30 |
completion = client.chat.completions.create(
|
31 |
+
model=model_id,
|
32 |
+
messages=messages,
|
33 |
+
max_tokens=8192 if "Qwen" in model_id else 800,
|
34 |
)
|
35 |
response = completion.choices[0].message['content']
|
36 |
except Exception as e:
|
|
|
38 |
end_time = time.time()
|
39 |
|
40 |
history_log.append({
|
41 |
+
"Modelo": model_name,
|
42 |
"Pergunta": user_input,
|
43 |
"Resposta": response,
|
44 |
"Tempo de Resposta (s)": round(end_time - start_time, 2)
|
45 |
})
|
|
|
|
|
46 |
|
47 |
return response
|
48 |
|
49 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column(scale=1):
|
52 |
+
gr.Markdown("## 鈿欙笍 Configura莽玫es")
|
53 |
+
model_selector = gr.Radio(
|
54 |
+
choices=list(MODELS.keys()),
|
55 |
+
label="Escolha o Modelo",
|
56 |
+
value="LLaMA 70B"
|
57 |
+
)
|
58 |
+
history_btn = gr.Button("Ver/Fechar Hist贸rico", variant="secondary")
|
59 |
+
history_output = gr.JSON()
|
60 |
+
with gr.Column(scale=4):
|
61 |
+
gr.Markdown("# 馃 Chatbot - SambaNova")
|
62 |
+
chatbot = gr.Chatbot(height=500)
|
63 |
+
msg = gr.Textbox(placeholder="Digite sua mensagem aqui...", show_label=False)
|
64 |
+
with gr.Row():
|
65 |
+
btn = gr.Button("Enviar", variant="primary")
|
66 |
+
history_btn = gr.Button("Ver/Fechar Hist贸rico", variant="secondary")
|
67 |
+
|
68 |
+
history_output = gr.JSON()
|
69 |
+
|
70 |
+
def respond(message, chat_history, model_name):
|
71 |
+
response = chatbot_response(message, model_name)
|
72 |
chat_history.append((message, response))
|
73 |
return "", chat_history
|
74 |
+
|
75 |
+
btn.click(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
76 |
+
msg.submit(respond, [msg, chatbot, model_selector], [msg, chatbot]) # permite envio com Enter
|
77 |
+
|
78 |
def toggle_history():
|
79 |
global show_history_flag
|
80 |
show_history_flag = not show_history_flag
|
81 |
return history_log if show_history_flag else {}
|
82 |
+
|
|
|
|
|
83 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
84 |
|
85 |
if __name__ == "__main__":
|