Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -401,17 +401,6 @@ def generate_prompt_with_history_langchain(prompt, history):
|
|
| 401 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 402 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 403 |
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,):
|
| 404 |
-
#Bild nach Anweisung zeichnen und in History darstellen...
|
| 405 |
-
if (prompt.find('zeichnen') != -1):
|
| 406 |
-
response = generate_bild(prompt)
|
| 407 |
-
result = response.content
|
| 408 |
-
#Bild ausgeben
|
| 409 |
-
image = Image.open(io.BytesIO(result))
|
| 410 |
-
image_64 = umwandeln_fuer_anzeige(image)
|
| 411 |
-
chatbot[-1][1] = "<img src='data:image/png;base64,{0}'/>".format(base64.b64encode(image_64).decode('utf-8'))
|
| 412 |
-
history = history + [(prompt, result)]
|
| 413 |
-
return chatbot, history, "Success"
|
| 414 |
-
else:
|
| 415 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 416 |
if (file == None):
|
| 417 |
result = generate_text(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,)
|
|
@@ -440,11 +429,19 @@ def generate_auswahl(prompt, file, chatbot, history, rag_option, model_option, o
|
|
| 440 |
"""
|
| 441 |
##################################################
|
| 442 |
#zu einem Text-Prompt ein Bild via Stable Diffusion generieren
|
| 443 |
-
def generate_bild(prompt):
|
|
|
|
| 444 |
data = {"inputs": prompt}
|
| 445 |
response = requests.post(API_URL, headers=HEADERS, json=data)
|
| 446 |
print("fertig Bild")
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
##################################################
|
| 450 |
#zu einem Bild und Text-Prompt eine Analyse generieren
|
|
@@ -552,74 +549,115 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 552 |
history = gr.State([])
|
| 553 |
#damit der Prompt auch nach dem upload in die History noch für predicts_args verfügbar ist
|
| 554 |
user_question = gr.State("")
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
gr.
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
)
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
gr.Markdown(description)
|
| 622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
#Argumente für generate Funktion als Input
|
| 624 |
predict_args = dict(
|
| 625 |
fn=generate_auswahl,
|
|
@@ -660,8 +698,12 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 660 |
cancelBtn.click(
|
| 661 |
cancels=[predict_event1,predict_event2, predict_event3 ]
|
| 662 |
)
|
| 663 |
-
demo.title = "LI-ChatBot"
|
| 664 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
demo.queue().launch(debug=True)
|
| 666 |
|
| 667 |
|
|
|
|
| 401 |
#Funktion wird direkt aufgerufen aus der GUI - von hier muss auch die Rückmeldung kommen....
|
| 402 |
#man kann einen Text-Prompt eingeben (mit oder ohne RAG), dazu ein Image hochladen, ein Bild zu einem reinen textprompt erzeugen lassen
|
| 403 |
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,):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
#kein Bild hochgeladen -> auf Text antworten...
|
| 405 |
if (file == None):
|
| 406 |
result = generate_text(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,)
|
|
|
|
| 429 |
"""
|
| 430 |
##################################################
|
| 431 |
#zu einem Text-Prompt ein Bild via Stable Diffusion generieren
|
| 432 |
+
def generate_bild(prompt, chatbot, temperature=0.5, max_new_tokens=4048,top_p=0.6, repetition_penalty=1.3,):
|
| 433 |
+
#Bild nach Anweisung zeichnen und in History darstellen...
|
| 434 |
data = {"inputs": prompt}
|
| 435 |
response = requests.post(API_URL, headers=HEADERS, json=data)
|
| 436 |
print("fertig Bild")
|
| 437 |
+
result = response.content
|
| 438 |
+
#Bild ausgeben
|
| 439 |
+
image = Image.open(io.BytesIO(result))
|
| 440 |
+
image_64 = umwandeln_fuer_anzeige(image)
|
| 441 |
+
chatbot.append(prompt, "<img src='data:image/png;base64,{0}'/>".format(base64.b64encode(image_64).decode('utf-8')))
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
|
| 445 |
|
| 446 |
##################################################
|
| 447 |
#zu einem Bild und Text-Prompt eine Analyse generieren
|
|
|
|
| 549 |
history = gr.State([])
|
| 550 |
#damit der Prompt auch nach dem upload in die History noch für predicts_args verfügbar ist
|
| 551 |
user_question = gr.State("")
|
| 552 |
+
################################################
|
| 553 |
+
# Tab zum Chatbot mit Text oder Bildeingabe
|
| 554 |
+
################################################
|
| 555 |
+
with gr.Tab(Chatbot):
|
| 556 |
+
with gr.Row():
|
| 557 |
+
gr.HTML("LI Chatot")
|
| 558 |
+
status_display = gr.Markdown("Success", elem_id="status_display")
|
| 559 |
+
gr.Markdown(description_top)
|
| 560 |
+
with gr.Row():
|
| 561 |
+
with gr.Column(scale=5):
|
| 562 |
+
with gr.Row():
|
| 563 |
+
chatbot = gr.Chatbot(elem_id="chuanhu_chatbot",)
|
| 564 |
+
with gr.Row():
|
| 565 |
+
with gr.Column(scale=12):
|
| 566 |
+
user_input = gr.Textbox(
|
| 567 |
+
show_label=False, placeholder="Gib hier deinen Prompt ein...",
|
| 568 |
+
container=False
|
| 569 |
+
)
|
| 570 |
+
with gr.Column(min_width=70, scale=1):
|
| 571 |
+
submitBtn = gr.Button("Senden")
|
| 572 |
+
with gr.Column(min_width=70, scale=1):
|
| 573 |
+
cancelBtn = gr.Button("Stop")
|
| 574 |
+
with gr.Row():
|
| 575 |
+
file_display = gr.File(visible=False)
|
| 576 |
+
image_display = gr.Image( visible=False)
|
| 577 |
+
upload = gr.UploadButton("📁", file_types=["image", "video", "audio"], scale = 10)
|
| 578 |
+
emptyBtn = gr.ClearButton([user_input, chatbot, history, file_display, image_display], value="🧹 Neue Session", scale=10)
|
| 579 |
+
|
| 580 |
+
with gr.Column():
|
| 581 |
+
with gr.Column(min_width=50, scale=1):
|
| 582 |
+
with gr.Tab(label="Parameter Einstellung"):
|
| 583 |
+
#gr.Markdown("# Parameters")
|
| 584 |
+
rag_option = gr.Radio(["Aus", "An"], label="RAG - LI Erweiterungen", value = "Aus")
|
| 585 |
+
model_option = gr.Radio(["OpenAI", "HuggingFace"], label="Modellauswahl", value = "OpenAI")
|
| 586 |
+
|
| 587 |
+
top_p = gr.Slider(
|
| 588 |
+
minimum=-0,
|
| 589 |
+
maximum=1.0,
|
| 590 |
+
value=0.95,
|
| 591 |
+
step=0.05,
|
| 592 |
+
interactive=True,
|
| 593 |
+
label="Top-p",
|
| 594 |
+
)
|
| 595 |
+
temperature = gr.Slider(
|
| 596 |
+
minimum=0.1,
|
| 597 |
+
maximum=2.0,
|
| 598 |
+
value=0.5,
|
| 599 |
+
step=0.1,
|
| 600 |
+
interactive=True,
|
| 601 |
+
label="Temperature",
|
| 602 |
+
)
|
| 603 |
+
max_length_tokens = gr.Slider(
|
| 604 |
+
minimum=0,
|
| 605 |
+
maximum=512,
|
| 606 |
+
value=512,
|
| 607 |
+
step=8,
|
| 608 |
+
interactive=True,
|
| 609 |
+
label="Max Generation Tokens",
|
| 610 |
+
)
|
| 611 |
+
max_context_length_tokens = gr.Slider(
|
| 612 |
+
minimum=0,
|
| 613 |
+
maximum=4096,
|
| 614 |
+
value=2048,
|
| 615 |
+
step=128,
|
| 616 |
+
interactive=True,
|
| 617 |
+
label="Max History Tokens",
|
| 618 |
+
)
|
| 619 |
+
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)
|
| 620 |
+
anzahl_docs = gr.Slider(label="Anzahl Dokumente", 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)
|
| 621 |
+
openai_key = gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1)
|
| 622 |
+
################################################
|
| 623 |
+
# Tab zum Zeichnen mit Stable Diffusion
|
| 624 |
+
################################################
|
| 625 |
+
with gr.Tab(KI zum Zeichnen):
|
| 626 |
+
with gr.Row():
|
| 627 |
+
gr.HTML("LI Zeichnen mit KI")
|
| 628 |
+
status_display = gr.Markdown("Success", elem_id="status_display")
|
| 629 |
+
gr.Markdown(description_top)
|
| 630 |
+
with gr.Row():
|
| 631 |
+
description2 = <strong>Information:</strong> Hier wird ein <strong>Large Language Model (LLM)</strong> zum Zeichnen verwendet. Zur Zeit wird hier Stable Diffusion verwendet.\n\n
|
| 632 |
+
css = .toast-wrap { display: none !important }
|
| 633 |
+
additional_inputs = [
|
| 634 |
+
gr.Slider(label="Temperature", value=0.65, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Höhere Werte erzeugen diversere Antworten", visible=True),
|
| 635 |
+
gr.Slider(label="Max new tokens", value=1024, minimum=0, maximum=4096, step=64, interactive=True, info="Maximale Anzahl neuer Tokens", visible=True),
|
| 636 |
+
gr.Slider(label="Top-p (nucleus sampling)", value=0.6, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Höhere Werte verwenden auch Tokens mit niedrigerer Wahrscheinlichkeit.", visible=True),
|
| 637 |
+
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)
|
| 638 |
+
]
|
| 639 |
+
chatbot_bild = gr.Chatbot()
|
| 640 |
+
chat_interface_bild = gr.ChatInterface(fn=generate_bild,
|
| 641 |
+
additional_inputs = additional_inputs,
|
| 642 |
+
additional_inputs_accordion = gr.Accordion(label="Weitere Eingaben...", open=False),
|
| 643 |
+
title = "ChatGPT vom LI",
|
| 644 |
+
theme="soft",
|
| 645 |
+
chatbot=chatbot_bild,
|
| 646 |
+
retry_btn="🔄 Wiederholen",
|
| 647 |
+
undo_btn="↩️ Letztes löschen",
|
| 648 |
+
clear_btn="🗑️ Verlauf löschen",
|
| 649 |
+
submit_btn = "Abschicken",
|
| 650 |
+
description = description)
|
| 651 |
+
#chatbot_bild.like(vote, None, None)
|
| 652 |
+
|
| 653 |
+
|
| 654 |
gr.Markdown(description)
|
| 655 |
+
|
| 656 |
+
######################################
|
| 657 |
+
# Events und Übergabe Werte an Funktionen
|
| 658 |
+
#######################################
|
| 659 |
+
######################################
|
| 660 |
+
# Für Tab 1: Chatbot
|
| 661 |
#Argumente für generate Funktion als Input
|
| 662 |
predict_args = dict(
|
| 663 |
fn=generate_auswahl,
|
|
|
|
| 698 |
cancelBtn.click(
|
| 699 |
cancels=[predict_event1,predict_event2, predict_event3 ]
|
| 700 |
)
|
|
|
|
| 701 |
|
| 702 |
+
######################################
|
| 703 |
+
# Für Tab 2: Zeichnen
|
| 704 |
+
|
| 705 |
+
|
| 706 |
+
demo.title = "LI-ChatBot"
|
| 707 |
demo.queue().launch(debug=True)
|
| 708 |
|
| 709 |
|