Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import time
|
5 |
-
import re
|
6 |
|
7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
8 |
|
@@ -17,18 +17,20 @@ MODELS = {
|
|
17 |
"DeepSeek R1": "deepseek-ai/DeepSeek-R1"
|
18 |
}
|
19 |
|
20 |
-
history_log = []
|
|
|
21 |
show_history_flag = False
|
22 |
|
23 |
def clean_response(text):
|
24 |
text = re.sub(r"</?think>", "", text)
|
25 |
-
text = re.sub(r"\\boxed
|
26 |
-
|
27 |
return text.strip()
|
28 |
|
29 |
def chatbot_response(user_input, model_name):
|
30 |
model_id = MODELS[model_name]
|
31 |
-
|
|
|
|
|
32 |
messages.append({"role": "user", "content": user_input})
|
33 |
|
34 |
start_time = time.time()
|
@@ -36,14 +38,17 @@ def chatbot_response(user_input, model_name):
|
|
36 |
completion = client.chat.completions.create(
|
37 |
model=model_id,
|
38 |
messages=messages,
|
39 |
-
max_tokens
|
40 |
)
|
41 |
response = completion.choices[0].message['content']
|
42 |
-
|
43 |
except Exception as e:
|
44 |
response = f"Erro ao gerar resposta: {str(e)}"
|
45 |
end_time = time.time()
|
46 |
|
|
|
|
|
|
|
|
|
47 |
history_log.append({
|
48 |
"Modelo": model_name,
|
49 |
"Pergunta": user_input,
|
@@ -67,10 +72,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
67 |
gr.Markdown("# 馃 Chatbot - API SambaNova")
|
68 |
chatbot = gr.Chatbot(height=500)
|
69 |
msg = gr.Textbox(placeholder="Digite sua mensagem aqui...", show_label=False)
|
70 |
-
|
71 |
btn = gr.Button("Enviar", variant="primary")
|
72 |
history_btn = gr.Button("Hist贸rico", variant="secondary")
|
73 |
-
|
74 |
history_output = gr.JSON()
|
75 |
|
76 |
def respond(message, chat_history, model_name):
|
@@ -91,4 +95,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
demo.launch()
|
94 |
-
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import time
|
5 |
+
import re
|
6 |
|
7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
8 |
|
|
|
17 |
"DeepSeek R1": "deepseek-ai/DeepSeek-R1"
|
18 |
}
|
19 |
|
20 |
+
history_log = []
|
21 |
+
recent_history = []
|
22 |
show_history_flag = False
|
23 |
|
24 |
def clean_response(text):
|
25 |
text = re.sub(r"</?think>", "", text)
|
26 |
+
text = re.sub(r"\\boxed\\{.*?\\}", "", text)
|
|
|
27 |
return text.strip()
|
28 |
|
29 |
def chatbot_response(user_input, model_name):
|
30 |
model_id = MODELS[model_name]
|
31 |
+
|
32 |
+
# Mant茅m apenas as 2 煤ltimas intera莽玫es para o modelo
|
33 |
+
messages = recent_history[-2:] if len(recent_history) >= 2 else []
|
34 |
messages.append({"role": "user", "content": user_input})
|
35 |
|
36 |
start_time = time.time()
|
|
|
38 |
completion = client.chat.completions.create(
|
39 |
model=model_id,
|
40 |
messages=messages,
|
41 |
+
max_tokens=8192 if any(m in model_id for m in ["Qwen", "DeepSeek"]) else 800
|
42 |
)
|
43 |
response = completion.choices[0].message['content']
|
|
|
44 |
except Exception as e:
|
45 |
response = f"Erro ao gerar resposta: {str(e)}"
|
46 |
end_time = time.time()
|
47 |
|
48 |
+
# Atualiza os dois hist贸ricos
|
49 |
+
recent_history.append({"role": "user", "content": user_input})
|
50 |
+
recent_history.append({"role": "assistant", "content": response})
|
51 |
+
|
52 |
history_log.append({
|
53 |
"Modelo": model_name,
|
54 |
"Pergunta": user_input,
|
|
|
72 |
gr.Markdown("# 馃 Chatbot - API SambaNova")
|
73 |
chatbot = gr.Chatbot(height=500)
|
74 |
msg = gr.Textbox(placeholder="Digite sua mensagem aqui...", show_label=False)
|
75 |
+
|
76 |
btn = gr.Button("Enviar", variant="primary")
|
77 |
history_btn = gr.Button("Hist贸rico", variant="secondary")
|
|
|
78 |
history_output = gr.JSON()
|
79 |
|
80 |
def respond(message, chat_history, model_name):
|
|
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
demo.launch()
|
|