Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -321,65 +321,69 @@ def create_assistant_suche_hf(chatbot, prompt):
|
|
| 321 |
#########################################################
|
| 322 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 323 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 324 |
-
def generate_auswahl(prompt_in, file, file_history, chatbot, history, rag_option, model_option, openai_api_key, k=3, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3,top_k=35, websuche="Aus"):
|
| 325 |
-
global
|
| 326 |
-
#wenn
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
splits = document_loading_splitting()
|
| 339 |
-
document_storage_chroma(splits)
|
| 340 |
-
db = document_retrieval_chroma2()
|
| 341 |
-
print("db aktiv!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 342 |
-
#else: #unnötig, da wenn Vektorstor einmal für alle user eingerichtet, wer weiter besthen bleiben kann - die unterschiedlichen Propmt werden dann später je nach rag_option erzeugt
|
| 343 |
-
#db=None
|
| 344 |
-
#splittet = False #sonst würde es für alle User wieder ausgeschaltet - Alternative: gr.State(False) dazu anlegen
|
| 345 |
-
|
| 346 |
-
#kein Bild hochgeladen -> auf Text antworten...
|
| 347 |
-
status = "Antwort der KI ..."
|
| 348 |
-
if (file == None and file_history == None):
|
| 349 |
-
result, status = generate_text(prompt, chatbot, history, rag_option, model_option, openai_api_key, db, websuche, k=3, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3, top_k=3)
|
| 350 |
-
history = history + [[prompt, result]]
|
| 351 |
-
else:
|
| 352 |
-
#Es wurde ein File neu angehängt -> wenn prompt dazu, das Bild analysieren
|
| 353 |
-
#das history_fiel muss neu gesetzt werden
|
| 354 |
-
if (file != None):
|
| 355 |
-
# file_history wird neu gesetzt in der Rückgabe dieser Funktion...
|
| 356 |
-
neu_file = file
|
| 357 |
-
|
| 358 |
-
#herausfinden, ob Bild oder Dokument...
|
| 359 |
-
ext = analyze_file(neu_file)
|
| 360 |
-
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 361 |
-
result= generate_text_zu_bild(neu_file, prompt, k, rag_option, chatbot, history, db)
|
| 362 |
-
else:
|
| 363 |
-
result = generate_text_zu_doc(neu_file, prompt, k, rag_option, chatbot, history, db)
|
| 364 |
-
|
| 365 |
-
#die history erweitern - abhängig davon, ob gerade ein file hochgeladen wurde oder nicht
|
| 366 |
-
if (file != None):
|
| 367 |
-
history = history + [[(file,), None],[prompt, result]]
|
| 368 |
-
else:
|
| 369 |
history = history + [[prompt, result]]
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
##################################################
|
| 384 |
#zu einem Text-Prompt ein Bild via Stable Diffusion generieren
|
| 385 |
def generate_bild(prompt, chatbot, model_option_zeichnen='HuggingFace', temperature=0.5, max_new_tokens=4048,top_p=0.6, repetition_penalty=1.3):
|
|
@@ -668,28 +672,16 @@ def custom_css():
|
|
| 668 |
def validate_input(user_input, state):
|
| 669 |
user_input_hashed = hash_input(user_input.lower())
|
| 670 |
if user_input_hashed == correct_hash:
|
| 671 |
-
|
| 672 |
return "Richtig! Weiter gehts... ", ""
|
| 673 |
else:
|
| 674 |
return "Falsche Antwort!", None
|
| 675 |
|
| 676 |
-
def launch_main_app(_):
|
| 677 |
-
# Schließt das Validierungs-Interface und startet die Hauptanwendung
|
| 678 |
-
validation_interface.close()
|
| 679 |
-
demo.queue(default_concurrency_limit=15).launch(debug=True)
|
| 680 |
|
| 681 |
#############################################################################################
|
| 682 |
# Start Gui Vorabfrage
|
| 683 |
# Validierungs-Interface - Bots weghalten...
|
| 684 |
print ("Start GUI Vorabfrage")
|
| 685 |
-
"""
|
| 686 |
-
validation_interface = gr.Interface(
|
| 687 |
-
fn=validate_input,
|
| 688 |
-
inputs=gr.Textbox(label="Bitte tippen Sie das oben im Moodle Kurs angegebene Wort ein, um zu beweisen, dass Sie kein Bot sind."),
|
| 689 |
-
outputs=[gr.Text(label="Überprüfungsergebnis")],
|
| 690 |
-
live=True # Aktiviert die Live-Aktualisierung ohne Submit-Button
|
| 691 |
-
)
|
| 692 |
-
"""
|
| 693 |
#################################################################################################
|
| 694 |
print ("Start GUI Hauptanwendung")
|
| 695 |
with open("custom.css", "r", encoding="utf-8") as f:
|
|
@@ -703,6 +695,8 @@ additional_inputs = [
|
|
| 703 |
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)
|
| 704 |
]
|
| 705 |
with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
|
|
|
|
|
| 706 |
#Session Variablen, um Weete zu speichern, auch wenn die Felder in der GUI bereits wieder leer sind
|
| 707 |
# history parallel zu chatbot speichern - da in chatbot bei Bildern zum Anzeigen in der GUI die Bilder speziell formatiert werden,
|
| 708 |
# für die Übergabe an die ki aber der Pfad zum Bild behalten werden muss - was in der history der Fall ist!
|
|
@@ -734,12 +728,11 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 734 |
user_input = gr.Textbox(label="Bitte tippen Sie das oben im Moodle Kurs angegebene Wort ein, um zu beweisen, dass Sie kein Bot sind.")
|
| 735 |
validate_btn = gr.Button("Überprüfen")
|
| 736 |
validation_result = gr.Text(label="Validierungsergebnis")
|
| 737 |
-
state = gr.State(visible=False) # Zustand zur Steuerung der Sichtbarkeit der Hauptanwendung
|
| 738 |
|
| 739 |
-
with gr.Row(
|
| 740 |
#gr.HTML("LI Chatot")
|
| 741 |
status_display = gr.Markdown("Antwort der KI ...", visible = True) #, elem_id="status_display")
|
| 742 |
-
with gr.Row(
|
| 743 |
with gr.Column(scale=5):
|
| 744 |
with gr.Row():
|
| 745 |
chatbot = gr.Chatbot(elem_id="li-chat",show_copy_button=True)
|
|
@@ -827,11 +820,11 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 827 |
# Tab zum Zeichnen mit Stable Diffusion
|
| 828 |
################################################
|
| 829 |
with gr.Tab("LI Zeichnen"):
|
| 830 |
-
with gr.Row(
|
| 831 |
gr.HTML("Lass den KI-Bot deine Ideen zeichnen...")
|
| 832 |
status_display2 = gr.Markdown("Success", visible = False, elem_id="status_display")
|
| 833 |
#gr.Markdown(description2)
|
| 834 |
-
with gr.Row(
|
| 835 |
with gr.Column(scale=5):
|
| 836 |
with gr.Row():
|
| 837 |
chatbot_bild = gr.Chatbot(elem_id="li-zeichnen",show_copy_button=True, show_share_button=True)
|
|
@@ -914,7 +907,8 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 914 |
max_context_length_tokens,
|
| 915 |
repetition_penalty,
|
| 916 |
top_k,
|
| 917 |
-
websuche
|
|
|
|
| 918 |
],
|
| 919 |
outputs=[chatbot, history, attached_file, attached_file_history, status_display],
|
| 920 |
show_progress=True,
|
|
@@ -955,6 +949,7 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 955 |
user_question2,
|
| 956 |
chatbot_bild,
|
| 957 |
model_option_zeichnen,
|
|
|
|
| 958 |
#additional_inputs,
|
| 959 |
],
|
| 960 |
outputs=[chatbot_bild, status_display2], #[chatbot, history, status_display]
|
|
@@ -1017,7 +1012,7 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 1017 |
|
| 1018 |
|
| 1019 |
demo.title = "KKG-ChatBot"
|
| 1020 |
-
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
|
|
|
| 321 |
#########################################################
|
| 322 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 323 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 324 |
+
def generate_auswahl(prompt_in, file, file_history, chatbot, history, rag_option, model_option, openai_api_key, k=3, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3,top_k=35, websuche="Aus", validate=False):
|
| 325 |
+
global db
|
| 326 |
+
#nur wenn man sich validiert hat, kann die Anwendung los legen
|
| 327 |
+
if (validate):
|
| 328 |
+
#wenn RAG angeschaltet - Vektorstore initialisieren
|
| 329 |
+
#aber nur, wenn es noch nicht geshehen ist (splittet = False)
|
| 330 |
+
#falls schon ein File hochgeladen wurde, ist es in history_file gespeichert - falls ein neues File hochgeladen wurde, wird es anschließend neu gesetzt
|
| 331 |
+
neu_file = file_history
|
| 332 |
+
|
| 333 |
+
#prompt normalisieren bevor er an die KIs geht
|
| 334 |
+
prompt = normalise_prompt(prompt_in)
|
| 335 |
+
|
| 336 |
+
if (rag_option == "An"):
|
| 337 |
+
#muss nur einmal ausgeführt werden...
|
| 338 |
+
while db == None:
|
| 339 |
+
print("db neu aufbauen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1")
|
| 340 |
+
splits = document_loading_splitting()
|
| 341 |
+
document_storage_chroma(splits)
|
| 342 |
+
db = document_retrieval_chroma2()
|
| 343 |
+
print("db aktiv!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 344 |
+
#else: #unnötig, da wenn Vektorstor einmal für alle user eingerichtet, wer weiter besthen bleiben kann - die unterschiedlichen Propmt werden dann später je nach rag_option erzeugt
|
| 345 |
+
#db=None
|
| 346 |
+
#splittet = False #sonst würde es für alle User wieder ausgeschaltet - Alternative: gr.State(False) dazu anlegen
|
| 347 |
|
| 348 |
+
#kein Bild hochgeladen -> auf Text antworten...
|
| 349 |
+
status = "Antwort der KI ..."
|
| 350 |
+
if (file == None and file_history == None):
|
| 351 |
+
result, status = generate_text(prompt, chatbot, history, rag_option, model_option, openai_api_key, db, websuche, k=3, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3, top_k=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
history = history + [[prompt, result]]
|
| 353 |
+
else:
|
| 354 |
+
#Es wurde ein File neu angehängt -> wenn prompt dazu, das Bild analysieren
|
| 355 |
+
#das history_fiel muss neu gesetzt werden
|
| 356 |
+
if (file != None):
|
| 357 |
+
# file_history wird neu gesetzt in der Rückgabe dieser Funktion...
|
| 358 |
+
neu_file = file
|
| 359 |
+
|
| 360 |
+
#herausfinden, ob Bild oder Dokument...
|
| 361 |
+
ext = analyze_file(neu_file)
|
| 362 |
+
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 363 |
+
result= generate_text_zu_bild(neu_file, prompt, k, rag_option, chatbot, history, db)
|
| 364 |
+
else:
|
| 365 |
+
result = generate_text_zu_doc(neu_file, prompt, k, rag_option, chatbot, history, db)
|
| 366 |
+
|
| 367 |
+
#die history erweitern - abhängig davon, ob gerade ein file hochgeladen wurde oder nicht
|
| 368 |
+
if (file != None):
|
| 369 |
+
history = history + [[(file,), None],[prompt, result]]
|
| 370 |
+
else:
|
| 371 |
+
history = history + [[prompt, result]]
|
| 372 |
+
|
| 373 |
+
chatbot[-1][1] = ""
|
| 374 |
+
for character in result:
|
| 375 |
+
chatbot[-1][1] += character
|
| 376 |
+
time.sleep(0.03)
|
| 377 |
+
yield chatbot, history, None, neu_file, status
|
| 378 |
+
if shared_state.interrupted:
|
| 379 |
+
shared_state.recover()
|
| 380 |
+
try:
|
| 381 |
+
yield chatbot, history, None, neu_file, "Stop: Success"
|
| 382 |
+
except:
|
| 383 |
+
pass
|
| 384 |
+
else: #noch nicht validiert
|
| 385 |
+
return chatbot, history, None, file_history, "Erst validieren bitte!"
|
| 386 |
+
|
| 387 |
##################################################
|
| 388 |
#zu einem Text-Prompt ein Bild via Stable Diffusion generieren
|
| 389 |
def generate_bild(prompt, chatbot, model_option_zeichnen='HuggingFace', temperature=0.5, max_new_tokens=4048,top_p=0.6, repetition_penalty=1.3):
|
|
|
|
| 672 |
def validate_input(user_input, state):
|
| 673 |
user_input_hashed = hash_input(user_input.lower())
|
| 674 |
if user_input_hashed == correct_hash:
|
| 675 |
+
validate = True
|
| 676 |
return "Richtig! Weiter gehts... ", ""
|
| 677 |
else:
|
| 678 |
return "Falsche Antwort!", None
|
| 679 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 680 |
|
| 681 |
#############################################################################################
|
| 682 |
# Start Gui Vorabfrage
|
| 683 |
# Validierungs-Interface - Bots weghalten...
|
| 684 |
print ("Start GUI Vorabfrage")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
#################################################################################################
|
| 686 |
print ("Start GUI Hauptanwendung")
|
| 687 |
with open("custom.css", "r", encoding="utf-8") as f:
|
|
|
|
| 695 |
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)
|
| 696 |
]
|
| 697 |
with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
| 698 |
+
#validiert speichern
|
| 699 |
+
validate = gr.State(False)
|
| 700 |
#Session Variablen, um Weete zu speichern, auch wenn die Felder in der GUI bereits wieder leer sind
|
| 701 |
# history parallel zu chatbot speichern - da in chatbot bei Bildern zum Anzeigen in der GUI die Bilder speziell formatiert werden,
|
| 702 |
# für die Übergabe an die ki aber der Pfad zum Bild behalten werden muss - was in der history der Fall ist!
|
|
|
|
| 728 |
user_input = gr.Textbox(label="Bitte tippen Sie das oben im Moodle Kurs angegebene Wort ein, um zu beweisen, dass Sie kein Bot sind.")
|
| 729 |
validate_btn = gr.Button("Überprüfen")
|
| 730 |
validation_result = gr.Text(label="Validierungsergebnis")
|
|
|
|
| 731 |
|
| 732 |
+
with gr.Row():
|
| 733 |
#gr.HTML("LI Chatot")
|
| 734 |
status_display = gr.Markdown("Antwort der KI ...", visible = True) #, elem_id="status_display")
|
| 735 |
+
with gr.Row():
|
| 736 |
with gr.Column(scale=5):
|
| 737 |
with gr.Row():
|
| 738 |
chatbot = gr.Chatbot(elem_id="li-chat",show_copy_button=True)
|
|
|
|
| 820 |
# Tab zum Zeichnen mit Stable Diffusion
|
| 821 |
################################################
|
| 822 |
with gr.Tab("LI Zeichnen"):
|
| 823 |
+
with gr.Row():
|
| 824 |
gr.HTML("Lass den KI-Bot deine Ideen zeichnen...")
|
| 825 |
status_display2 = gr.Markdown("Success", visible = False, elem_id="status_display")
|
| 826 |
#gr.Markdown(description2)
|
| 827 |
+
with gr.Row():
|
| 828 |
with gr.Column(scale=5):
|
| 829 |
with gr.Row():
|
| 830 |
chatbot_bild = gr.Chatbot(elem_id="li-zeichnen",show_copy_button=True, show_share_button=True)
|
|
|
|
| 907 |
max_context_length_tokens,
|
| 908 |
repetition_penalty,
|
| 909 |
top_k,
|
| 910 |
+
websuche,
|
| 911 |
+
validate
|
| 912 |
],
|
| 913 |
outputs=[chatbot, history, attached_file, attached_file_history, status_display],
|
| 914 |
show_progress=True,
|
|
|
|
| 949 |
user_question2,
|
| 950 |
chatbot_bild,
|
| 951 |
model_option_zeichnen,
|
| 952 |
+
validate
|
| 953 |
#additional_inputs,
|
| 954 |
],
|
| 955 |
outputs=[chatbot_bild, status_display2], #[chatbot, history, status_display]
|
|
|
|
| 1012 |
|
| 1013 |
|
| 1014 |
demo.title = "KKG-ChatBot"
|
| 1015 |
+
demo.queue(default_concurrency_limit=15).launch(debug=True)
|
| 1016 |
|
| 1017 |
|
| 1018 |
|