Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,35 @@ headers = {
|
|
15 |
|
16 |
BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def clear_chat(chat_history_state, chat_message):
|
19 |
print("Clearing chat...")
|
20 |
chat_history_state = []
|
@@ -66,6 +95,52 @@ def call_api(history, max_tokens, temperature, top_p, system_message="", seed=42
|
|
66 |
print(f"Full API response: {full_response}")
|
67 |
return full_response
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
71 |
print("Starting chat...")
|
|
|
15 |
|
16 |
BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
|
17 |
|
18 |
+
def clear_chat(chat_history_state, chat_message):
|
19 |
+
print("Clearing chat...")
|
20 |
+
chat_history_state = []
|
21 |
+
chat_message = ''
|
22 |
+
return chat_history_state, chat_message
|
23 |
+
|
24 |
+
def user(message, history):
|
25 |
+
print(f"User message: {message}")
|
26 |
+
history = history or []
|
27 |
+
history.append({"role": "user", "content": message})
|
28 |
+
return history
|
29 |
+
|
30 |
+
import gradio as gr
|
31 |
+
import requests
|
32 |
+
import json
|
33 |
+
import os
|
34 |
+
|
35 |
+
# Definir variáveis de ambiente ou substituir com sua chave de API real
|
36 |
+
API_KEY = os.getenv('API_KEY')
|
37 |
+
INVOKE_URL = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/df2bee43-fb69-42b9-9ee5-f4eabbeaf3a8"
|
38 |
+
|
39 |
+
headers = {
|
40 |
+
"Authorization": f"Bearer {API_KEY}",
|
41 |
+
"accept": "text/event-stream",
|
42 |
+
"content-type": "application/json",
|
43 |
+
}
|
44 |
+
|
45 |
+
BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
|
46 |
+
|
47 |
def clear_chat(chat_history_state, chat_message):
|
48 |
print("Clearing chat...")
|
49 |
chat_history_state = []
|
|
|
95 |
print(f"Full API response: {full_response}")
|
96 |
return full_response
|
97 |
|
98 |
+
def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
99 |
+
print("Starting chat...")
|
100 |
+
system_message_to_use = system_message if system_message.strip() else BASE_SYSTEM_MESSAGE
|
101 |
+
if history and history[-1]["role"] == "user":
|
102 |
+
history = user(history[-1]["content"], history)
|
103 |
+
else:
|
104 |
+
history.append({"role": "system", "content": system_message_to_use})
|
105 |
+
assistant_response = call_api(history, max_tokens, temperature, top_p)
|
106 |
+
if assistant_response:
|
107 |
+
history.append({"role": "assistant", "content": assistant_response})
|
108 |
+
return history, "", ""
|
109 |
+
|
110 |
+
# Gradio interface setup
|
111 |
+
with gr.Blocks() as demo:
|
112 |
+
with gr.Row():
|
113 |
+
with gr.Column():
|
114 |
+
gr.Markdown("## Your Chatbot Interface")
|
115 |
+
chatbot = gr.Chatbot()
|
116 |
+
message = gr.Textbox(label="What do you want to chat about?", placeholder="Ask me anything.", lines=3)
|
117 |
+
submit = gr.Button(value="Send message")
|
118 |
+
clear = gr.Button(value="New topic")
|
119 |
+
system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
|
120 |
+
max_tokens = gr.Slider(20, 512, label="Max Tokens", step=20, value=500)
|
121 |
+
temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.7)
|
122 |
+
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
|
123 |
+
chat_history_state = gr.State([])
|
124 |
+
|
125 |
+
def update_chatbot(message, chat_history):
|
126 |
+
print("Updating chatbot...")
|
127 |
+
chat_history = user(message, chat_history)
|
128 |
+
chat_history, _, _ = chat(chat_history, system_msg.value, max_tokens.value, temperature.value, top_p.value, 40, 1.1)
|
129 |
+
return chat_history, chat_history, ""
|
130 |
+
|
131 |
+
submit.click(
|
132 |
+
fn=update_chatbot,
|
133 |
+
inputs=[message, chat_history_state],
|
134 |
+
outputs=[chatbot, chat_history_state, message]
|
135 |
+
)
|
136 |
+
|
137 |
+
clear.click(
|
138 |
+
fn=clear_chat,
|
139 |
+
inputs=[chat_history_state, message],
|
140 |
+
outputs=[chat_history_state, message]
|
141 |
+
)
|
142 |
+
|
143 |
+
demo.launch()
|
144 |
|
145 |
def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
146 |
print("Starting chat...")
|