Commit
·
f89bf86
1
Parent(s):
d99c739
Remove unnecessary code for chatbot interface
Browse files
app.py
CHANGED
@@ -15,35 +15,6 @@ 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 = []
|
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 = []
|
@@ -121,54 +92,7 @@ with gr.Blocks() as demo:
|
|
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...")
|
147 |
-
system_message_to_use = system_message if system_message.strip() else BASE_SYSTEM_MESSAGE
|
148 |
-
if history and history[-1]["role"] == "user":
|
149 |
-
history = user(history[-1]["content"], history)
|
150 |
-
else:
|
151 |
-
history.append({"role": "system", "content": system_message_to_use})
|
152 |
-
assistant_response = call_api(history, max_tokens, temperature, top_p)
|
153 |
-
if assistant_response:
|
154 |
-
history.append({"role": "assistant", "content": assistant_response})
|
155 |
-
return history, "", ""
|
156 |
|
157 |
-
# Gradio interface setup
|
158 |
-
with gr.Blocks() as demo:
|
159 |
-
with gr.Row():
|
160 |
-
with gr.Column():
|
161 |
-
gr.Markdown("## Your Chatbot Interface")
|
162 |
-
chatbot = gr.Chatbot()
|
163 |
-
message = gr.Textbox(label="What do you want to chat about?", placeholder="Ask me anything.", lines=3)
|
164 |
-
submit = gr.Button(value="Send message")
|
165 |
-
clear = gr.Button(value="New topic")
|
166 |
-
system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
|
167 |
-
max_tokens = gr.Slider(20, 512, label="Max Tokens", step=20, value=500)
|
168 |
-
temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.7)
|
169 |
-
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
|
170 |
-
chat_history_state = gr.State([])
|
171 |
-
|
172 |
def update_chatbot(message, chat_history):
|
173 |
print("Updating chatbot...")
|
174 |
chat_history = user(message, chat_history)
|
|
|
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 = []
|
|
|
92 |
temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.7)
|
93 |
top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
|
94 |
chat_history_state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
def update_chatbot(message, chat_history):
|
97 |
print("Updating chatbot...")
|
98 |
chat_history = user(message, chat_history)
|