asthaa30 commited on
Commit
1d21262
·
verified ·
1 Parent(s): 5fb7b3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py CHANGED
@@ -178,6 +178,101 @@ def respond(message, history, system_message):
178
  # Update the system prompt to be more relevant to maritime legal assistance
179
  system_prompt = "You are a maritime legal assistant with expertise in maritime law. Provide detailed legal advice and information based on maritime legal principles and regulations."
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  demo = gr.ChatInterface(
182
  respond,
183
  additional_inputs=[
 
178
  # Update the system prompt to be more relevant to maritime legal assistance
179
  system_prompt = "You are a maritime legal assistant with expertise in maritime law. Provide detailed legal advice and information based on maritime legal principles and regulations."
180
 
181
+ # Use gr.Blocks and gr.Chatbot for Gradio 3.x
182
+ with gr.Blocks() as demo:
183
+ chatbot = gr.Chatbot()
184
+ system_message_input = gr.Textbox(value=system_prompt, label="System message")
185
+ message_input = gr.Textbox(label="Message")
186
+
187
+ def process_message(message, history, system_message):
188
+ response_gen = respond(message, history, system_message)
189
+ return list(response_gen), history
190
+
191
+ message_input.submit(process_message, [message_input, chatbot, system_message_input], [chatbot, chatbot])
192
+
193
+ if __name__ == "__main__":
194
+ demo.launch()
195
+
196
+ assistant_content = ""
197
+ assistant_native_message_list = []
198
+
199
+ while True:
200
+ response_message = (
201
+ get_model_response(history, inner_history, message, system_message)
202
+ .choices[0]
203
+ .message
204
+ )
205
+
206
+ if not response_message.tool_calls and response_message.content is not None:
207
+ break
208
+
209
+ if response_message.tool_calls is not None:
210
+ assistant_native_message_list.append(response_message)
211
+ inner_history.append(response_message)
212
+
213
+ assistant_content += (
214
+ "```json\n"
215
+ + json.dumps(
216
+ [
217
+ tool_call.model_dump()
218
+ for tool_call in response_message.tool_calls
219
+ ],
220
+ indent=2,
221
+ )
222
+ + "\n```\n"
223
+ )
224
+ assistant_message = {
225
+ "role": "assistant",
226
+ "content": assistant_content,
227
+ "metadata": {"native_messages": assistant_native_message_list},
228
+ }
229
+
230
+ yield assistant_message
231
+
232
+ for tool_call in response_message.tool_calls:
233
+ function_response = call_function(tool_call, available_functions)
234
+ assistant_content += (
235
+ "```json\n"
236
+ + json.dumps(
237
+ {
238
+ "name": tool_call.function.name,
239
+ "arguments": json.loads(tool_call.function.arguments),
240
+ "response": json.loads(function_response["content"]),
241
+ },
242
+ indent=2,
243
+ )
244
+ + "\n```\n"
245
+ )
246
+ native_tool_message = {
247
+ "tool_call_id": tool_call.id,
248
+ "role": "tool",
249
+ "content": function_response["content"],
250
+ }
251
+ assistant_native_message_list.append(
252
+ native_tool_message
253
+ )
254
+ tool_message = {
255
+ "role": "assistant",
256
+ "content": assistant_content,
257
+ "metadata": {"native_messages": assistant_native_message_list},
258
+ }
259
+ yield tool_message
260
+ inner_history.append(native_tool_message)
261
+
262
+ assistant_content += response_message.content
263
+ assistant_native_message_list.append(response_message)
264
+
265
+ final_message = {
266
+ "role": "assistant",
267
+ "content": assistant_content,
268
+ "metadata": {"native_messages": assistant_native_message_list},
269
+ }
270
+
271
+ yield final_message
272
+
273
+ # Update the system prompt to be more relevant to maritime legal assistance
274
+ system_prompt = "You are a maritime legal assistant with expertise in maritime law. Provide detailed legal advice and information based on maritime legal principles and regulations."
275
+
276
  demo = gr.ChatInterface(
277
  respond,
278
  additional_inputs=[