Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import io
|
|
| 9 |
from PIL import Image, ImageDraw, ImageOps, ImageFont
|
| 10 |
import base64
|
| 11 |
import tempfile
|
|
|
|
| 12 |
|
| 13 |
from hugchat import hugchat
|
| 14 |
from hugchat.login import Login
|
|
@@ -149,10 +150,8 @@ general_assistant_suche= openai_assistant_suche(client)
|
|
| 149 |
#wenn löschen Button geklickt
|
| 150 |
def clear_all(history, uploaded_file_paths, chats):
|
| 151 |
dic_history = {schluessel: wert for schluessel, wert in history}
|
| 152 |
-
#summary
|
| 153 |
-
|
| 154 |
-
erster_eintrag = next(iter(dic_history.items()))
|
| 155 |
-
summary = "\n\n".join(f'{erster_eintrag[0]}: \n {erster_eintrag[1]}')
|
| 156 |
|
| 157 |
#falls file mit summay für download existiert hat: das zunächst löschen
|
| 158 |
#cleanup(file_path_download)
|
|
@@ -163,7 +162,8 @@ def clear_all(history, uploaded_file_paths, chats):
|
|
| 163 |
chats[id_neu]=summary
|
| 164 |
else:
|
| 165 |
chats[0]=summary
|
| 166 |
-
|
|
|
|
| 167 |
#file_path_download = save_and_download(summary)
|
| 168 |
headers, payload = process_chatverlauf(summary, MODEL_NAME, OAI_API_KEY)
|
| 169 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
@@ -176,9 +176,30 @@ def clear_all(history, uploaded_file_paths, chats):
|
|
| 176 |
file_path_download = "data/" + str(len(chats)) + "_Chatverlauf"
|
| 177 |
else:
|
| 178 |
file_path_download = "data/" + str(len(chats)) + "_" + result
|
|
|
|
|
|
|
|
|
|
| 179 |
with open(file_path_download, 'w') as file:
|
| 180 |
# String in die Datei schreiben
|
| 181 |
file.write(summary)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
#die session variable in gradio erweitern und alle fliepath neu in das gr.File hochladen
|
| 184 |
uploaded_file_paths= uploaded_file_paths + [file_path_download]
|
|
|
|
| 9 |
from PIL import Image, ImageDraw, ImageOps, ImageFont
|
| 10 |
import base64
|
| 11 |
import tempfile
|
| 12 |
+
from reportlab.pdfgen import canvas
|
| 13 |
|
| 14 |
from hugchat import hugchat
|
| 15 |
from hugchat.login import Login
|
|
|
|
| 150 |
#wenn löschen Button geklickt
|
| 151 |
def clear_all(history, uploaded_file_paths, chats):
|
| 152 |
dic_history = {schluessel: wert for schluessel, wert in history}
|
| 153 |
+
#später wird die summary auf 50 tokens verkürzt, um die Anfrage nicht so teuer werden zu lassen
|
| 154 |
+
summary = "\n\n".join(f'{schluessel}: \n {wert}' for schluessel, wert in dic_history.items())
|
|
|
|
|
|
|
| 155 |
|
| 156 |
#falls file mit summay für download existiert hat: das zunächst löschen
|
| 157 |
#cleanup(file_path_download)
|
|
|
|
| 162 |
chats[id_neu]=summary
|
| 163 |
else:
|
| 164 |
chats[0]=summary
|
| 165 |
+
|
| 166 |
+
#Eine Überschrift zu dem jeweiligen Chatverlauf finden - abhängig vom Inhalt
|
| 167 |
#file_path_download = save_and_download(summary)
|
| 168 |
headers, payload = process_chatverlauf(summary, MODEL_NAME, OAI_API_KEY)
|
| 169 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
|
|
| 176 |
file_path_download = "data/" + str(len(chats)) + "_Chatverlauf"
|
| 177 |
else:
|
| 178 |
file_path_download = "data/" + str(len(chats)) + "_" + result
|
| 179 |
+
|
| 180 |
+
#summary in ein File laden
|
| 181 |
+
"""
|
| 182 |
with open(file_path_download, 'w') as file:
|
| 183 |
# String in die Datei schreiben
|
| 184 |
file.write(summary)
|
| 185 |
+
"""
|
| 186 |
+
# Erzeuge einen Bytestream
|
| 187 |
+
memoryFile = io.BytesIO()
|
| 188 |
+
# Erstelle eine Canvas-Instanz im Bytestream
|
| 189 |
+
c = canvas.Canvas(memoryFile, pagesize=letter)
|
| 190 |
+
# Setze die Schriftart und -grösse
|
| 191 |
+
c.setFont("Helvetica", 12)
|
| 192 |
+
# Fülle die Canvas mit dem Inhalt der Textdatei
|
| 193 |
+
c.drawString(100, 750, summary)
|
| 194 |
+
# Speicher die Canvas im Bytestream
|
| 195 |
+
c.save()
|
| 196 |
+
# Lege den Cursor zurück zum Anfang des Bytestreams
|
| 197 |
+
memoryFile.seek(0)
|
| 198 |
+
# Öffne eine neue PDF-Datei und kopiere den Inhalt des Bytestreams hinein
|
| 199 |
+
with open(file_path_download, 'wb') as outf:
|
| 200 |
+
outf.write(memoryFile.read())
|
| 201 |
+
|
| 202 |
+
|
| 203 |
|
| 204 |
#die session variable in gradio erweitern und alle fliepath neu in das gr.File hochladen
|
| 205 |
uploaded_file_paths= uploaded_file_paths + [file_path_download]
|