Update app.py
Browse files
app.py
CHANGED
|
@@ -61,6 +61,121 @@ file_path_download = ""
|
|
| 61 |
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
####################################################
|
| 66 |
#aus einem Text-Prompt die Antwort von KI bekommen
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
+
#################################################
|
| 65 |
+
#################################################
|
| 66 |
+
#Funktionen zur Verarbeitung
|
| 67 |
+
################################################
|
| 68 |
+
|
| 69 |
+
##############################################
|
| 70 |
+
#wenn löschen Button geklickt
|
| 71 |
+
def clear_all(history, uploaded_file_paths, chats):
|
| 72 |
+
dic_history = {schluessel: wert for schluessel, wert in history}
|
| 73 |
+
#später wird die summary auf 50 tokens verkürzt, um die Anfrage nicht so teuer werden zu lassen
|
| 74 |
+
#summary wird gebraucht für die Anfrage beim NN, um eine Überschrift des Eintrages zu generieren
|
| 75 |
+
summary = "\n\n".join(f'{schluessel}: \n {wert}' for schluessel, wert in dic_history.items())
|
| 76 |
+
|
| 77 |
+
#falls file mit summay für download existiert hat: das zunächst löschen
|
| 78 |
+
#cleanup(file_path_download)
|
| 79 |
+
#noch nicht im Einsatz, aber hier werden alle Chats einer Sitzung gespeichert
|
| 80 |
+
#den aktuellen Chatverlauf zum Download bereitstellen:
|
| 81 |
+
if chats != {} :
|
| 82 |
+
id_neu = len(chats)+1
|
| 83 |
+
chats[id_neu]= summary
|
| 84 |
+
else:
|
| 85 |
+
chats[0]= summary
|
| 86 |
+
|
| 87 |
+
#Eine Überschrift zu dem jeweiligen Chatverlauf finden - abhängig vom Inhalt
|
| 88 |
+
#file_path_download = save_and_download(summary)
|
| 89 |
+
headers, payload = process_chatverlauf(summary, MODEL_NAME, OAI_API_KEY)
|
| 90 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 91 |
+
#als json ausgeben
|
| 92 |
+
data = response.json()
|
| 93 |
+
# Den "content" auswählen, da dort die Antwort der Ki enthalten ist
|
| 94 |
+
result = data['choices'][0]['message']['content']
|
| 95 |
+
worte = result.split()
|
| 96 |
+
if len(worte) > 2:
|
| 97 |
+
file_path_download = "data/" + str(len(chats)) + "_Chatverlauf.pdf"
|
| 98 |
+
else:
|
| 99 |
+
file_path_download = "data/" + str(len(chats)) + "_" + result + ".pdf"
|
| 100 |
+
|
| 101 |
+
erstellePdf(file_path_download, result, dic_history)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
#die session variable in gradio erweitern und alle fliepath neu in das gr.File hochladen
|
| 105 |
+
uploaded_file_paths= uploaded_file_paths + [file_path_download]
|
| 106 |
+
|
| 107 |
+
return None, gr.Image(visible=False), uploaded_file_paths, [], gr.File(uploaded_file_paths, label="Download-Chatverläufe", visible=True, file_count="multiple", interactive = False), chats
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
#wenn löschen Button geklickt
|
| 111 |
+
def clear_all3(history):
|
| 112 |
+
#die session variable in gradio erweitern und alle fliepath neu in das gr.File hochladen
|
| 113 |
+
uploaded_file_paths= ""
|
| 114 |
+
return None, gr.Image(visible=False), [],
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
##############################################
|
| 119 |
+
#History - die Frage oder das File eintragen...
|
| 120 |
+
#in history_file ist ein file gespeichert, falls voher im Verlauf schon ein File hochgeladen wurde.
|
| 121 |
+
#wird ein neuer File hochgeladen, so wird history_fiel dadurch ersetzt
|
| 122 |
+
def add_text(chatbot, history, prompt, file, file_history):
|
| 123 |
+
if (file == None):
|
| 124 |
+
chatbot = chatbot +[(prompt, None)]
|
| 125 |
+
else:
|
| 126 |
+
file_history = file
|
| 127 |
+
if (prompt == ""):
|
| 128 |
+
chatbot=chatbot + [((file.name,), "Prompt fehlt!")]
|
| 129 |
+
else:
|
| 130 |
+
ext = analyze_file(file)
|
| 131 |
+
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 132 |
+
chatbot = chatbot +[((file.name,), None), (prompt, None)]
|
| 133 |
+
else:
|
| 134 |
+
chatbot = chatbot +[("Hochgeladenes Dokument: "+ get_filename(file) +"\n" + prompt, None)]
|
| 135 |
+
|
| 136 |
+
return chatbot, history, prompt, file, file_history, gr.Image(visible = False), "" #gr.Image( label=None, size=(30,30), visible=False, scale=1) #gr.Textbox(value="", interactive=False)
|
| 137 |
+
|
| 138 |
+
def add_text2(chatbot, prompt):
|
| 139 |
+
if (prompt == ""):
|
| 140 |
+
chatbot = chatbot + [("", "Prompt fehlt!")]
|
| 141 |
+
else:
|
| 142 |
+
chatbot = chatbot + [(prompt, None)]
|
| 143 |
+
print("chatbot nach add_text............")
|
| 144 |
+
print(chatbot)
|
| 145 |
+
return chatbot, prompt, ""
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
############################################
|
| 149 |
+
#nach dem Upload soll das zusätzliche Fenster mit dem image drinnen angezeigt werden
|
| 150 |
+
def file_anzeigen(file):
|
| 151 |
+
ext = analyze_file(file)
|
| 152 |
+
if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
|
| 153 |
+
return gr.Image(width=47, visible=True, interactive = False, height=47, min_width=47, show_label=False, show_share_button=False, show_download_button=False, scale = 0.5), file, file
|
| 154 |
+
else:
|
| 155 |
+
return gr.Image(width=47, visible=True, interactive = False, height=47, min_width=47, show_label=False, show_share_button=False, show_download_button=False, scale = 0.5), "data/file.png", file
|
| 156 |
+
|
| 157 |
+
def file_loeschen():
|
| 158 |
+
return None, gr.Image(visible = False)
|
| 159 |
+
|
| 160 |
+
############################################
|
| 161 |
+
#wenn 'Stop' Button geklickt, dann Message dazu und das Eingabe-Fenster leeren
|
| 162 |
+
def cancel_outputing():
|
| 163 |
+
reset_textbox()
|
| 164 |
+
return "Stop Done"
|
| 165 |
+
|
| 166 |
+
def reset_textbox():
|
| 167 |
+
return gr.update(value=""),""
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
##########################################
|
| 171 |
+
#Hilfsfunktion, um ein von Stable Diffusion erzeugtes Bild für die Ausgabe in der History vorzubereiten
|
| 172 |
+
def umwandeln_fuer_anzeige(image):
|
| 173 |
+
buffer = io.BytesIO()
|
| 174 |
+
image.save(buffer, format='PNG')
|
| 175 |
+
return buffer.getvalue()
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
|
| 180 |
####################################################
|
| 181 |
#aus einem Text-Prompt die Antwort von KI bekommen
|