Update app.py
Browse files
app.py
CHANGED
|
@@ -112,6 +112,7 @@ general_assistant_file = client.beta.assistants.create(name="File Analysator",in
|
|
| 112 |
thread_file = client.beta.threads.create()
|
| 113 |
general_assistant_suche= openai_assistant_suche(client)
|
| 114 |
|
|
|
|
| 115 |
#################################################
|
| 116 |
#################################################
|
| 117 |
#################################################
|
|
@@ -121,7 +122,7 @@ general_assistant_suche= openai_assistant_suche(client)
|
|
| 121 |
##############################################
|
| 122 |
#wenn löschen Button geklickt
|
| 123 |
def clear_all():
|
| 124 |
-
return None, gr.Image(visible=False), []
|
| 125 |
|
| 126 |
##############################################
|
| 127 |
#History - die Frage oder das File eintragen...
|
|
@@ -129,6 +130,7 @@ def add_text(chatbot, history, prompt, file):
|
|
| 129 |
if (file == None):
|
| 130 |
chatbot = chatbot +[(prompt, None)]
|
| 131 |
else:
|
|
|
|
| 132 |
if (prompt == ""):
|
| 133 |
chatbot=chatbot + [((file.name,), "Prompt fehlt!")]
|
| 134 |
else:
|
|
@@ -138,7 +140,7 @@ def add_text(chatbot, history, prompt, file):
|
|
| 138 |
else:
|
| 139 |
chatbot = chatbot +[("Hochgeladenes Dokument: "+ get_filename(file) +"\n" + prompt, None)]
|
| 140 |
|
| 141 |
-
return chatbot, history, prompt, file, gr.Image(visible = False), "" #gr.Image( label=None, size=(30,30), visible=False, scale=1) #gr.Textbox(value="", interactive=False)
|
| 142 |
|
| 143 |
def add_text2(chatbot, prompt):
|
| 144 |
if (prompt == ""):
|
|
@@ -259,11 +261,12 @@ def create_assistant_suche(prompt):
|
|
| 259 |
#########################################################
|
| 260 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 261 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 262 |
-
def generate_auswahl(prompt, file, 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,):
|
| 263 |
global splittet
|
| 264 |
#wenn RAG angeschaltet - Vektorstore initialisieren
|
| 265 |
#aber nur, wenn es noch nicht geshehen ist (splittet = False)
|
| 266 |
-
|
|
|
|
| 267 |
if (rag_option == "An"):
|
| 268 |
#muss nur einmal ausgeführt werden...
|
| 269 |
if not splittet:
|
|
@@ -277,29 +280,36 @@ def generate_auswahl(prompt, file, chatbot, history, rag_option, model_option, o
|
|
| 277 |
|
| 278 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 279 |
status = "Antwort der KI ..."
|
| 280 |
-
if (file == None):
|
| 281 |
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,)
|
| 282 |
history = history + [[prompt, result]]
|
| 283 |
else:
|
| 284 |
-
#Es wurde ein
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
#geht nur über spezielle OpenAI-Schnittstelle...
|
| 286 |
-
ext = analyze_file(
|
| 287 |
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 288 |
-
result= generate_text_zu_bild(
|
| 289 |
else:
|
| 290 |
-
result = generate_text_zu_doc(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
-
history = history + [[(file,), None],[prompt, result]]
|
| 293 |
-
|
| 294 |
chatbot[-1][1] = ""
|
| 295 |
for character in result:
|
| 296 |
chatbot[-1][1] += character
|
| 297 |
time.sleep(0.03)
|
| 298 |
-
yield chatbot, history, None, status
|
| 299 |
if shared_state.interrupted:
|
| 300 |
shared_state.recover()
|
| 301 |
try:
|
| 302 |
-
yield chatbot, history, None, "Stop: Success"
|
| 303 |
except:
|
| 304 |
pass
|
| 305 |
|
|
@@ -334,14 +344,14 @@ def generate_text_zu_bild(file, prompt, k, rag_option, chatbot, history, db):
|
|
| 334 |
global splittet
|
| 335 |
print("Text mit Bild ..............................")
|
| 336 |
print(file)
|
| 337 |
-
prompt_neu = prompt
|
| 338 |
if (rag_option == "An"):
|
| 339 |
print("Bild mit RAG..............................")
|
| 340 |
neu_text_mit_chunks = rag_chain2(prompt, db, k)
|
| 341 |
#für Chat LLM:
|
| 342 |
#prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
|
| 343 |
#als reiner prompt:
|
| 344 |
-
prompt_neu = generate_prompt_with_history(neu_text_mit_chunks,
|
| 345 |
|
| 346 |
headers, payload = process_image(file, prompt_neu)
|
| 347 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
@@ -480,6 +490,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 480 |
#damit der Prompt auch nach dem upload in die History noch für predicts_args verfügbar ist
|
| 481 |
user_question2 = gr.State("")
|
| 482 |
attached_file = gr.State(None)
|
|
|
|
| 483 |
status_display = gr.State("")
|
| 484 |
status_display2 = gr.State("")
|
| 485 |
################################################
|
|
@@ -508,7 +519,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 508 |
#file_display = gr.File(visible=False)
|
| 509 |
image_display = gr.Image( visible=False)
|
| 510 |
upload = gr.UploadButton("📁", file_types=["image", "pdf", "docx", "pptx", "xlsx"], scale = 10)
|
| 511 |
-
emptyBtn = gr.ClearButton([user_input, chatbot, history, attached_file, image_display], value="🧹 Neue Session", scale=10)
|
| 512 |
|
| 513 |
with gr.Column():
|
| 514 |
with gr.Column(min_width=50, scale=1):
|
|
@@ -602,6 +613,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 602 |
inputs=[
|
| 603 |
user_question,
|
| 604 |
attached_file,
|
|
|
|
| 605 |
chatbot,
|
| 606 |
history,
|
| 607 |
rag_option,
|
|
@@ -614,7 +626,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 614 |
max_context_length_tokens,
|
| 615 |
repetition_penalty
|
| 616 |
],
|
| 617 |
-
outputs=[chatbot, history, attached_file, status_display],
|
| 618 |
show_progress=True,
|
| 619 |
)
|
| 620 |
|
|
@@ -624,7 +636,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 624 |
|
| 625 |
# Chatbot
|
| 626 |
transfer_input_args = dict(
|
| 627 |
-
fn=add_text, inputs=[chatbot, history, user_input, attached_file], outputs=[chatbot, history, user_question, attached_file, image_display , user_input], show_progress=True
|
| 628 |
)
|
| 629 |
|
| 630 |
predict_event1 = user_input.submit(**transfer_input_args, queue=False,).then(**predict_args)
|
|
|
|
| 112 |
thread_file = client.beta.threads.create()
|
| 113 |
general_assistant_suche= openai_assistant_suche(client)
|
| 114 |
|
| 115 |
+
|
| 116 |
#################################################
|
| 117 |
#################################################
|
| 118 |
#################################################
|
|
|
|
| 122 |
##############################################
|
| 123 |
#wenn löschen Button geklickt
|
| 124 |
def clear_all():
|
| 125 |
+
return None, gr.Image(visible=False), [], []
|
| 126 |
|
| 127 |
##############################################
|
| 128 |
#History - die Frage oder das File eintragen...
|
|
|
|
| 130 |
if (file == None):
|
| 131 |
chatbot = chatbot +[(prompt, None)]
|
| 132 |
else:
|
| 133 |
+
|
| 134 |
if (prompt == ""):
|
| 135 |
chatbot=chatbot + [((file.name,), "Prompt fehlt!")]
|
| 136 |
else:
|
|
|
|
| 140 |
else:
|
| 141 |
chatbot = chatbot +[("Hochgeladenes Dokument: "+ get_filename(file) +"\n" + prompt, None)]
|
| 142 |
|
| 143 |
+
return chatbot, history, prompt, file, file, gr.Image(visible = False), "" #gr.Image( label=None, size=(30,30), visible=False, scale=1) #gr.Textbox(value="", interactive=False)
|
| 144 |
|
| 145 |
def add_text2(chatbot, prompt):
|
| 146 |
if (prompt == ""):
|
|
|
|
| 261 |
#########################################################
|
| 262 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 263 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 264 |
+
def generate_auswahl(prompt, 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,):
|
| 265 |
global splittet
|
| 266 |
#wenn RAG angeschaltet - Vektorstore initialisieren
|
| 267 |
#aber nur, wenn es noch nicht geshehen ist (splittet = False)
|
| 268 |
+
#falls schon ein File hochgeladen wurde, ist es in history_file gespeichert - falls ein neues File hochgeladen wurde, wird es anschließend neu gesetzt
|
| 269 |
+
neu_file = file_history
|
| 270 |
if (rag_option == "An"):
|
| 271 |
#muss nur einmal ausgeführt werden...
|
| 272 |
if not splittet:
|
|
|
|
| 280 |
|
| 281 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 282 |
status = "Antwort der KI ..."
|
| 283 |
+
if (file == None and file_history == None):
|
| 284 |
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,)
|
| 285 |
history = history + [[prompt, result]]
|
| 286 |
else:
|
| 287 |
+
#Es wurde ein File angehängt -> wenn prompt dazu, das Bild analysieren
|
| 288 |
+
if (file != None):
|
| 289 |
+
# file_history wird neu gesetzt in der Rückgabe dieser Funktion...
|
| 290 |
+
neu_file = file
|
| 291 |
+
|
| 292 |
#geht nur über spezielle OpenAI-Schnittstelle...
|
| 293 |
+
ext = analyze_file(neu_file)
|
| 294 |
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 295 |
+
result= generate_text_zu_bild(neu_file, prompt, k, rag_option, chatbot, history, db)
|
| 296 |
else:
|
| 297 |
+
result = generate_text_zu_doc(_neu_file, prompt, k, rag_option, chatbot, history, db)
|
| 298 |
+
|
| 299 |
+
if (file != None):
|
| 300 |
+
history = history + [[(file,), None],[prompt, result]]
|
| 301 |
+
else:
|
| 302 |
+
history = history + [[prompt, result]]
|
| 303 |
|
|
|
|
|
|
|
| 304 |
chatbot[-1][1] = ""
|
| 305 |
for character in result:
|
| 306 |
chatbot[-1][1] += character
|
| 307 |
time.sleep(0.03)
|
| 308 |
+
yield chatbot, history, None, neu_file, status
|
| 309 |
if shared_state.interrupted:
|
| 310 |
shared_state.recover()
|
| 311 |
try:
|
| 312 |
+
yield chatbot, history, None, neu_file, "Stop: Success"
|
| 313 |
except:
|
| 314 |
pass
|
| 315 |
|
|
|
|
| 344 |
global splittet
|
| 345 |
print("Text mit Bild ..............................")
|
| 346 |
print(file)
|
| 347 |
+
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 348 |
if (rag_option == "An"):
|
| 349 |
print("Bild mit RAG..............................")
|
| 350 |
neu_text_mit_chunks = rag_chain2(prompt, db, k)
|
| 351 |
#für Chat LLM:
|
| 352 |
#prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
|
| 353 |
#als reiner prompt:
|
| 354 |
+
prompt_neu = generate_prompt_with_history(neu_text_mit_chunks, history)
|
| 355 |
|
| 356 |
headers, payload = process_image(file, prompt_neu)
|
| 357 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
|
|
| 490 |
#damit der Prompt auch nach dem upload in die History noch für predicts_args verfügbar ist
|
| 491 |
user_question2 = gr.State("")
|
| 492 |
attached_file = gr.State(None)
|
| 493 |
+
attached_file_history = gr.State(None)
|
| 494 |
status_display = gr.State("")
|
| 495 |
status_display2 = gr.State("")
|
| 496 |
################################################
|
|
|
|
| 519 |
#file_display = gr.File(visible=False)
|
| 520 |
image_display = gr.Image( visible=False)
|
| 521 |
upload = gr.UploadButton("📁", file_types=["image", "pdf", "docx", "pptx", "xlsx"], scale = 10)
|
| 522 |
+
emptyBtn = gr.ClearButton([user_input, chatbot, history, attached_file, attached_file_history, image_display], value="🧹 Neue Session", scale=10)
|
| 523 |
|
| 524 |
with gr.Column():
|
| 525 |
with gr.Column(min_width=50, scale=1):
|
|
|
|
| 613 |
inputs=[
|
| 614 |
user_question,
|
| 615 |
attached_file,
|
| 616 |
+
attached_file_history,
|
| 617 |
chatbot,
|
| 618 |
history,
|
| 619 |
rag_option,
|
|
|
|
| 626 |
max_context_length_tokens,
|
| 627 |
repetition_penalty
|
| 628 |
],
|
| 629 |
+
outputs=[chatbot, history, attached_file, attached_file_history, status_display],
|
| 630 |
show_progress=True,
|
| 631 |
)
|
| 632 |
|
|
|
|
| 636 |
|
| 637 |
# Chatbot
|
| 638 |
transfer_input_args = dict(
|
| 639 |
+
fn=add_text, inputs=[chatbot, history, user_input, attached_file], outputs=[chatbot, history, user_question, attached_file, attached_file_history, image_display , user_input], show_progress=True
|
| 640 |
)
|
| 641 |
|
| 642 |
predict_event1 = user_input.submit(**transfer_input_args, queue=False,).then(**predict_args)
|