Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
import json
|
4 |
import os
|
|
|
5 |
|
6 |
-
# API
|
7 |
-
API_KEY = os.getenv('API_KEY')
|
8 |
INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/0e349b44-440a-44e1-93e9-abe8dcb27158"
|
9 |
FETCH_URL_FORMAT = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
|
|
|
10 |
headers = {
|
11 |
"Authorization": f"Bearer {API_KEY}",
|
12 |
"Accept": "application/json",
|
13 |
"Content-Type": "application/json",
|
14 |
}
|
15 |
|
16 |
-
# Base system message
|
17 |
BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
|
18 |
|
19 |
-
def clear_chat():
|
20 |
-
"""Clears the chat history and message state."""
|
21 |
-
print("Clearing chat...")
|
22 |
-
chat_history_state.value = []
|
23 |
-
chatbot.textbox.value = ""
|
24 |
-
|
25 |
def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
26 |
-
"""
|
27 |
-
|
28 |
-
messages
|
|
|
|
|
|
|
29 |
|
30 |
payload = {
|
31 |
"messages": messages,
|
@@ -34,16 +31,14 @@ def call_nvidia_api(history, system_message, max_tokens, temperature, top_p):
|
|
34 |
"max_tokens": max_tokens,
|
35 |
"stream": False
|
36 |
}
|
37 |
-
print(f"Payload enviado: {payload}")
|
38 |
session = requests.Session()
|
39 |
response = session.post(INVOKE_URL, headers=headers, json=payload)
|
40 |
while response.status_code == 202:
|
41 |
request_id = response.headers.get("NVCF-REQID")
|
42 |
fetch_url = FETCH_URL_FORMAT + request_id
|
43 |
response = session.get(fetch_url, headers=headers)
|
44 |
-
|
45 |
response_body = response.json()
|
46 |
-
print(f"Payload recebido: {response_body}")
|
47 |
if response_body.get("choices"):
|
48 |
assistant_message = response_body["choices"][0]["message"]["content"]
|
49 |
return assistant_message
|
@@ -65,39 +60,32 @@ def chatbot_submit(message, chat_history, system_message, max_tokens_val, temper
|
|
65 |
|
66 |
return assistant_message, chat_history
|
67 |
|
68 |
-
|
69 |
-
system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
|
70 |
-
max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024)
|
71 |
-
temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.2)
|
72 |
-
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7)
|
73 |
with gr.Blocks() as demo:
|
74 |
chat_history_state = gr.State([])
|
|
|
|
|
|
|
|
|
75 |
chatbot = gr.ChatInterface(
|
76 |
fn=chatbot_submit,
|
77 |
additional_inputs=[system_msg, max_tokens, temperature, top_p],
|
78 |
title="LLAMA 70B Free Demo",
|
79 |
-
description="""
|
80 |
-
<
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
<
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
<p><strong>Discover more:</strong> <a href="https://artificialguy.com">artificialguy.com</a></p>
|
93 |
-
""",
|
94 |
submit_btn="Submit",
|
95 |
clear_btn="🗑️ Clear",
|
96 |
)
|
97 |
|
98 |
-
|
99 |
-
chat_history_state.value = []
|
100 |
-
chatbot.textbox.value = ""
|
101 |
-
|
102 |
-
chatbot.clear()
|
103 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
import os
|
4 |
+
import json
|
5 |
|
6 |
+
# Carrega a chave da API do ambiente ou define diretamente
|
7 |
+
API_KEY = os.getenv('API_KEY')
|
8 |
INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/0e349b44-440a-44e1-93e9-abe8dcb27158"
|
9 |
FETCH_URL_FORMAT = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
|
10 |
+
|
11 |
headers = {
|
12 |
"Authorization": f"Bearer {API_KEY}",
|
13 |
"Accept": "application/json",
|
14 |
"Content-Type": "application/json",
|
15 |
}
|
16 |
|
|
|
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 |
"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"]
|
44 |
return assistant_message
|
|
|
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],
|
73 |
title="LLAMA 70B Free Demo",
|
74 |
+
description="""<div style="text-align: center; font-size: 1.5em; margin-bottom: 20px;">
|
75 |
+
<strong>Explore the Capabilities of LLAMA 2 70B</strong>
|
76 |
+
</div>
|
77 |
+
<p>Llama 2 is a large language AI model capable of generating text and code in response to prompts.</p>
|
78 |
+
<p><strong>How to Use:</strong></p>
|
79 |
+
<ol>
|
80 |
+
<li>Enter your <strong>message</strong> in the textbox to start a conversation or ask a question.</li>
|
81 |
+
<li>Adjust the parameters in the "Additional Inputs" accordion to control the model's behavior.</li>
|
82 |
+
<li>Use the buttons below the chatbot to submit your query, clear the chat history, or perform other actions.</li>
|
83 |
+
</ol>
|
84 |
+
<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>
|
85 |
+
<p><strong>HF Created by:</strong> @artificialguybr (<a href="https://twitter.com/artificialguybr">Twitter</a>)</p>
|
86 |
+
<p><strong>Discover more:</strong> <a href="https://artificialguy.com">artificialguy.com</a></p>""",
|
|
|
|
|
87 |
submit_btn="Submit",
|
88 |
clear_btn="🗑️ Clear",
|
89 |
)
|
90 |
|
91 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|