Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,7 +50,9 @@ _ = load_dotenv(find_dotenv())
|
|
| 50 |
#nur bei ersten Anfrage splitten der Dokumente - um die Vektordatenbank entsprechend zu füllen
|
| 51 |
#splittet = False
|
| 52 |
#DB für Vektorstore
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
#############################################
|
| 56 |
# Allgemeine Konstanten
|
|
@@ -323,7 +325,7 @@ def create_assistant_suche_hf(chatbot, prompt):
|
|
| 323 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 324 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 325 |
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):
|
| 326 |
-
global
|
| 327 |
#nur wenn man sich validiert hat, kann die Anwendung los legen
|
| 328 |
if (validate and not prompt_in == "" and not prompt_in == None):
|
| 329 |
#wenn RAG angeschaltet - Vektorstore initialisieren
|
|
@@ -334,23 +336,16 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, rag_option
|
|
| 334 |
#prompt normalisieren bevor er an die KIs geht
|
| 335 |
prompt = normalise_prompt(prompt_in)
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
db = document_retrieval_chroma2()
|
| 344 |
-
print("db aktiv!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
| 345 |
-
print(db)
|
| 346 |
-
#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
|
| 347 |
-
#db=None
|
| 348 |
-
#splittet = False #sonst würde es für alle User wieder ausgeschaltet - Alternative: gr.State(False) dazu anlegen
|
| 349 |
-
|
| 350 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 351 |
status = "Antwort der KI ..."
|
| 352 |
if (file == None and file_history == None):
|
| 353 |
-
result, status = generate_text(prompt, chatbot, history, rag_option, model_option, openai_api_key,
|
| 354 |
history = history + [[prompt, result]]
|
| 355 |
else:
|
| 356 |
#Es wurde ein File neu angehängt -> wenn prompt dazu, das Bild analysieren
|
|
@@ -362,9 +357,9 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, rag_option
|
|
| 362 |
#herausfinden, ob Bild oder Dokument...
|
| 363 |
ext = analyze_file(neu_file)
|
| 364 |
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 365 |
-
result= generate_text_zu_bild(neu_file, prompt, k, rag_option, chatbot, history,
|
| 366 |
else:
|
| 367 |
-
result = generate_text_zu_doc(neu_file, prompt, k, rag_option, chatbot, history,
|
| 368 |
|
| 369 |
#die history erweitern - abhängig davon, ob gerade ein file hochgeladen wurde oder nicht
|
| 370 |
if (file != None):
|
|
@@ -416,13 +411,13 @@ def generate_bild(prompt, chatbot, model_option_zeichnen='HuggingFace', temperat
|
|
| 416 |
|
| 417 |
##################################################
|
| 418 |
#zu einem Bild und Text-Prompt eine Analyse generieren
|
| 419 |
-
def generate_text_zu_bild(file, prompt, k, rag_option, chatbot, history,
|
| 420 |
global splittet
|
| 421 |
print("Text mit Bild ..............................")
|
| 422 |
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 423 |
if (rag_option == "An"):
|
| 424 |
print("Bild mit RAG..............................")
|
| 425 |
-
neu_text_mit_chunks = rag_chain2(prompt,
|
| 426 |
#für Chat LLM:
|
| 427 |
#prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
|
| 428 |
#als reiner prompt:
|
|
@@ -439,14 +434,14 @@ def generate_text_zu_bild(file, prompt, k, rag_option, chatbot, history, db):
|
|
| 439 |
|
| 440 |
##################################################
|
| 441 |
#zu einem Bild und Text-Prompt eine Analyse generieren
|
| 442 |
-
def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history,
|
| 443 |
global splittet
|
| 444 |
print("text mit doc ..............................")
|
| 445 |
|
| 446 |
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 447 |
if (rag_option == "An"):
|
| 448 |
print("Doc mit RAG..............................")
|
| 449 |
-
neu_text_mit_chunks = rag_chain2(prompt,
|
| 450 |
#für Chat LLM:
|
| 451 |
#prompt_neu = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
|
| 452 |
#als reiner prompt:
|
|
@@ -459,7 +454,7 @@ def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history, db):
|
|
| 459 |
####################################################
|
| 460 |
#aus einem Text-Prompt die Antwort von KI bekommen
|
| 461 |
#mit oder ohne RAG möglich
|
| 462 |
-
def generate_text (prompt, chatbot, history, rag_option, model_option, openai_api_key,
|
| 463 |
#global splittet
|
| 464 |
#hugchat=False
|
| 465 |
suche_im_Netz="Antwort der KI ..."
|
|
@@ -517,9 +512,9 @@ def generate_text (prompt, chatbot, history, rag_option, model_option, openai_ap
|
|
| 517 |
print("LLM aufrufen mit RAG: ...........")
|
| 518 |
print(history_text_und_prompt)
|
| 519 |
print("-------------------------------")
|
| 520 |
-
result = rag_chain(llm, history_text_und_prompt,
|
| 521 |
#weitere Möglichkeit für Rag-Chain - dann auch für HF Modelle möglich, da kein llm in Langchain übergeben werden muss...
|
| 522 |
-
#result = rag_chain2(history_text_und_prompt,
|
| 523 |
print("result regchain.....................")
|
| 524 |
print(result)
|
| 525 |
else:
|
|
|
|
| 50 |
#nur bei ersten Anfrage splitten der Dokumente - um die Vektordatenbank entsprechend zu füllen
|
| 51 |
#splittet = False
|
| 52 |
#DB für Vektorstore
|
| 53 |
+
vektordatenbank = None
|
| 54 |
+
retriever = None
|
| 55 |
+
|
| 56 |
|
| 57 |
#############################################
|
| 58 |
# Allgemeine Konstanten
|
|
|
|
| 325 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 326 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 327 |
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):
|
| 328 |
+
global vektordatenbank, retriever
|
| 329 |
#nur wenn man sich validiert hat, kann die Anwendung los legen
|
| 330 |
if (validate and not prompt_in == "" and not prompt_in == None):
|
| 331 |
#wenn RAG angeschaltet - Vektorstore initialisieren
|
|
|
|
| 336 |
#prompt normalisieren bevor er an die KIs geht
|
| 337 |
prompt = normalise_prompt(prompt_in)
|
| 338 |
|
| 339 |
+
#muss nur einmal ausgeführt werden...
|
| 340 |
+
if vektordatenbank == None:
|
| 341 |
+
print("db neu aufbauen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1")
|
| 342 |
+
splits = document_loading_splitting()
|
| 343 |
+
vektordatenbank, retriever = document_storage_chroma(splits)
|
| 344 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 346 |
status = "Antwort der KI ..."
|
| 347 |
if (file == None and file_history == None):
|
| 348 |
+
result, status = generate_text(prompt, chatbot, history, rag_option, model_option, openai_api_key, vektordatenbank, 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)
|
| 349 |
history = history + [[prompt, result]]
|
| 350 |
else:
|
| 351 |
#Es wurde ein File neu angehängt -> wenn prompt dazu, das Bild analysieren
|
|
|
|
| 357 |
#herausfinden, ob Bild oder Dokument...
|
| 358 |
ext = analyze_file(neu_file)
|
| 359 |
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 360 |
+
result= generate_text_zu_bild(neu_file, prompt, k, rag_option, chatbot, history, vektordatenbank)
|
| 361 |
else:
|
| 362 |
+
result = generate_text_zu_doc(neu_file, prompt, k, rag_option, chatbot, history, vektordatenbank)
|
| 363 |
|
| 364 |
#die history erweitern - abhängig davon, ob gerade ein file hochgeladen wurde oder nicht
|
| 365 |
if (file != None):
|
|
|
|
| 411 |
|
| 412 |
##################################################
|
| 413 |
#zu einem Bild und Text-Prompt eine Analyse generieren
|
| 414 |
+
def generate_text_zu_bild(file, prompt, k, rag_option, chatbot, history, vektordatenbank):
|
| 415 |
global splittet
|
| 416 |
print("Text mit Bild ..............................")
|
| 417 |
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 418 |
if (rag_option == "An"):
|
| 419 |
print("Bild mit RAG..............................")
|
| 420 |
+
neu_text_mit_chunks = rag_chain2(prompt, vektordatenbank, k)
|
| 421 |
#für Chat LLM:
|
| 422 |
#prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
|
| 423 |
#als reiner prompt:
|
|
|
|
| 434 |
|
| 435 |
##################################################
|
| 436 |
#zu einem Bild und Text-Prompt eine Analyse generieren
|
| 437 |
+
def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history, vektordatenbank):
|
| 438 |
global splittet
|
| 439 |
print("text mit doc ..............................")
|
| 440 |
|
| 441 |
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 442 |
if (rag_option == "An"):
|
| 443 |
print("Doc mit RAG..............................")
|
| 444 |
+
neu_text_mit_chunks = rag_chain2(prompt, vektordatenbank, k)
|
| 445 |
#für Chat LLM:
|
| 446 |
#prompt_neu = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
|
| 447 |
#als reiner prompt:
|
|
|
|
| 454 |
####################################################
|
| 455 |
#aus einem Text-Prompt die Antwort von KI bekommen
|
| 456 |
#mit oder ohne RAG möglich
|
| 457 |
+
def generate_text (prompt, chatbot, history, rag_option, model_option, openai_api_key, vektordatenbank, 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=35):
|
| 458 |
#global splittet
|
| 459 |
#hugchat=False
|
| 460 |
suche_im_Netz="Antwort der KI ..."
|
|
|
|
| 512 |
print("LLM aufrufen mit RAG: ...........")
|
| 513 |
print(history_text_und_prompt)
|
| 514 |
print("-------------------------------")
|
| 515 |
+
result = rag_chain(llm, history_text_und_prompt, retriever) #für hugchat noch kein rag möglich...
|
| 516 |
#weitere Möglichkeit für Rag-Chain - dann auch für HF Modelle möglich, da kein llm in Langchain übergeben werden muss...
|
| 517 |
+
#result = rag_chain2(history_text_und_prompt, vektordatenbank, 5)
|
| 518 |
print("result regchain.....................")
|
| 519 |
print(result)
|
| 520 |
else:
|