Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,8 @@ import requests
|
|
3 |
import os
|
4 |
import json
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/df2bee43-fb69-42b9-9ee5-f4eabbeaf3a8"
|
9 |
FETCH_URL_FORMAT = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
|
10 |
|
11 |
headers = {
|
@@ -16,9 +15,13 @@ headers = {
|
|
16 |
|
17 |
BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
|
18 |
|
19 |
-
def call_nvidia_api(
|
20 |
messages = [{"role": "system", "content": system_message}] if system_message else []
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
payload = {
|
23 |
"messages": messages,
|
24 |
"temperature": temperature,
|
@@ -38,24 +41,17 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
|
38 |
|
39 |
if response_body.get("choices"):
|
40 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
41 |
-
# Retorna tanto a mensagem formatada para o usuário quanto a estrutura completa para o histórico da API
|
42 |
return assistant_message, response_body["choices"][0]
|
43 |
else:
|
44 |
return "Desculpe, ocorreu um erro ao gerar a resposta.", None
|
45 |
|
46 |
-
|
47 |
def chatbot_submit(message, chat_history_ui, chat_history_api, system_message, max_tokens_val, temperature_val, top_p_val):
|
48 |
-
print("Updating chatbot...")
|
49 |
-
|
50 |
-
# Chama a API da NVIDIA para gerar uma resposta
|
51 |
assistant_message, api_response = call_nvidia_api(chat_history_api, system_message, max_tokens_val, temperature_val, top_p_val)
|
52 |
|
53 |
-
# Atualiza o histórico da interface do usuário
|
54 |
chat_history_ui.append([message, assistant_message])
|
55 |
|
56 |
-
# Atualiza o histórico da API se a resposta incluir o formato esperado
|
57 |
if api_response:
|
58 |
-
chat_history_api.append(
|
59 |
|
60 |
return assistant_message, chat_history_ui, chat_history_api
|
61 |
|
@@ -67,27 +63,15 @@ top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7)
|
|
67 |
with gr.Blocks() as demo:
|
68 |
chat_history_state_ui = gr.State([])
|
69 |
chat_history_state_api = gr.State([])
|
70 |
-
|
|
|
|
|
|
|
71 |
chatbot = gr.ChatInterface(
|
72 |
fn=chatbot_submit,
|
73 |
inputs=[gr.Textbox(label="Your Message"), chat_history_state_ui, chat_history_state_api, system_msg, max_tokens, temperature, top_p],
|
74 |
outputs=[gr.Text(label="Assistant Response"), chat_history_state_ui, chat_history_state_api],
|
75 |
title="Chatbot Interface"
|
76 |
-
description="""<div style="text-align: center; font-size: 1.5em; margin-bottom: 20px;">
|
77 |
-
<strong>Explore the Capabilities of LLAMA 2 70B</strong>
|
78 |
-
</div>
|
79 |
-
<p>Llama 2 is a large language AI model capable of generating text and code in response to prompts.</p>
|
80 |
-
<p><strong>How to Use:</strong></p>
|
81 |
-
<ol>
|
82 |
-
<li>Enter your <strong>message</strong> in the textbox to start a conversation or ask a question.</li>
|
83 |
-
<li>Adjust the parameters in the "Additional Inputs" accordion to control the model's behavior.</li>
|
84 |
-
<li>Use the buttons below the chatbot to submit your query, clear the chat history, or perform other actions.</li>
|
85 |
-
</ol>
|
86 |
-
<p><strong>Powered by NVIDIA's cutting-edge AI API, LLAMA 2 70B offers an unparalleled opportunity to interact with an AI model of exceptional conversational ability, accessible to everyone at no cost.</strong></p>
|
87 |
-
<p><strong>HF Created by:</strong> @artificialguybr (<a href="https://twitter.com/artificialguybr">Twitter</a>)</p>
|
88 |
-
<p><strong>Discover more:</strong> <a href="https://artificialguy.com">artificialguy.com</a></p>""",
|
89 |
-
submit_btn="Submit",
|
90 |
-
clear_btn="🗑️ Clear",
|
91 |
)
|
92 |
|
93 |
-
demo.launch()
|
|
|
3 |
import os
|
4 |
import json
|
5 |
|
6 |
+
API_KEY = os.getenv('API_KEY')
|
7 |
+
INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/0e349b44-440a-44e1-93e9-abe8dcb27158"
|
|
|
8 |
FETCH_URL_FORMAT = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
|
9 |
|
10 |
headers = {
|
|
|
15 |
|
16 |
BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
|
17 |
|
18 |
+
def call_nvidia_api(history_api, system_message, max_tokens, temperature, top_p):
|
19 |
messages = [{"role": "system", "content": system_message}] if system_message else []
|
20 |
+
for msg in history_api:
|
21 |
+
if msg[1]: # Se existe uma resposta do assistente
|
22 |
+
messages.append({"role": "user", "content": msg[0]})
|
23 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
24 |
+
|
25 |
payload = {
|
26 |
"messages": messages,
|
27 |
"temperature": temperature,
|
|
|
41 |
|
42 |
if response_body.get("choices"):
|
43 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
|
|
44 |
return assistant_message, response_body["choices"][0]
|
45 |
else:
|
46 |
return "Desculpe, ocorreu um erro ao gerar a resposta.", None
|
47 |
|
|
|
48 |
def chatbot_submit(message, chat_history_ui, chat_history_api, system_message, max_tokens_val, temperature_val, top_p_val):
|
|
|
|
|
|
|
49 |
assistant_message, api_response = call_nvidia_api(chat_history_api, system_message, max_tokens_val, temperature_val, top_p_val)
|
50 |
|
|
|
51 |
chat_history_ui.append([message, assistant_message])
|
52 |
|
|
|
53 |
if api_response:
|
54 |
+
chat_history_api.append([message, assistant_message])
|
55 |
|
56 |
return assistant_message, chat_history_ui, chat_history_api
|
57 |
|
|
|
63 |
with gr.Blocks() as demo:
|
64 |
chat_history_state_ui = gr.State([])
|
65 |
chat_history_state_api = gr.State([])
|
66 |
+
system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
|
67 |
+
max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024)
|
68 |
+
temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.2)
|
69 |
+
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7)
|
70 |
chatbot = gr.ChatInterface(
|
71 |
fn=chatbot_submit,
|
72 |
inputs=[gr.Textbox(label="Your Message"), chat_history_state_ui, chat_history_state_api, system_msg, max_tokens, temperature, top_p],
|
73 |
outputs=[gr.Text(label="Assistant Response"), chat_history_state_ui, chat_history_state_api],
|
74 |
title="Chatbot Interface"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
)
|
76 |
|
77 |
+
demo.launch()
|