giannantonio15 commited on
Commit
e2eaf2f
1 Parent(s): e14a22a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -47
app.py CHANGED
@@ -129,9 +129,15 @@ memory_bandi_sistema_puglia = None
129
  stream_response = None
130
  divDocumenti = None
131
 
 
 
 
 
 
 
 
132
 
133
-
134
- def select_initial_collection():
135
  global current_collection
136
  global retriever
137
 
@@ -155,7 +161,7 @@ def select_initial_collection():
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,12 +226,12 @@ def select_collection(evt: gr.SelectData):
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,12 +249,12 @@ def select_chat_mode():
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,7 +306,7 @@ def set_chat_engine():
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,7 +347,7 @@ def html_escape(text):
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,7 +362,28 @@ def reset():
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,8 +391,8 @@ def user(userMessage, history):
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,46 +497,15 @@ def bot(history):
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
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
501
  bot, chatbot, [chatbot, divDocumenti]
502
  )
503
- clear = gr.ClearButton([msg, chatbot], elem_id="btnClear")
504
- clear.click(fn=reset, outputs=output)
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()
 
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
 
140
+ def select_initial_collection():
 
141
  global current_collection
142
  global retriever
143
 
 
161
  current_collection = "BANDI_SISTEMA_PUGLIA"
162
  return "collezione settata"
163
 
164
+ def select_collection(evt: gr.SelectData):
165
  global current_collection
166
  global retriever
167
  global chat_engine_bandi
 
226
 
227
  return "<div class='alert alert-success' role='alert'> Collezione "+selected_collection+" selezionata </div>"
228
 
229
+ def select_response_mode(evt: gr.SelectData):
230
  global current_response_mode
231
  current_response_mode = evt.value
232
  return "<div class='alert alert-success' role='alert'>"+current_response_mode+" selezionato </div>"
233
 
234
+ def select_chat_mode():
235
  global current_chat_mode
236
  global memory_bandi
237
  global memory_bandi_sistema_puglia
 
249
 
250
  return "<div class='alert alert-success' role='alert'>Hai selezionato la modalità "+current_chat_mode+" </div>"
251
 
252
+ def select_standard_mode():
253
  global current_chat_mode
254
  current_chat_mode = "STANDARD"
255
  return "<div class='alert alert-success' role='alert'>Hai selezionato la modalità "+current_chat_mode+" </div>"
256
 
257
+ def set_chat_engine():
258
  global chat_engine_bandi
259
  global chat_engine_bandi_sistema_puglia
260
  global memory_bandi
 
306
  "\n", llm=llm, memory=memory_bandi_sistema_puglia, prefix_messages=[])
307
 
308
 
309
+ def html_escape(text):
310
  html_entities = {
311
  'à': '&agrave;',
312
  'è': '&egrave;',
 
347
  }
348
  return ''.join(html_entities.get(c, c) for c in text)
349
 
350
+ def reset():
351
  global chat_engine_bandi
352
  global chat_engine_bandi_sistema_puglia
353
  global memory_bandi
 
362
  token_count_bandi_sistema_puglia = 0
363
  return "<div class='alert alert-success' role='alert'>Cronologia chat eliminata</div>"
364
 
365
+ with gr.Blocks(css=css, head=head) as demo:
366
+
367
+ with gr.Row():
368
+ output = gr.HTML()
369
+ with gr.Row(elem_id="buttonChat"):
370
+ gr.Button("STANDARD", size="sm").click(fn=select_standard_mode, outputs=output)
371
+ gr.Button("CHAT",size="sm").click(fn=select_chat_mode, outputs=output)
372
+
373
+ chatbot = gr.Chatbot(elem_id="chatbot", container=False)
374
+
375
+ with gr.Column(elem_id="colonnaElementi"):
376
+ gr.Dropdown(
377
+ ["BANDI_SISTEMA_PUGLIA","BANDI"], label="Collezione di documenti", info="", container=False, interactive=True, value="BANDI_SISTEMA_PUGLIA", elem_id="dropdown"
378
+ ).select(fn=select_collection, outputs=output)
379
+
380
+ 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),
381
+ divDocumenti = gr.HTML("<div id='divDocumenti'></div>")
382
+ msg = gr.Textbox(elem_id="textBox", container=False)
383
+ clear = gr.ClearButton([msg, chatbot], elem_id="btnClear")
384
+ clear.click(fn=reset, outputs=output)
385
+
386
+ def user(userMessage, history):
387
  global user_message
388
  user_message = userMessage
389
  if history is None:
 
391
 
392
  return "", history + [[user_message, None]]
393
 
394
+ @spaces.GPU(duration=120)
395
+ def bot(history):
396
  global chat_engine_bandi
397
  global chat_engine_bandi_sistema_puglia
398
  global memory_bandi
 
497
  torch.cuda.reset_max_memory_allocated()
498
  torch.cuda.reset_max_memory_cached()
499
 
 
 
500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
502
  bot, chatbot, [chatbot, divDocumenti]
503
  )
 
 
504
 
505
+ demo.queue()
506
+ demo.launch(debug=True, share=True)
507
+ select_initial_collection()
508
+ set_chat_engine()
 
 
 
509
 
510
  if __name__ == "__main__":
511
  main()