Commit
·
62cbda2
1
Parent(s):
d445c9b
Refactor call_api function to call_nvidia_api
Browse files
app.py
CHANGED
@@ -27,23 +27,9 @@ def user(message, history):
|
|
27 |
history.append({"role": "user", "content": message})
|
28 |
return history
|
29 |
|
30 |
-
def
|
31 |
-
|
32 |
-
messages = []
|
33 |
-
|
34 |
-
# Adicionando a mensagem de sistema, se presente
|
35 |
-
if system_message:
|
36 |
-
messages.append({"role": "system", "content": system_message})
|
37 |
-
|
38 |
-
# Adicionando mensagens de usuário e assistente da conversa
|
39 |
-
for msg in history:
|
40 |
-
messages.append({"role": msg["role"], "content": msg["content"]})
|
41 |
-
|
42 |
-
# Verificando se a última mensagem é do usuário, se não, ajuste conforme necessário
|
43 |
-
if not messages or messages[-1]["role"] != "user":
|
44 |
-
print("A última mensagem deve ser do usuário com conteúdo preenchido.")
|
45 |
-
return ""
|
46 |
-
|
47 |
payload = {
|
48 |
"messages": messages,
|
49 |
"temperature": temperature,
|
@@ -52,6 +38,7 @@ def call_api(history, max_tokens, temperature, top_p, system_message="", seed=42
|
|
52 |
"seed": seed,
|
53 |
"stream": True
|
54 |
}
|
|
|
55 |
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
56 |
full_response = ""
|
57 |
for line in response.iter_lines():
|
@@ -60,27 +47,24 @@ def call_api(history, max_tokens, temperature, top_p, system_message="", seed=42
|
|
60 |
if decoded_line.startswith("data:"):
|
61 |
try:
|
62 |
json_data = json.loads(decoded_line[5:])
|
|
|
|
|
|
|
63 |
except json.JSONDecodeError:
|
64 |
print(f"Invalid JSON: {decoded_line[5:]}")
|
65 |
-
json_data = {}
|
66 |
-
if "choices" in json_data and len(json_data["choices"]) > 0:
|
67 |
-
deltas = json_data["choices"][0].get("delta", {})
|
68 |
-
if "content" in deltas:
|
69 |
-
full_response += deltas["content"]
|
70 |
-
print(f"Full API response: {full_response}")
|
71 |
return full_response
|
72 |
|
73 |
def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
74 |
print("Starting chat...")
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
78 |
else:
|
79 |
-
history.append(
|
80 |
-
|
81 |
-
|
82 |
-
history.append({"role": "assistant", "content": assistant_response})
|
83 |
-
return history, "", ""
|
84 |
|
85 |
# Gradio interface setup
|
86 |
with gr.Blocks() as demo:
|
|
|
27 |
history.append({"role": "user", "content": message})
|
28 |
return history
|
29 |
|
30 |
+
def call_nvidia_api(history, max_tokens, temperature, top_p, seed=42):
|
31 |
+
# Preparar o payload com o histórico de chat formatado
|
32 |
+
messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg} for i, msg in enumerate(history)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
payload = {
|
34 |
"messages": messages,
|
35 |
"temperature": temperature,
|
|
|
38 |
"seed": seed,
|
39 |
"stream": True
|
40 |
}
|
41 |
+
|
42 |
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
43 |
full_response = ""
|
44 |
for line in response.iter_lines():
|
|
|
47 |
if decoded_line.startswith("data:"):
|
48 |
try:
|
49 |
json_data = json.loads(decoded_line[5:])
|
50 |
+
# Processar a resposta da API aqui
|
51 |
+
# Supondo que a resposta da API seja diretamente o texto a ser adicionado ao chat
|
52 |
+
full_response += json_data.get("content", "")
|
53 |
except json.JSONDecodeError:
|
54 |
print(f"Invalid JSON: {decoded_line[5:]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return full_response
|
56 |
|
57 |
def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
58 |
print("Starting chat...")
|
59 |
+
# Chamar a API da NVIDIA aqui com o histórico formatado
|
60 |
+
assistant_response = call_nvidia_api(history, max_tokens, temperature, top_p)
|
61 |
+
# Atualizar o histórico com a resposta do assistente
|
62 |
+
if history:
|
63 |
+
history[-1][1] += assistant_response
|
64 |
else:
|
65 |
+
history.append(["", assistant_response])
|
66 |
+
return history, history, ""
|
67 |
+
|
|
|
|
|
68 |
|
69 |
# Gradio interface setup
|
70 |
with gr.Blocks() as demo:
|