artificialguybr commited on
Commit
85dbf4a
1 Parent(s): 8b4d47d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -17,12 +17,10 @@ headers = {
17
  BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
18
 
19
  def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
20
- """Chama a API da NVIDIA para gerar uma resposta."""
21
- # Prepara as mensagens, incluindo a mensagem do sistema se fornecida
22
  messages = []
23
  if system_message:
24
  messages.append({"role": "system", "content": system_message})
25
- messages.extend(history)
26
 
27
  payload = {
28
  "messages": messages,
@@ -31,13 +29,15 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
31
  "max_tokens": max_tokens,
32
  "stream": False
33
  }
 
 
34
  session = requests.Session()
35
  response = session.post(INVOKE_URL, headers=headers, json=payload)
36
- while response.status_code == 202:
37
  request_id = response.headers.get("NVCF-REQID")
38
  fetch_url = FETCH_URL_FORMAT + request_id
39
  response = session.get(fetch_url, headers=headers)
40
- response.raise_for_status()
41
  response_body = response.json()
42
  if response_body.get("choices"):
43
  assistant_message = response_body["choices"][0]["message"]["content"]
@@ -45,6 +45,7 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
45
  else:
46
  return "Desculpe, ocorreu um erro ao gerar a resposta."
47
 
 
48
  def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
49
  """Submits the user message to the chatbot and updates the chat history."""
50
  print("Updating chatbot...")
@@ -60,13 +61,13 @@ def chatbot_submit(message, chat_history, system_message, max_tokens_val, temper
60
 
61
  return assistant_message, chat_history
62
 
 
 
 
 
63
  # Gradio interface setup
64
  with gr.Blocks() as demo:
65
  chat_history_state = 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
  additional_inputs=[system_msg, max_tokens, temperature, top_p],
 
17
  BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
18
 
19
  def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
 
 
20
  messages = []
21
  if system_message:
22
  messages.append({"role": "system", "content": system_message})
23
+ messages.extend([{"role": "user", "content": msg[0]} for msg in history])
24
 
25
  payload = {
26
  "messages": messages,
 
29
  "max_tokens": max_tokens,
30
  "stream": False
31
  }
32
+ print("Payload sendo enviado:", json.dumps(payload, indent=4)) # Debug: Imprime a payload
33
+
34
  session = requests.Session()
35
  response = session.post(INVOKE_URL, headers=headers, json=payload)
36
+ if response.status_code == 202:
37
  request_id = response.headers.get("NVCF-REQID")
38
  fetch_url = FETCH_URL_FORMAT + request_id
39
  response = session.get(fetch_url, headers=headers)
40
+ response.raise_for_status() # Isso lançará uma exceção se o status não for 200
41
  response_body = response.json()
42
  if response_body.get("choices"):
43
  assistant_message = response_body["choices"][0]["message"]["content"]
 
45
  else:
46
  return "Desculpe, ocorreu um erro ao gerar a resposta."
47
 
48
+
49
  def chatbot_submit(message, chat_history, system_message, max_tokens_val, temperature_val, top_p_val):
50
  """Submits the user message to the chatbot and updates the chat history."""
51
  print("Updating chatbot...")
 
61
 
62
  return assistant_message, chat_history
63
 
64
+ system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
65
+ max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024)
66
+ temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.2)
67
+ top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7)
68
  # Gradio interface setup
69
  with gr.Blocks() as demo:
70
  chat_history_state = gr.State([])
 
 
 
 
71
  chatbot = gr.ChatInterface(
72
  fn=chatbot_submit,
73
  additional_inputs=[system_msg, max_tokens, temperature, top_p],