Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,29 +2,35 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import time
|
|
|
5 |
|
|
|
6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
7 |
|
|
|
8 |
client = InferenceClient(
|
9 |
provider="sambanova",
|
10 |
api_key=HF_TOKEN,
|
11 |
)
|
12 |
|
|
|
13 |
MODELS = {
|
14 |
"LLaMA 70B": "meta-llama/Llama-3.3-70B-Instruct",
|
15 |
"Qwen 32B": "Qwen/QwQ-32B",
|
16 |
-
"DeepSeek R1":"deepseek-ai/DeepSeek-R1",
|
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(
|
@@ -36,26 +42,26 @@ def chatbot_response(user_input, model_name):
|
|
36 |
except Exception as e:
|
37 |
response = f"Erro ao gerar resposta: {str(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.
|
54 |
choices=list(MODELS.keys()),
|
55 |
label="Escolha o Modelo",
|
56 |
value="LLaMA 70B"
|
57 |
)
|
58 |
-
|
59 |
with gr.Column(scale=4):
|
60 |
gr.Markdown("# 馃 Chatbot - API SambaNova")
|
61 |
chatbot = gr.Chatbot(height=500)
|
@@ -63,16 +69,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
63 |
with gr.Row():
|
64 |
btn = gr.Button("Enviar", variant="primary")
|
65 |
history_btn = gr.Button("Hist贸rico", variant="secondary")
|
66 |
-
|
67 |
history_output = gr.JSON()
|
68 |
|
69 |
def respond(message, chat_history, model_name):
|
70 |
response = chatbot_response(message, model_name)
|
|
|
71 |
chat_history.append((message, response))
|
72 |
return "", chat_history
|
73 |
|
74 |
btn.click(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
75 |
-
msg.submit(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
76 |
|
77 |
def toggle_history():
|
78 |
global show_history_flag
|
@@ -81,5 +87,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
81 |
|
82 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
83 |
|
|
|
84 |
if __name__ == "__main__":
|
85 |
demo.launch()
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import time
|
5 |
+
import re # para limpar tags como <think>
|
6 |
|
7 |
+
# Token de autentica莽茫o
|
8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
9 |
|
10 |
+
# Cliente da API da HuggingFace/SambaNova
|
11 |
client = InferenceClient(
|
12 |
provider="sambanova",
|
13 |
api_key=HF_TOKEN,
|
14 |
)
|
15 |
|
16 |
+
# Modelos dispon铆veis
|
17 |
MODELS = {
|
18 |
"LLaMA 70B": "meta-llama/Llama-3.3-70B-Instruct",
|
19 |
"Qwen 32B": "Qwen/QwQ-32B",
|
20 |
+
"DeepSeek R1": "deepseek-ai/DeepSeek-R1",
|
21 |
}
|
22 |
|
23 |
history_log = []
|
24 |
show_history_flag = False
|
25 |
|
26 |
+
def clean_response(text):
|
27 |
+
return re.sub(r"</?think>", "", text).strip()
|
28 |
+
|
29 |
def chatbot_response(user_input, model_name):
|
30 |
model_id = MODELS[model_name]
|
|
|
31 |
messages = [{"role": "user", "content": entry["Pergunta"]} for entry in history_log[-2:]]
|
32 |
messages.append({"role": "user", "content": user_input})
|
33 |
+
|
34 |
start_time = time.time()
|
35 |
try:
|
36 |
completion = client.chat.completions.create(
|
|
|
42 |
except Exception as e:
|
43 |
response = f"Erro ao gerar resposta: {str(e)}"
|
44 |
end_time = time.time()
|
45 |
+
|
46 |
history_log.append({
|
47 |
"Modelo": model_name,
|
48 |
"Pergunta": user_input,
|
49 |
"Resposta": response,
|
50 |
"Tempo de Resposta (s)": round(end_time - start_time, 2)
|
51 |
})
|
52 |
+
|
53 |
return response
|
54 |
|
55 |
+
# Interface Gradio
|
56 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
57 |
with gr.Row():
|
58 |
with gr.Column(scale=1):
|
59 |
gr.Markdown("## 鈿欙笍 Configura莽玫es")
|
60 |
+
model_selector = gr.Dropdown(
|
61 |
choices=list(MODELS.keys()),
|
62 |
label="Escolha o Modelo",
|
63 |
value="LLaMA 70B"
|
64 |
)
|
|
|
65 |
with gr.Column(scale=4):
|
66 |
gr.Markdown("# 馃 Chatbot - API SambaNova")
|
67 |
chatbot = gr.Chatbot(height=500)
|
|
|
69 |
with gr.Row():
|
70 |
btn = gr.Button("Enviar", variant="primary")
|
71 |
history_btn = gr.Button("Hist贸rico", variant="secondary")
|
|
|
72 |
history_output = gr.JSON()
|
73 |
|
74 |
def respond(message, chat_history, model_name):
|
75 |
response = chatbot_response(message, model_name)
|
76 |
+
response = clean_response(response)
|
77 |
chat_history.append((message, response))
|
78 |
return "", chat_history
|
79 |
|
80 |
btn.click(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
81 |
+
msg.submit(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
82 |
|
83 |
def toggle_history():
|
84 |
global show_history_flag
|
|
|
87 |
|
88 |
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
89 |
|
90 |
+
# Executa o app
|
91 |
if __name__ == "__main__":
|
92 |
demo.launch()
|