Update utils.py
Browse files
utils.py
CHANGED
|
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type
|
|
| 3 |
import logging
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
-
import datetime
|
| 7 |
import hashlib
|
| 8 |
import csv
|
| 9 |
import requests
|
|
@@ -58,6 +58,14 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 58 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 59 |
import numpy as np
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
logging.basicConfig(
|
| 63 |
level=logging.INFO,
|
|
@@ -734,6 +742,74 @@ def create_picture(history, prompt):
|
|
| 734 |
return image
|
| 735 |
|
| 736 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
|
| 738 |
###################################################
|
| 739 |
#zur Zeit nicht im Gebrauch
|
|
@@ -771,4 +847,20 @@ def is_stop_word_or_prefix(s: str, stop_words: list) -> bool:
|
|
| 771 |
return True
|
| 772 |
return False
|
| 773 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
|
|
|
|
| 3 |
import logging
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
import hashlib
|
| 8 |
import csv
|
| 9 |
import requests
|
|
|
|
| 58 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 59 |
import numpy as np
|
| 60 |
|
| 61 |
+
from reportlab.lib.pagesizes import inch, A4
|
| 62 |
+
from reportlab.platypus import SimpleDocTemplate, Frame, Spacer
|
| 63 |
+
from reportlab.lib import colors
|
| 64 |
+
from reportlab.lib.units import mm
|
| 65 |
+
from reportlab.platypus import Paragraph, SimpleDocTemplate, Frame, Image, Table
|
| 66 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 67 |
+
from reportlab.lib.units import cm
|
| 68 |
+
|
| 69 |
|
| 70 |
logging.basicConfig(
|
| 71 |
level=logging.INFO,
|
|
|
|
| 742 |
return image
|
| 743 |
|
| 744 |
|
| 745 |
+
########################################
|
| 746 |
+
# Ausgabe in PDF des Chathistory
|
| 747 |
+
########################################
|
| 748 |
+
#callback Methode, die auf jeder PDF Seite das Datum oben hinschreibt
|
| 749 |
+
def on_each_page(canvas, doc):
|
| 750 |
+
page_width, page_height = A4 # Oder das von Ihnen verwendete Seitenformat, z.B. A4
|
| 751 |
+
canvas.saveState()
|
| 752 |
+
canvas.setFont('Times-Roman', 10)
|
| 753 |
+
# Formatieren Sie das Datum nach Ihrem Wunsch
|
| 754 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 755 |
+
print(current_date)
|
| 756 |
+
# Positionieren Sie das Datum oben rechts auf der Seite
|
| 757 |
+
canvas.drawRightString(page_width - 72, page_height - 28, current_date)
|
| 758 |
+
canvas.restoreState()
|
| 759 |
+
|
| 760 |
+
#PDF Inhalte zusammenstellen und PDF unter dem angegebene Pfad file_path_download ablegen
|
| 761 |
+
def erstellePdf(file_path_download, ueberschrift, dic_history):
|
| 762 |
+
# Initialisiere eine leere Listseinstellung ("flowables"), die später gefüllt wird
|
| 763 |
+
elements = []
|
| 764 |
+
# Definiere ein neues Papierformat mit A4 Maßen
|
| 765 |
+
paper_size = A4
|
| 766 |
+
|
| 767 |
+
# Erstellen Sie ein neues StyleSheet-Objekt
|
| 768 |
+
styles = getSampleStyleSheet()
|
| 769 |
+
# Neuen Style hinzufügen
|
| 770 |
+
new_style = ParagraphStyle('NewStyle', fontName='Helvetica', fontSize=12)
|
| 771 |
+
styles.add(new_style)
|
| 772 |
+
#style für Trennlinie
|
| 773 |
+
line_style = ParagraphStyle('LineStyle', fontSize=4, leading=6, borderPadding=0,
|
| 774 |
+
spaceBefore=0, spaceAfter=0, textColor='black')
|
| 775 |
+
#Feststehende Überschriften erzeugen
|
| 776 |
+
# Chat-Überschrift
|
| 777 |
+
title = Paragraph(ueberschrift, styles['Title'])
|
| 778 |
+
headline_nutzer = Paragraph('Nutzer:', styles['Heading3'])
|
| 779 |
+
headline_assi = Paragraph('Assistent:', styles['Heading3'])
|
| 780 |
+
|
| 781 |
+
#Pdf Abschnittsweise zusammenstellen
|
| 782 |
+
elements.append(title)
|
| 783 |
+
for nutzer, assi in dic_history.items():
|
| 784 |
+
elements.append(headline_nutzer)
|
| 785 |
+
p = Paragraph(nutzer, styles['NewStyle'])
|
| 786 |
+
elements.append(p)
|
| 787 |
+
# Einen Abstand hinzufügen (optional)
|
| 788 |
+
elements.append(Spacer(1, 2*mm))
|
| 789 |
+
elements.append(headline_assi)
|
| 790 |
+
p = Paragraph(assi, styles['NewStyle'])
|
| 791 |
+
elements.append(p)
|
| 792 |
+
# Einen Abstand hinzufügen (optional)
|
| 793 |
+
elements.append(Spacer(1, 8*mm))
|
| 794 |
+
#Trennlinie
|
| 795 |
+
elements.append(Paragraph('_' * 100, line_style))
|
| 796 |
+
# Einen Abstand hinzufügen (optional)
|
| 797 |
+
elements.append(Spacer(1, 8*mm))
|
| 798 |
+
|
| 799 |
+
#Für später, um bilder einzufügen
|
| 800 |
+
# Fügen Sie andere Flowables wie Bilder oder Tabellen hinzu
|
| 801 |
+
#image = Image('path/to/your/image.png', width=10*cm, height=5*cm)
|
| 802 |
+
#elements.append(image)
|
| 803 |
+
#table = Table([['Cell 1', 'Cell 2'], ['Cell 3', 'Cell 4']])
|
| 804 |
+
#elements.append(table)
|
| 805 |
+
|
| 806 |
+
|
| 807 |
+
# Generiere das PDF-Dokument
|
| 808 |
+
doc = CustomDocTemplate(file_path_download, pagesize=paper_size)
|
| 809 |
+
#on_each_page ist eine callback Methode, die auf jeder neuen PDF Seite ausgeführt wird
|
| 810 |
+
doc.onPage = on_each_page
|
| 811 |
+
doc.build(elements)
|
| 812 |
+
|
| 813 |
|
| 814 |
###################################################
|
| 815 |
#zur Zeit nicht im Gebrauch
|
|
|
|
| 847 |
return True
|
| 848 |
return False
|
| 849 |
|
| 850 |
+
##########################################
|
| 851 |
+
# Klasse, die die SimpleDocTemplate überschreibt (für PDF Generierung)
|
| 852 |
+
# war nötig, da SimpleDocTemplate die on_each_page nicht ausgeführt hat - warum auch immer ...
|
| 853 |
+
##########################################
|
| 854 |
+
class CustomDocTemplate(SimpleDocTemplate):
|
| 855 |
+
def handle_pageBegin(self):
|
| 856 |
+
# Sorgt dafür, dass die Standard-Page-Begin-Logik ausgeführt wird
|
| 857 |
+
self._handle_pageBegin()
|
| 858 |
+
# Jetzt können Sie das Canvas-Objekt über self.canv sicher verwenden
|
| 859 |
+
self.canv.saveState()
|
| 860 |
+
self.canv.setFont('Helvetica', 10)
|
| 861 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 862 |
+
# Passen Sie hier die Positionierung an Ihre Bedürfnisse an
|
| 863 |
+
self.canv.drawRightString(550, 800, current_date) # Position anpassen
|
| 864 |
+
self.canv.restoreState()
|
| 865 |
+
|
| 866 |
|