Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
|
|
4 |
|
5 |
-
# Pegando o token da Hugging Face do ambiente
|
6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
7 |
|
8 |
-
# Inicializando o cliente de infer锚ncia
|
9 |
client = InferenceClient(
|
10 |
provider="sambanova",
|
11 |
api_key=HF_TOKEN,
|
12 |
)
|
13 |
|
|
|
|
|
|
|
14 |
def chatbot_response(user_input):
|
15 |
-
messages = [{"role": "user", "content":
|
|
|
16 |
|
|
|
17 |
try:
|
18 |
completion = client.chat.completions.create(
|
19 |
model="meta-llama/Llama-3.3-70B-Instruct",
|
20 |
messages=messages,
|
21 |
max_tokens=500,
|
22 |
)
|
23 |
-
|
24 |
except Exception as e:
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Criando interface Gradio
|
28 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
29 |
gr.Markdown("# 馃 Llama-70B Chatbot - SambaNova")
|
30 |
gr.Markdown("Modelo: meta-llama/Llama-3.3-70B-Instruct")
|
31 |
|
32 |
-
chatbot = gr.Chatbot()
|
33 |
msg = gr.Textbox(placeholder="Digite sua mensagem aqui...")
|
34 |
-
btn = gr.Button("Enviar")
|
35 |
|
36 |
def respond(message, chat_history):
|
37 |
response = chatbot_response(message)
|
@@ -39,7 +51,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
39 |
return "", chat_history
|
40 |
|
41 |
btn.click(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
# Rodando a aplica莽茫o
|
44 |
if __name__ == "__main__":
|
45 |
demo.launch()
|
|
|
1 |
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 |
+
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="meta-llama/Llama-3.3-70B-Instruct",
|
24 |
messages=messages,
|
25 |
max_tokens=500,
|
26 |
)
|
27 |
+
response = completion.choices[0].message['content']
|
28 |
except Exception as e:
|
29 |
+
response = f"Erro ao gerar resposta: {str(e)}"
|
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 |
+
return response
|
39 |
|
|
|
40 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
41 |
gr.Markdown("# 馃 Llama-70B Chatbot - SambaNova")
|
42 |
gr.Markdown("Modelo: meta-llama/Llama-3.3-70B-Instruct")
|
43 |
|
44 |
+
chatbot = gr.Chatbot(height=500)
|
45 |
msg = gr.Textbox(placeholder="Digite sua mensagem aqui...")
|
46 |
+
btn = gr.Button("Enviar", variant="primary")
|
47 |
|
48 |
def respond(message, chat_history):
|
49 |
response = chatbot_response(message)
|
|
|
51 |
return "", chat_history
|
52 |
|
53 |
btn.click(respond, [msg, chatbot], [msg, chatbot])
|
54 |
+
|
55 |
+
def toggle_history():
|
56 |
+
global show_history_flag
|
57 |
+
show_history_flag = not show_history_flag
|
58 |
+
return history_log if show_history_flag else {}
|
59 |
+
|
60 |
+
history_btn = gr.Button("Ver/Fechar Hist贸rico", variant="secondary")
|
61 |
+
history_output = gr.JSON()
|
62 |
+
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
63 |
|
|
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|