giannantonio15 commited on
Commit
b3740a9
·
verified ·
1 Parent(s): 8d65aab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -53
app.py CHANGED
@@ -129,17 +129,9 @@ memory_bandi_sistema_puglia = None
129
  stream_response = None
130
  divDocumenti = None
131
 
132
- def main():
133
- setGPU()
134
- llm = setLLM()
135
- Settings.llm = llm
136
- Settings.embed_model = "local:google-bert/bert-base-multilingual-cased"
137
- embed_model = Settings.embed_model
138
- text_qa_template, refine_template = setPromptTemplate()
139
- select_initial_collection()
140
- set_chat_engine()
141
 
142
- def select_initial_collection():
 
143
  global current_collection
144
  global retriever
145
 
@@ -163,7 +155,7 @@ def main():
163
  current_collection = "BANDI_SISTEMA_PUGLIA"
164
  return "collezione settata"
165
 
166
- def select_collection(evt: gr.SelectData):
167
  global current_collection
168
  global retriever
169
  global chat_engine_bandi
@@ -228,12 +220,12 @@ def main():
228
 
229
  return "<div class='alert alert-success' role='alert'> Collezione "+selected_collection+" selezionata </div>"
230
 
231
- def select_response_mode(evt: gr.SelectData):
232
  global current_response_mode
233
  current_response_mode = evt.value
234
  return "<div class='alert alert-success' role='alert'>"+current_response_mode+" selezionato </div>"
235
 
236
- def select_chat_mode():
237
  global current_chat_mode
238
  global memory_bandi
239
  global memory_bandi_sistema_puglia
@@ -251,12 +243,12 @@ def main():
251
 
252
  return "<div class='alert alert-success' role='alert'>Hai selezionato la modalità "+current_chat_mode+" </div>"
253
 
254
- def select_standard_mode():
255
  global current_chat_mode
256
  current_chat_mode = "STANDARD"
257
  return "<div class='alert alert-success' role='alert'>Hai selezionato la modalità "+current_chat_mode+" </div>"
258
 
259
- def set_chat_engine():
260
  global chat_engine_bandi
261
  global chat_engine_bandi_sistema_puglia
262
  global memory_bandi
@@ -308,7 +300,7 @@ def main():
308
  "\n", llm=llm, memory=memory_bandi_sistema_puglia, prefix_messages=[])
309
 
310
 
311
- def html_escape(text):
312
  html_entities = {
313
  'à': '&agrave;',
314
  'è': '&egrave;',
@@ -349,7 +341,7 @@ def main():
349
  }
350
  return ''.join(html_entities.get(c, c) for c in text)
351
 
352
- def reset():
353
  global chat_engine_bandi
354
  global chat_engine_bandi_sistema_puglia
355
  global memory_bandi
@@ -364,28 +356,7 @@ def main():
364
  token_count_bandi_sistema_puglia = 0
365
  return "<div class='alert alert-success' role='alert'>Cronologia chat eliminata</div>"
366
 
367
- with gr.Blocks(css=css, head=head) as demo:
368
-
369
- with gr.Row():
370
- output = gr.HTML()
371
- with gr.Row(elem_id="buttonChat"):
372
- gr.Button("STANDARD", size="sm").click(fn=select_standard_mode, outputs=output)
373
- gr.Button("CHAT",size="sm").click(fn=select_chat_mode, outputs=output)
374
-
375
- chatbot = gr.Chatbot(elem_id="chatbot", container=False)
376
-
377
- with gr.Column(elem_id="colonnaElementi"):
378
- gr.Dropdown(
379
- ["BANDI_SISTEMA_PUGLIA","BANDI"], label="Collezione di documenti", info="", container=False, interactive=True, value="BANDI_SISTEMA_PUGLIA", elem_id="dropdown"
380
- ).select(fn=select_collection, outputs=output)
381
-
382
- gr.Radio(["compact","tree_summarize"], label="Response mode", info="Influenzerà il modo in cui il chatbot risponde", interactive=True,container=False, value="compact",elem_id="responseMode").select(fn=select_response_mode, outputs=output),
383
- divDocumenti = gr.HTML("<div id='divDocumenti'></div>")
384
- msg = gr.Textbox(elem_id="textBox", container=False)
385
- clear = gr.ClearButton([msg, chatbot], elem_id="btnClear")
386
- clear.click(fn=reset, outputs=output)
387
-
388
- def user(userMessage, history):
389
  global user_message
390
  user_message = userMessage
391
  if history is None:
@@ -393,15 +364,8 @@ def main():
393
 
394
  return "", history + [[user_message, None]]
395
 
396
- def getStreamResponse(type_engine, engine, message):
397
- response = None
398
- if(type_engine=="CHAT"):
399
- response = engine.stream_chat(message)
400
- else:
401
- response = engine.query(message)
402
- return response
403
- @spaces.GPU(duration=120)
404
- def bot(history):
405
  global chat_engine_bandi
406
  global chat_engine_bandi_sistema_puglia
407
  global memory_bandi
@@ -506,13 +470,46 @@ def main():
506
  torch.cuda.reset_max_memory_allocated()
507
  torch.cuda.reset_max_memory_cached()
508
 
 
 
509
 
510
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
511
- bot, chatbot, [chatbot, divDocumenti]
512
- )
 
 
 
 
 
 
 
 
 
 
 
 
513
 
514
- demo.queue()
515
- demo.launch(debug=True, share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516
 
517
  if __name__ == "__main__":
518
  main()
 
129
  stream_response = None
130
  divDocumenti = None
131
 
 
 
 
 
 
 
 
 
 
132
 
133
+
134
+ def select_initial_collection():
135
  global current_collection
136
  global retriever
137
 
 
155
  current_collection = "BANDI_SISTEMA_PUGLIA"
156
  return "collezione settata"
157
 
158
+ def select_collection(evt: gr.SelectData):
159
  global current_collection
160
  global retriever
161
  global chat_engine_bandi
 
220
 
221
  return "<div class='alert alert-success' role='alert'> Collezione "+selected_collection+" selezionata </div>"
222
 
223
+ def select_response_mode(evt: gr.SelectData):
224
  global current_response_mode
225
  current_response_mode = evt.value
226
  return "<div class='alert alert-success' role='alert'>"+current_response_mode+" selezionato </div>"
227
 
228
+ def select_chat_mode():
229
  global current_chat_mode
230
  global memory_bandi
231
  global memory_bandi_sistema_puglia
 
243
 
244
  return "<div class='alert alert-success' role='alert'>Hai selezionato la modalità "+current_chat_mode+" </div>"
245
 
246
+ def select_standard_mode():
247
  global current_chat_mode
248
  current_chat_mode = "STANDARD"
249
  return "<div class='alert alert-success' role='alert'>Hai selezionato la modalità "+current_chat_mode+" </div>"
250
 
251
+ def set_chat_engine():
252
  global chat_engine_bandi
253
  global chat_engine_bandi_sistema_puglia
254
  global memory_bandi
 
300
  "\n", llm=llm, memory=memory_bandi_sistema_puglia, prefix_messages=[])
301
 
302
 
303
+ def html_escape(text):
304
  html_entities = {
305
  'à': '&agrave;',
306
  'è': '&egrave;',
 
341
  }
342
  return ''.join(html_entities.get(c, c) for c in text)
343
 
344
+ def reset():
345
  global chat_engine_bandi
346
  global chat_engine_bandi_sistema_puglia
347
  global memory_bandi
 
356
  token_count_bandi_sistema_puglia = 0
357
  return "<div class='alert alert-success' role='alert'>Cronologia chat eliminata</div>"
358
 
359
+ def user(userMessage, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  global user_message
361
  user_message = userMessage
362
  if history is None:
 
364
 
365
  return "", history + [[user_message, None]]
366
 
367
+ @spaces.GPU(duration=120)
368
+ def bot(history):
 
 
 
 
 
 
 
369
  global chat_engine_bandi
370
  global chat_engine_bandi_sistema_puglia
371
  global memory_bandi
 
470
  torch.cuda.reset_max_memory_allocated()
471
  torch.cuda.reset_max_memory_cached()
472
 
473
+
474
+
475
 
476
+ def main():
477
+ setGPU()
478
+ llm = setLLM()
479
+ Settings.llm = llm
480
+ Settings.embed_model = "local:google-bert/bert-base-multilingual-cased"
481
+ embed_model = Settings.embed_model
482
+ text_qa_template, refine_template = setPromptTemplate()
483
+
484
+ with gr.Blocks(css=css, head=head) as demo:
485
+ with gr.Row():
486
+ output = gr.HTML()
487
+ with gr.Row(elem_id="buttonChat"):
488
+ gr.Button("STANDARD", size="sm").click(fn=select_standard_mode, outputs=output)
489
+ gr.Button("CHAT",size="sm").click(fn=select_chat_mode, outputs=output)
490
+ chatbot = gr.Chatbot(elem_id="chatbot", container=False)
491
 
492
+ with gr.Column(elem_id="colonnaElementi"):
493
+ gr.Dropdown(
494
+ ["BANDI_SISTEMA_PUGLIA","BANDI"], label="Collezione di documenti", info="", container=False, interactive=True, value="BANDI_SISTEMA_PUGLIA", elem_id="dropdown"
495
+ ).select(fn=select_collection, outputs=output)
496
+
497
+ gr.Radio(["compact","tree_summarize"], label="Response mode", info="Influenzerà il modo in cui il chatbot risponde", interactive=True,container=False, value="compact",elem_id="responseMode").select(fn=select_response_mode, outputs=output),
498
+ divDocumenti = gr.HTML("<div id='divDocumenti'></div>")
499
+ msg = gr.Textbox(elem_id="textBox", container=False)
500
+ clear = gr.ClearButton([msg, chatbot], elem_id="btnClear")
501
+ clear.click(fn=reset, outputs=output)
502
+
503
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
504
+ bot, chatbot, [chatbot, divDocumenti]
505
+ )
506
+
507
+ demo.queue()
508
+ demo.launch(debug=True, share=True)
509
+
510
+ select_initial_collection()
511
+ set_chat_engine()
512
+
513
 
514
  if __name__ == "__main__":
515
  main()