artificialguybr commited on
Commit
0e16686
1 Parent(s): 85dbf4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -17,10 +17,8 @@ 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
- 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,37 +27,38 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
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"]
44
- return assistant_message
 
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...")
52
 
53
- # Adiciona a mensagem do usuário ao histórico para exibição
54
- chat_history.append([message, ""])
55
-
56
  # Chama a API da NVIDIA para gerar uma resposta
57
- assistant_message = call_nvidia_api(chat_history, system_message, max_tokens_val, temperature_val, top_p_val)
 
 
 
58
 
59
- # Atualiza o histórico com a resposta do assistente
60
- chat_history[-1][1] = assistant_message
 
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)
@@ -67,11 +66,14 @@ 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],
74
- title="LLAMA 70B Free Demo",
 
75
  description="""<div style="text-align: center; font-size: 1.5em; margin-bottom: 20px;">
76
  <strong>Explore the Capabilities of LLAMA 2 70B</strong>
77
  </div>
 
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 = [{"role": "system", "content": system_message}] if system_message else []
21
+ messages.extend([{"role": "user", "content": msg[0]}, {"role": "assistant", "content": msg[1]} for msg in history if msg[1]])
 
 
22
 
23
  payload = {
24
  "messages": messages,
 
27
  "max_tokens": max_tokens,
28
  "stream": False
29
  }
 
30
 
31
  session = requests.Session()
32
  response = session.post(INVOKE_URL, headers=headers, json=payload)
33
+ while response.status_code == 202:
34
  request_id = response.headers.get("NVCF-REQID")
35
  fetch_url = FETCH_URL_FORMAT + request_id
36
  response = session.get(fetch_url, headers=headers)
37
+ response.raise_for_status()
38
  response_body = response.json()
39
+
40
  if response_body.get("choices"):
41
  assistant_message = response_body["choices"][0]["message"]["content"]
42
+ # Retorna tanto a mensagem formatada para o usuário quanto a estrutura completa para o histórico da API
43
+ return assistant_message, response_body["choices"][0]
44
  else:
45
+ return "Desculpe, ocorreu um erro ao gerar a resposta.", None
46
 
47
 
48
+ def chatbot_submit(message, chat_history_ui, chat_history_api, system_message, max_tokens_val, temperature_val, top_p_val):
 
49
  print("Updating chatbot...")
50
 
 
 
 
51
  # Chama a API da NVIDIA para gerar uma resposta
52
+ assistant_message, api_response = call_nvidia_api(chat_history_api, system_message, max_tokens_val, temperature_val, top_p_val)
53
+
54
+ # Atualiza o histórico da interface do usuário
55
+ chat_history_ui.append([message, assistant_message])
56
 
57
+ # Atualiza o histórico da API se a resposta incluir o formato esperado
58
+ if api_response:
59
+ chat_history_api.append(api_response)
60
 
61
+ return assistant_message, chat_history_ui, chat_history_api
62
 
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)
 
66
  top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7)
67
  # Gradio interface setup
68
  with gr.Blocks() as demo:
69
+ chat_history_state_ui = gr.State([])
70
+ chat_history_state_api = gr.State([])
71
+ # Outros componentes da interface...
72
  chatbot = gr.ChatInterface(
73
  fn=chatbot_submit,
74
+ inputs=[gr.Textbox(label="Your Message"), chat_history_state_ui, chat_history_state_api, system_msg, max_tokens, temperature, top_p],
75
+ outputs=[gr.Text(label="Assistant Response"), chat_history_state_ui, chat_history_state_api],
76
+ title="Chatbot Interface"
77
  description="""<div style="text-align: center; font-size: 1.5em; margin-bottom: 20px;">
78
  <strong>Explore the Capabilities of LLAMA 2 70B</strong>
79
  </div>