artificialguybr commited on
Commit
d09c482
·
verified ·
1 Parent(s): d5f5631

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -42
app.py CHANGED
@@ -63,7 +63,20 @@ def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetit
63
  print("Starting chat...")
64
  updated_history = call_nvidia_api(history, max_tokens, temperature, top_p)
65
  return updated_history, ""
66
-
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  # Gradio interface setup
68
  with gr.Blocks() as demo:
69
  with gr.Row():
@@ -88,46 +101,32 @@ with gr.Blocks() as demo:
88
  <p> <strong>HF Created by:</strong> @artificialguybr (<a href="https://twitter.com/artificialguybr">Twitter</a>)</p>
89
  <p> <strong>Discover more:</strong> <a href="https://artificialguy.com">artificialguy.com</a></p>
90
  """
91
- gr.Markdown(description)
92
- chatbot = gr.Chatbot()
93
- message = gr.Textbox(label="What do you want to chat about?", placeholder="Ask me anything.", lines=3)
94
- submit = gr.Button(value="Send message")
95
- clear = gr.Button(value="New topic")
96
- system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5)
97
- max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024, interactive=True)
98
- temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.2, interactive=True)
99
- top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7, interactive=True)
100
- chat_history_state = gr.State([])
101
-
102
- # Ajuste na definição da função update_chatbot para aceitar os valores atualizados
103
- def update_chatbot(message, chat_history, system_message, max_tokens, temperature, top_p): # Inclua os novos parâmetros aqui
104
- print("Updating chatbot...")
105
- if not chat_history or (chat_history and chat_history[-1]["role"] != "user"):
106
- chat_history = user(message, chat_history, system_message if not chat_history else None)
107
- else:
108
- chat_history = user(message, chat_history)
109
- chat_history, _ = chat(chat_history, system_message, max_tokens, temperature, top_p, 40, 1.1) # Use os parâmetros atualizados aqui
110
-
111
- formatted_chat_history = []
112
- for user_msg, assistant_msg in zip([msg["content"].strip() for msg in chat_history if msg["role"] == "user"],
113
- [msg["content"].strip() for msg in chat_history if msg["role"] == "assistant"]):
114
- if user_msg or assistant_msg:
115
- formatted_chat_history.append([user_msg, assistant_msg])
116
-
117
- return formatted_chat_history, chat_history, ""
118
-
119
- # Ajuste na chamada do botão submit para incluir os novos inputs
120
- submit.click(
121
- fn=update_chatbot,
122
- inputs=[message, chat_history_state, system_msg, max_tokens, temperature, top_p], # Inclua os novos inputs aqui
123
- outputs=[chatbot, chat_history_state, message]
124
- )
125
-
126
-
127
- clear.click(
128
- fn=clear_chat,
129
- inputs=[chat_history_state, message],
130
- outputs=[chat_history_state, message]
131
- )
132
 
133
  demo.launch()
 
63
  print("Starting chat...")
64
  updated_history = call_nvidia_api(history, max_tokens, temperature, top_p)
65
  return updated_history, ""
66
+
67
+ def update_chatbot(message, chat_history, system_message, max_tokens, temperature, top_p):
68
+ if not chat_history or (chat_history and chat_history[-1]["role"] != "user"):
69
+ chat_history = user(message, chat_history, system_message if not chat_history else None)
70
+ else:
71
+ chat_history = user(message, chat_history)
72
+ chat_history = call_nvidia_api(chat_history, max_tokens, temperature, top_p)
73
+
74
+ formatted_chat_history = []
75
+ for msg in chat_history:
76
+ if msg["role"] == "user" or msg["role"] == "assistant":
77
+ formatted_chat_history.append([msg["content"].strip()])
78
+
79
+ return formatted_chat_history, chat_history
80
  # Gradio interface setup
81
  with gr.Blocks() as demo:
82
  with gr.Row():
 
101
  <p> <strong>HF Created by:</strong> @artificialguybr (<a href="https://twitter.com/artificialguybr">Twitter</a>)</p>
102
  <p> <strong>Discover more:</strong> <a href="https://artificialguy.com">artificialguy.com</a></p>
103
  """
104
+ gr.Markdown(description)
105
+ chatbot = gr.Chatbot()
106
+ message = gr.Textbox(label="What do you want to chat about?", placeholder="Ask me anything.", lines=3)
107
+ system_msg = gr.Textbox(BASE_SYSTEM_MESSAGE, label="System Message", placeholder="System prompt.", lines=5, visible=False)
108
+ max_tokens = gr.Slider(20, 1024, label="Max Tokens", step=20, value=1024, interactive=True, visible=False)
109
+ temperature = gr.Slider(0.0, 1.0, label="Temperature", step=0.1, value=0.2, interactive=True, visible=False)
110
+ top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.7, interactive=True, visible=False)
111
+ chat_history_state = gr.State([])
112
+
113
+ submit = gr.Button(value="Send message")
114
+ clear = gr.Button(value="New topic")
115
+
116
+ additional_inputs = gr.Accordion("Additional Inputs", open=False)
117
+ with additional_inputs:
118
+ gr.Row([system_msg, max_tokens, temperature, top_p])
119
+
120
+ submit.click(
121
+ fn=update_chatbot,
122
+ inputs=[message, chat_history_state, system_msg, max_tokens, temperature, top_p],
123
+ outputs=[chatbot, chat_history_state]
124
+ )
125
+
126
+ clear.click(
127
+ fn=clear_chat,
128
+ inputs=[chat_history_state],
129
+ outputs=[chatbot, chat_history_state]
130
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  demo.launch()