Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import json
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# API and environment variables
|
| 7 |
API_KEY = os.getenv('API_KEY')
|
|
@@ -19,7 +19,7 @@ BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuance
|
|
| 19 |
def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
| 20 |
"""Calls the NVIDIA API to generate a response."""
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
-
messages.extend([{"role": "user", "content":
|
| 23 |
|
| 24 |
payload = {
|
| 25 |
"messages": messages,
|
|
@@ -28,7 +28,6 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
|
| 28 |
"max_tokens": max_tokens,
|
| 29 |
"stream": False
|
| 30 |
}
|
| 31 |
-
print(f"Payload enviado: {json.dumps(payload, indent=2)}")
|
| 32 |
session = requests.Session()
|
| 33 |
response = session.post(INVOKE_URL, headers=headers, json=payload)
|
| 34 |
while response.status_code == 202:
|
|
@@ -37,28 +36,25 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
|
| 37 |
response = session.get(fetch_url, headers=headers)
|
| 38 |
response.raise_for_status()
|
| 39 |
response_body = response.json()
|
| 40 |
-
print(f"Payload recebido: {json.dumps(response_body, indent=2)}")
|
| 41 |
if response_body.get("choices"):
|
| 42 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
| 43 |
return assistant_message
|
| 44 |
else:
|
| 45 |
-
return "
|
| 46 |
|
| 47 |
def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
|
| 48 |
"""Submits the user message to the chatbot and updates the chat history."""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Adiciona a mensagem do usu谩rio ao hist贸rico para exibi莽茫o
|
| 52 |
-
chat_history.append([message, ""])
|
| 53 |
|
| 54 |
-
#
|
| 55 |
assistant_message = call_nvidia_api(chat_history, system_message, max_tokens_val, temperature_val, top_p_val)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
chat_history[-1][1] = assistant_message
|
| 59 |
|
| 60 |
return assistant_message, chat_history
|
| 61 |
|
|
|
|
| 62 |
chat_history_state = gr.State([])
|
| 63 |
system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
|
| 64 |
max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
# API and environment variables
|
| 7 |
API_KEY = os.getenv('API_KEY')
|
|
|
|
| 19 |
def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
| 20 |
"""Calls the NVIDIA API to generate a response."""
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
+
messages.extend([{"role": "user", "content": msg} for msg, _ in history])
|
| 23 |
|
| 24 |
payload = {
|
| 25 |
"messages": messages,
|
|
|
|
| 28 |
"max_tokens": max_tokens,
|
| 29 |
"stream": False
|
| 30 |
}
|
|
|
|
| 31 |
session = requests.Session()
|
| 32 |
response = session.post(INVOKE_URL, headers=headers, json=payload)
|
| 33 |
while response.status_code == 202:
|
|
|
|
| 36 |
response = session.get(fetch_url, headers=headers)
|
| 37 |
response.raise_for_status()
|
| 38 |
response_body = response.json()
|
|
|
|
| 39 |
if response_body.get("choices"):
|
| 40 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
| 41 |
return assistant_message
|
| 42 |
else:
|
| 43 |
+
return "Sorry, there was an error generating the response."
|
| 44 |
|
| 45 |
def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
|
| 46 |
"""Submits the user message to the chatbot and updates the chat history."""
|
| 47 |
+
chat_history.append([message, ""]) # Add user message to history
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Call NVIDIA API to generate a response
|
| 50 |
assistant_message = call_nvidia_api(chat_history, system_message, max_tokens_val, temperature_val, top_p_val)
|
| 51 |
|
| 52 |
+
# Update history with assistant's response
|
| 53 |
chat_history[-1][1] = assistant_message
|
| 54 |
|
| 55 |
return assistant_message, chat_history
|
| 56 |
|
| 57 |
+
# Gradio interface setup
|
| 58 |
chat_history_state = gr.State([])
|
| 59 |
system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
|
| 60 |
max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024)
|