alexkueck commited on
Commit
1c98f59
1 Parent(s): d130b3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -9
app.py CHANGED
@@ -7,6 +7,8 @@ import gradio as gr
7
  from langchain.evaluation import load_evaluator
8
  from pprint import pprint as print
9
  import time
 
 
10
 
11
  #from langchain.chains import LLMChain, RetrievalQA
12
  #from langchain.retrievers.self_query.base import SelfQueryRetriever
@@ -258,14 +260,27 @@ def generate_prompt_with_history_openai(prompt, history):
258
 
259
  history_openai_format.append({"role": "user", "content": prompt})
260
  return history_openai_format
261
-
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
  ##############################################
264
  ##############################################
265
  ##############################################
266
  # generate function
267
  ##############################################
268
- def generate(text, history, rag_option, model_option, k=3, temperature=0.5, max_new_tokens=4048, top_p=0.6, repetition_penalty=1.3,):
269
  #mit RAG
270
  if (rag_option is None):
271
  raise gr.Error("Retrieval Augmented Generation ist erforderlich.")
@@ -323,11 +338,23 @@ def generate(text, history, rag_option, model_option, k=3, temperature=0.5, max
323
  #return chatbot_message
324
 
325
  #Antwort als Stream ausgeben...
326
- for i in range(len(chatbot_message)):
 
 
 
 
 
 
 
327
  time.sleep(0.03)
328
- yield chatbot_message[: i+1]
329
-
330
-
 
 
 
 
 
331
 
332
 
333
  #zum Evaluieren:
@@ -354,11 +381,134 @@ evaluator = load_evaluator("criteria", criteria="conciseness", llm=evaluation_ll
354
  #Beschreibung oben in GUI
355
  ################################################
356
  print ("Start GUI")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
 
358
- description = """<strong>Information:</strong> Hier wird ein <strong>Large Language Model (LLM)</strong> mit
359
  <strong>Retrieval Augmented Generation (RAG)</strong> auf <strong>externen Daten</strong> verwendet.\n\n
360
- """
361
- css = """.toast-wrap { display: none !important } """
362
  examples=[['Was ist ChtGPT-4?'],['schreibe ein Python Programm, dass die GPT-4 API aufruft.']]
363
 
364
  additional_inputs = [
@@ -390,3 +540,4 @@ with gr.Blocks() as demo:
390
  #chatbot_stream.like(vote, None, None)
391
  chat_interface_stream.queue().launch()
392
 
 
 
7
  from langchain.evaluation import load_evaluator
8
  from pprint import pprint as print
9
  import time
10
+ from utils import *
11
+ from beschreibungen import *
12
 
13
  #from langchain.chains import LLMChain, RetrievalQA
14
  #from langchain.retrievers.self_query.base import SelfQueryRetriever
 
260
 
261
  history_openai_format.append({"role": "user", "content": prompt})
262
  return history_openai_format
263
+
264
+ ##############################################
265
+ #History - die Frage oder das File eintragen...
266
+ ##############################################
267
+ def add_text(history, text):
268
+ history = history + [(text, None)]
269
+ return history, "" #gr.Textbox(value="", interactive=False)
270
+
271
+ def add_file(history, file, prompt):
272
+ if (prompt == ""):
273
+ history = history + [((file.name,), None)]
274
+ else:
275
+ history = history + [((file.name,), None), (prompt, None)]
276
+ return history, ""
277
 
278
  ##############################################
279
  ##############################################
280
  ##############################################
281
  # generate function
282
  ##############################################
283
+ def generate(history, text, rag_option, model_option, k=3, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3,):
284
  #mit RAG
285
  if (rag_option is None):
286
  raise gr.Error("Retrieval Augmented Generation ist erforderlich.")
 
338
  #return chatbot_message
339
 
340
  #Antwort als Stream ausgeben...
341
+ #for i in range(len(chatbot_message)):
342
+ #time.sleep(0.03)
343
+ #yield chatbot_message[: i+1]
344
+
345
+ #Antwort als Stream ausgeben...
346
+ history[-1][1] = ""
347
+ for character in chatbot_message:
348
+ history[-1][1] += character
349
  time.sleep(0.03)
350
+ yield history, "Generating"
351
+ if shared_state.interrupted:
352
+ shared_state.recover()
353
+ try:
354
+ yield history, "Stop: Success"
355
+ return
356
+ except:
357
+ pass
358
 
359
 
360
  #zum Evaluieren:
 
381
  #Beschreibung oben in GUI
382
  ################################################
383
  print ("Start GUI")
384
+ with open("custom.css", "r", encoding="utf-8") as f:
385
+ customCSS = f.read()
386
+
387
+ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
388
+ history = gr.State([])
389
+ with gr.Row():
390
+ gr.HTML("LI Chatot")
391
+ status_display = gr.Markdown("Success", elem_id="status_display")
392
+ gr.Markdown(description_top)
393
+ with gr.Row():
394
+ with gr.Column(scale=5):
395
+ with gr.Row():
396
+ chatbot = gr.Chatbot(elem_id="chuanhu_chatbot")
397
+ with gr.Row():
398
+ with gr.Column(scale=12):
399
+ user_input = gr.Textbox(
400
+ show_label=False, placeholder="Gib hier deinen Prompt ein...",
401
+ container=False
402
+ )
403
+ with gr.Column(min_width=70, scale=1):
404
+ submitBtn = gr.Button("Senden")
405
+ with gr.Column(min_width=70, scale=1):
406
+ cancelBtn = gr.Button("Stop")
407
+ with gr.Row():
408
+ emptyBtn = gr.ClearButton( [user_input, chatbot], value="🧹 Neue Session")
409
+ btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
410
+
411
+ with gr.Column():
412
+ with gr.Column(min_width=50, scale=1):
413
+ with gr.Tab(label="Parameter Einstellung"):
414
+ gr.Markdown("# Parameters")
415
+ rag_option = gr.Radio(["Aus", "An"], label="RAG - LI Erweiterungen", value = "Aus")
416
+ model_option = gr.Radio(["HuggingFace1", "HuggingFace2"], label="Modellauswahl", value = "HuggingFace1")
417
+
418
+ top_p = gr.Slider(
419
+ minimum=-0,
420
+ maximum=1.0,
421
+ value=0.95,
422
+ step=0.05,
423
+ interactive=True,
424
+ label="Top-p",
425
+ )
426
+ temperature = gr.Slider(
427
+ minimum=0.1,
428
+ maximum=2.0,
429
+ value=1,
430
+ step=0.1,
431
+ interactive=True,
432
+ label="Temperature",
433
+ )
434
+ max_length_tokens = gr.Slider(
435
+ minimum=0,
436
+ maximum=512,
437
+ value=512,
438
+ step=8,
439
+ interactive=True,
440
+ label="Max Generation Tokens",
441
+ )
442
+ max_context_length_tokens = gr.Slider(
443
+ minimum=0,
444
+ maximum=4096,
445
+ value=2048,
446
+ step=128,
447
+ interactive=True,
448
+ label="Max History Tokens",
449
+ )
450
+ repetition_penalty=gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Strafe für wiederholte Tokens", visible=True)
451
+ anzahl_docs = gr.Slider(label="Anzahl Vergleichsdokumente", value=3, minimum=1, maximum=10, step=1, interactive=True, info="wie viele Dokumententeile aus dem Vektorstore an den prompt gehängt werden", visible=True),
452
+
453
+ gr.Markdown(description)
454
+
455
+ #Argumente für generate Funktion als Input
456
+ predict_args = dict(
457
+ fn=generate,
458
+ inputs=[
459
+ user_input,
460
+ chatbot,
461
+ #history,
462
+ rag_option,
463
+ model_option,
464
+ anzahl_docs,
465
+ top_p,
466
+ temperature,
467
+ max_length_tokens,
468
+ max_context_length_tokens,
469
+ ],
470
+ outputs=[ chatbot, status_display], #[ chatbot, history, status_display],
471
+ show_progress=True,
472
+ )
473
+
474
+
475
+ reset_args = dict(
476
+ fn=reset_textbox, inputs=[], outputs=[user_input, status_display]
477
+ )
478
+
479
+ # Chatbot
480
+ transfer_input_args = dict(
481
+ fn=add_text, inputs=[ chatbot, user_input], outputs=[chatbot, user_input], show_progress=True
482
+ )
483
+
484
+ predict_event1 = user_input.submit(**transfer_input_args ,queue=False,).then(**predict_args)
485
+ predict_event3 = btn.upload(add_file, [chatbot, btn, user_input], [chatbot, user_input],queue=False, ).then(**predict_args)
486
+ predict_event2 = submitBtn.click(**transfer_input_args,queue=False,).then(**predict_args)
487
+
488
+ cancelBtn.click(
489
+ cancel_outputing, [], [status_display],
490
+ cancels=[
491
+ predict_event1,predict_event2, predict_event3
492
+ ]
493
+ )
494
+ demo.title = "LI-ChatBot"
495
+
496
+ demo.queue().launch(debug=True)
497
+
498
+
499
+
500
+
501
+
502
+
503
+
504
+
505
+
506
+ """
507
 
508
+ description = <strong>Information:</strong> Hier wird ein <strong>Large Language Model (LLM)</strong> mit
509
  <strong>Retrieval Augmented Generation (RAG)</strong> auf <strong>externen Daten</strong> verwendet.\n\n
510
+
511
+ css = .toast-wrap { display: none !important }
512
  examples=[['Was ist ChtGPT-4?'],['schreibe ein Python Programm, dass die GPT-4 API aufruft.']]
513
 
514
  additional_inputs = [
 
540
  #chatbot_stream.like(vote, None, None)
541
  chat_interface_stream.queue().launch()
542
 
543
+ """