Update app.py
Browse files
app.py
CHANGED
|
@@ -294,7 +294,7 @@ def create_assistant_suche(prompt):
|
|
| 294 |
#########################################################
|
| 295 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 296 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 297 |
-
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):
|
| 298 |
global splittet, db
|
| 299 |
#wenn RAG angeschaltet - Vektorstore initialisieren
|
| 300 |
#aber nur, wenn es noch nicht geshehen ist (splittet = False)
|
|
@@ -318,7 +318,7 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, rag_option
|
|
| 318 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 319 |
status = "Antwort der KI ..."
|
| 320 |
if (file == None and file_history == None):
|
| 321 |
-
result, status = generate_text(prompt, chatbot, history, rag_option, model_option, openai_api_key, db, 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)
|
| 322 |
history = history + [[prompt, result]]
|
| 323 |
else:
|
| 324 |
#Es wurde ein File neu angehängt -> wenn prompt dazu, das Bild analysieren
|
|
@@ -422,7 +422,7 @@ def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history, db):
|
|
| 422 |
####################################################
|
| 423 |
#aus einem Text-Prompt die Antwort von KI bekommen
|
| 424 |
#mit oder ohne RAG möglich
|
| 425 |
-
def generate_text (prompt, chatbot, history, rag_option, model_option, openai_api_key, db, 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):
|
| 426 |
global splittet
|
| 427 |
hugchat=False
|
| 428 |
suche_im_Netz="Antwort der KI ..."
|
|
@@ -444,68 +444,77 @@ def generate_text (prompt, chatbot, history, rag_option, model_option, openai_ap
|
|
| 444 |
#history_text_und_prompt = generate_prompt_with_history_langchain(prompt, history)
|
| 445 |
|
| 446 |
try:
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
else:
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
#Prompt an history anhängen und einen Text daraus machen
|
| 473 |
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
result = rag_chain(llm, history_text_und_prompt, db) #für hugchat noch kein rag möglich...
|
| 479 |
-
else:
|
| 480 |
-
#splittet = False
|
| 481 |
-
print("LLM aufrufen ohne RAG: ...........")
|
| 482 |
-
resulti = llm_chain(llm, history_text_und_prompt, hugchat)
|
| 483 |
-
result = resulti.strip()
|
| 484 |
-
"""
|
| 485 |
-
#Alternativ mit API_URL - aber das model braucht 93 B Space!!!
|
| 486 |
-
data = {"inputs": prompt, "options": {"max_new_tokens": max_new_tokens},}
|
| 487 |
-
response = requests.post(API_URL_TEXT, headers=HEADERS, json=data)
|
| 488 |
-
result = response.json()
|
| 489 |
-
print("responseresult.............................")
|
| 490 |
-
print(result)
|
| 491 |
-
chatbot_response = result[0]['generated_text']
|
| 492 |
-
print("anzahl tokens gesamt antwort:------------------")
|
| 493 |
-
print (len(chatbot_response.split()))
|
| 494 |
-
chatbot_message = chatbot_response[len(prompt):].strip()
|
| 495 |
-
print("history/chatbot_rsponse:--------------------------------")
|
| 496 |
-
print(history)
|
| 497 |
-
print(chatbot_message)
|
| 498 |
-
result = chatbot_message
|
| 499 |
-
"""
|
| 500 |
|
| 501 |
-
|
| 502 |
-
|
| 503 |
#Wenn keine Antwort möglich "Ich weiß es nicht" etc., dann versuchen mit Suche im Internet.
|
| 504 |
if (result == None or is_response_similar(result)):
|
| 505 |
print("Suche im Netz: ...........")
|
| 506 |
suche_im_Netz="Antwort aus dem Internet ..."
|
| 507 |
result = create_assistant_suche(prompt)
|
| 508 |
-
|
| 509 |
except Exception as e:
|
| 510 |
raise gr.Error(e)
|
| 511 |
|
|
@@ -673,7 +682,7 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 673 |
with gr.Column(min_width=70, scale=1):
|
| 674 |
cancelBtn = gr.Button("Stop")
|
| 675 |
with gr.Row():
|
| 676 |
-
|
| 677 |
image_display = gr.Image( visible=False)
|
| 678 |
upload = gr.UploadButton("📁", file_types=["image", "pdf", "docx", "pptx", "xlsx"], scale = 10)
|
| 679 |
emptyBtn = gr.ClearButton([user_input, chatbot, history, attached_file, attached_file_history, image_display], value="🧹 Neue Session", scale=10)
|
|
@@ -832,7 +841,8 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 832 |
max_length_tokens,
|
| 833 |
max_context_length_tokens,
|
| 834 |
repetition_penalty,
|
| 835 |
-
top_k
|
|
|
|
| 836 |
],
|
| 837 |
outputs=[chatbot, history, attached_file, attached_file_history, status_display],
|
| 838 |
show_progress=True,
|
|
|
|
| 294 |
#########################################################
|
| 295 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 296 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 297 |
+
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"):
|
| 298 |
global splittet, db
|
| 299 |
#wenn RAG angeschaltet - Vektorstore initialisieren
|
| 300 |
#aber nur, wenn es noch nicht geshehen ist (splittet = False)
|
|
|
|
| 318 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 319 |
status = "Antwort der KI ..."
|
| 320 |
if (file == None and file_history == None):
|
| 321 |
+
result, status = generate_text(prompt, chatbot, history, rag_option, model_option, openai_api_key, db, 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")
|
| 322 |
history = history + [[prompt, result]]
|
| 323 |
else:
|
| 324 |
#Es wurde ein File neu angehängt -> wenn prompt dazu, das Bild analysieren
|
|
|
|
| 422 |
####################################################
|
| 423 |
#aus einem Text-Prompt die Antwort von KI bekommen
|
| 424 |
#mit oder ohne RAG möglich
|
| 425 |
+
def generate_text (prompt, chatbot, history, rag_option, model_option, openai_api_key, db, 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"):
|
| 426 |
global splittet
|
| 427 |
hugchat=False
|
| 428 |
suche_im_Netz="Antwort der KI ..."
|
|
|
|
| 444 |
#history_text_und_prompt = generate_prompt_with_history_langchain(prompt, history)
|
| 445 |
|
| 446 |
try:
|
| 447 |
+
if (websuche="Aus"):
|
| 448 |
+
###########################
|
| 449 |
+
#LLM auswählen (OpenAI oder HF)
|
| 450 |
+
###########################
|
| 451 |
+
if (model_option == "OpenAI"):
|
| 452 |
+
#Anfrage an OpenAI ----------------------------
|
| 453 |
+
print("OpenAI Anfrage.......................")
|
| 454 |
+
llm = ChatOpenAI(model_name = MODEL_NAME, openai_api_key = openai_api_key, temperature=temperature)#, top_p = top_p)
|
| 455 |
+
#Prompt an history anhängen und einen Text daraus machen
|
| 456 |
+
if (rag_option == "An"):
|
| 457 |
+
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
| 458 |
+
else:
|
| 459 |
+
history_text_und_prompt = generate_prompt_with_history_openai(prompt, history)
|
| 460 |
+
else:
|
| 461 |
+
#oder an Hugging Face --------------------------
|
| 462 |
+
print("HF Anfrage.......................")
|
| 463 |
+
model_kwargs={"temperature": 0.5, "max_length": 512, "num_return_sequences": 1, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty}
|
| 464 |
+
#llm = HuggingFaceHub(repo_id=repo_id, model_kwargs=model_kwargs)
|
| 465 |
+
#llm = HuggingFaceChain(model=MODEL_NAME_HF, model_kwargs={"temperature": 0.5, "max_length": 128})
|
| 466 |
+
#llm = HuggingFaceHub(url_??? = "https://wdgsjd6zf201mufn.us-east-1.aws.endpoints.huggingface.cloud", model_kwargs={"temperature": 0.5, "max_length": 64})
|
| 467 |
+
#llm = HuggingFaceTextGenInference( inference_server_url="http://localhost:8010/", max_new_tokens=max_new_tokens,top_k=10,top_p=top_p,typical_p=0.95,temperature=temperature,repetition_penalty=repetition_penalty,)
|
| 468 |
+
#llm via HuggingChat
|
| 469 |
+
llm = hugchat.ChatBot(cookies=cookies.get_dict())
|
| 470 |
+
hugchat=True #da dieses Model in llm_chain bzw reag_chain anderes verarbeitet wird
|
| 471 |
+
|
| 472 |
+
print("HF")
|
| 473 |
+
#Prompt an history anhängen und einen Text daraus machen
|
| 474 |
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
| 475 |
+
|
| 476 |
+
#zusätzliche Dokumenten Splits aus DB zum Prompt hinzufügen (aus VektorDB - Chroma oder Mongo DB)
|
| 477 |
+
if (rag_option == "An"):
|
| 478 |
+
print("LLM aufrufen mit RAG: ...........")
|
| 479 |
+
result = rag_chain(llm, history_text_und_prompt, db) #für hugchat noch kein rag möglich...
|
| 480 |
else:
|
| 481 |
+
#splittet = False
|
| 482 |
+
print("LLM aufrufen ohne RAG: ...........")
|
| 483 |
+
resulti = llm_chain(llm, history_text_und_prompt, hugchat)
|
| 484 |
+
result = resulti.strip()
|
| 485 |
+
"""
|
| 486 |
+
#Alternativ mit API_URL - aber das model braucht 93 B Space!!!
|
| 487 |
+
data = {"inputs": prompt, "options": {"max_new_tokens": max_new_tokens},}
|
| 488 |
+
response = requests.post(API_URL_TEXT, headers=HEADERS, json=data)
|
| 489 |
+
result = response.json()
|
| 490 |
+
print("responseresult.............................")
|
| 491 |
+
print(result)
|
| 492 |
+
chatbot_response = result[0]['generated_text']
|
| 493 |
+
print("anzahl tokens gesamt antwort:------------------")
|
| 494 |
+
print (len(chatbot_response.split()))
|
| 495 |
+
chatbot_message = chatbot_response[len(prompt):].strip()
|
| 496 |
+
print("history/chatbot_rsponse:--------------------------------")
|
| 497 |
+
print(history)
|
| 498 |
+
print(chatbot_message)
|
| 499 |
+
result = chatbot_message
|
| 500 |
+
"""
|
| 501 |
+
else: #Websuche ist An
|
| 502 |
+
print("Suche im Netz: ...........")
|
| 503 |
+
suche_im_Netz="Antwort aus dem Internet ..."
|
| 504 |
#Prompt an history anhängen und einen Text daraus machen
|
| 505 |
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
| 506 |
+
#mit tavily:
|
| 507 |
+
#result = create_assistant_suche(history_text_und_prompt)
|
| 508 |
+
#mit hugchat
|
| 509 |
+
result = create_assistant_suche_hf(history_text_und_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
|
| 511 |
+
"""
|
|
|
|
| 512 |
#Wenn keine Antwort möglich "Ich weiß es nicht" etc., dann versuchen mit Suche im Internet.
|
| 513 |
if (result == None or is_response_similar(result)):
|
| 514 |
print("Suche im Netz: ...........")
|
| 515 |
suche_im_Netz="Antwort aus dem Internet ..."
|
| 516 |
result = create_assistant_suche(prompt)
|
| 517 |
+
"""
|
| 518 |
except Exception as e:
|
| 519 |
raise gr.Error(e)
|
| 520 |
|
|
|
|
| 682 |
with gr.Column(min_width=70, scale=1):
|
| 683 |
cancelBtn = gr.Button("Stop")
|
| 684 |
with gr.Row():
|
| 685 |
+
websuche = gr.Radio(["Aus", "An"], label="Web-Suche", value = "Aus")
|
| 686 |
image_display = gr.Image( visible=False)
|
| 687 |
upload = gr.UploadButton("📁", file_types=["image", "pdf", "docx", "pptx", "xlsx"], scale = 10)
|
| 688 |
emptyBtn = gr.ClearButton([user_input, chatbot, history, attached_file, attached_file_history, image_display], value="🧹 Neue Session", scale=10)
|
|
|
|
| 841 |
max_length_tokens,
|
| 842 |
max_context_length_tokens,
|
| 843 |
repetition_penalty,
|
| 844 |
+
top_k,
|
| 845 |
+
websuche
|
| 846 |
],
|
| 847 |
outputs=[chatbot, history, attached_file, attached_file_history, status_display],
|
| 848 |
show_progress=True,
|