Update app.py
Browse files
app.py
CHANGED
|
@@ -154,7 +154,16 @@ general_assistant_suche= openai_assistant_suche(client)
|
|
| 154 |
def clear_all(history, uploaded_file_paths, chats):
|
| 155 |
dic_history = {schluessel: wert for schluessel, wert in history}
|
| 156 |
#später wird die summary auf 50 tokens verkürzt, um die Anfrage nicht so teuer werden zu lassen
|
|
|
|
| 157 |
summary = "\n\n".join(f'{schluessel}: \n {wert}' for schluessel, wert in dic_history.items())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
#falls file mit summay für download existiert hat: das zunächst löschen
|
| 160 |
#cleanup(file_path_download)
|
|
@@ -162,9 +171,9 @@ def clear_all(history, uploaded_file_paths, chats):
|
|
| 162 |
#den aktuellen Chatverlauf zum Download bereitstellen:
|
| 163 |
if chats != {} :
|
| 164 |
id_neu = len(chats)+1
|
| 165 |
-
chats[id_neu]=summary
|
| 166 |
else:
|
| 167 |
-
chats[0]=summary
|
| 168 |
|
| 169 |
#Eine Überschrift zu dem jeweiligen Chatverlauf finden - abhängig vom Inhalt
|
| 170 |
#file_path_download = save_and_download(summary)
|
|
@@ -180,16 +189,39 @@ def clear_all(history, uploaded_file_paths, chats):
|
|
| 180 |
else:
|
| 181 |
file_path_download = "data/" + str(len(chats)) + "_" + result + ".pdf"
|
| 182 |
|
| 183 |
-
#
|
| 184 |
-
"""
|
| 185 |
-
with open(file_path_download, 'w') as file:
|
| 186 |
-
# String in die Datei schreiben
|
| 187 |
-
file.write(summary)
|
| 188 |
-
"""
|
| 189 |
# Erzeuge einen Bytestream
|
| 190 |
memoryFile = io.BytesIO()
|
| 191 |
# Erstelle eine Canvas-Instanz im Bytestream
|
| 192 |
c = canvas.Canvas(memoryFile, pagesize=letter) #oder A4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
# Setze die Schriftart und -grösse
|
| 194 |
c.setFont("Helvetica", 12)
|
| 195 |
# Fülle die Canvas mit dem Inhalt der Textdatei
|
|
@@ -443,7 +475,7 @@ def generate_text_zu_bild(file, prompt, k, rag_option, chatbot, history, db):
|
|
| 443 |
def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history, db):
|
| 444 |
global splittet
|
| 445 |
print("text mit doc ..............................")
|
| 446 |
-
|
| 447 |
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 448 |
if (rag_option == "An"):
|
| 449 |
print("Doc mit RAG..............................")
|
|
@@ -500,8 +532,8 @@ def generate_text (prompt, chatbot, history, rag_option, model_option, openai_ap
|
|
| 500 |
#oder an Hugging Face --------------------------
|
| 501 |
print("HF Anfrage.......................")
|
| 502 |
model_kwargs={"temperature": 0.5, "max_length": 512, "num_return_sequences": 1, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty}
|
| 503 |
-
llm = HuggingFaceHub(repo_id=repo_id, model_kwargs=model_kwargs)
|
| 504 |
-
|
| 505 |
#llm = HuggingFaceHub(url_??? = "https://wdgsjd6zf201mufn.us-east-1.aws.endpoints.huggingface.cloud", model_kwargs={"temperature": 0.5, "max_length": 64})
|
| 506 |
#llm = HuggingFaceTextGenInference( inference_server_url="http://localhost:8010/", max_new_tokens=max_new_tokens,top_k=10,top_p=top_p,typical_p=0.95,temperature=temperature,repetition_penalty=repetition_penalty,)
|
| 507 |
#llm via HuggingChat
|
|
@@ -739,7 +771,7 @@ with gr.Blocks(css=custom_css(), theme=themeAlex) as demo:
|
|
| 739 |
with gr.Tab(label="Parameter"):
|
| 740 |
#gr.Markdown("# Parameters")
|
| 741 |
rag_option = gr.Radio(["Aus", "An"], label="LI Erweiterungen (RAG)", value = "Aus")
|
| 742 |
-
model_option = gr.Radio(["OpenAI", "HuggingFace"], label="Modellauswahl", value = "
|
| 743 |
websuche = gr.Radio(["Aus", "An"], label="Web-Suche", value = "Aus")
|
| 744 |
|
| 745 |
|
|
|
|
| 154 |
def clear_all(history, uploaded_file_paths, chats):
|
| 155 |
dic_history = {schluessel: wert for schluessel, wert in history}
|
| 156 |
#später wird die summary auf 50 tokens verkürzt, um die Anfrage nicht so teuer werden zu lassen
|
| 157 |
+
#summary wird gebraucht für die Anfrage beim NN, um eine Überschrift des Eintrages zu generieren
|
| 158 |
summary = "\n\n".join(f'{schluessel}: \n {wert}' for schluessel, wert in dic_history.items())
|
| 159 |
+
# um den Chatverlauf für das PDF vorzubereiten:
|
| 160 |
+
for key, value in dic_history.items():
|
| 161 |
+
if key == 'timestamp':
|
| 162 |
+
chat_history[key] = value
|
| 163 |
+
elif key == 'user':
|
| 164 |
+
chat_history[key] = value
|
| 165 |
+
elif key == 'assistant':
|
| 166 |
+
chat_history[key] = value
|
| 167 |
|
| 168 |
#falls file mit summay für download existiert hat: das zunächst löschen
|
| 169 |
#cleanup(file_path_download)
|
|
|
|
| 171 |
#den aktuellen Chatverlauf zum Download bereitstellen:
|
| 172 |
if chats != {} :
|
| 173 |
id_neu = len(chats)+1
|
| 174 |
+
chats[id_neu]= summary
|
| 175 |
else:
|
| 176 |
+
chats[0]= summary
|
| 177 |
|
| 178 |
#Eine Überschrift zu dem jeweiligen Chatverlauf finden - abhängig vom Inhalt
|
| 179 |
#file_path_download = save_and_download(summary)
|
|
|
|
| 189 |
else:
|
| 190 |
file_path_download = "data/" + str(len(chats)) + "_" + result + ".pdf"
|
| 191 |
|
| 192 |
+
#chat_history in ein File laden
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
# Erzeuge einen Bytestream
|
| 194 |
memoryFile = io.BytesIO()
|
| 195 |
# Erstelle eine Canvas-Instanz im Bytestream
|
| 196 |
c = canvas.Canvas(memoryFile, pagesize=letter) #oder A4
|
| 197 |
+
|
| 198 |
+
#Inhalt des PDF formatieren:
|
| 199 |
+
# Schriftarten
|
| 200 |
+
TITLE_FONT = Font(Family="Times-Bold", Size=14)
|
| 201 |
+
TEXT_FONT = Font(Family="Courier", Size=12)
|
| 202 |
+
CODE_FONT = Font(Family="Monaco", Size=12)
|
| 203 |
+
# Breite und Höhe für Spalten
|
| 204 |
+
COLUMN_WIDTH = 150
|
| 205 |
+
ROW_HEIGHT = 20
|
| 206 |
+
# Bereiche für Spalten
|
| 207 |
+
TIMESTAMP_X = 50
|
| 208 |
+
USER_X = TIMESTAMP_X + COLUMN_WIDTH
|
| 209 |
+
ASSISTANT_X = USER_X + COLUMN_WIDTH
|
| 210 |
+
# Rand und Abstand zwischen Zeilen
|
| 211 |
+
MARGIN = 50
|
| 212 |
+
LINE_SPACING = 10
|
| 213 |
+
# Zeitstempel
|
| 214 |
+
c.setFillColorRGB(0, 0, 0)
|
| 215 |
+
c.setFont(TITLE_FONT)
|
| 216 |
+
c.drawCentredString(TIMESTAMP_X + COLUMN_WIDTH / 2, MARGIN, chat_history["timestamp"])
|
| 217 |
+
# User und Assistant
|
| 218 |
+
c.setFont(TEXT_FONT)
|
| 219 |
+
c.drawString(USER_X, MARGIN + LINE_SPACING, chat_history["user"] + ": ")
|
| 220 |
+
c.setFont(CODE_FONT)
|
| 221 |
+
c.drawString(USER_X + COLUMN_WIDTH, MARGIN + LINE_SPACING, chat_history["assistant"])
|
| 222 |
+
# Linie trennen
|
| 223 |
+
c.line(TIMESTAMP_X, MARGIN + LINE_SPACING + ROW_HEIGHT, ASSISTANT_X + COLUMN_WIDTH, MARGIN + LINE_SPACING + ROW_HEIGHT)
|
| 224 |
+
|
| 225 |
# Setze die Schriftart und -grösse
|
| 226 |
c.setFont("Helvetica", 12)
|
| 227 |
# Fülle die Canvas mit dem Inhalt der Textdatei
|
|
|
|
| 475 |
def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history, db):
|
| 476 |
global splittet
|
| 477 |
print("text mit doc ..............................")
|
| 478 |
+
|
| 479 |
prompt_neu = generate_prompt_with_history(prompt, history)
|
| 480 |
if (rag_option == "An"):
|
| 481 |
print("Doc mit RAG..............................")
|
|
|
|
| 532 |
#oder an Hugging Face --------------------------
|
| 533 |
print("HF Anfrage.......................")
|
| 534 |
model_kwargs={"temperature": 0.5, "max_length": 512, "num_return_sequences": 1, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty}
|
| 535 |
+
#llm = HuggingFaceHub(repo_id=repo_id, model_kwargs=model_kwargs)
|
| 536 |
+
llm = HuggingFaceChain(model=MODEL_NAME_HF, model_kwargs={"temperature": 0.5, "max_length": 128})
|
| 537 |
#llm = HuggingFaceHub(url_??? = "https://wdgsjd6zf201mufn.us-east-1.aws.endpoints.huggingface.cloud", model_kwargs={"temperature": 0.5, "max_length": 64})
|
| 538 |
#llm = HuggingFaceTextGenInference( inference_server_url="http://localhost:8010/", max_new_tokens=max_new_tokens,top_k=10,top_p=top_p,typical_p=0.95,temperature=temperature,repetition_penalty=repetition_penalty,)
|
| 539 |
#llm via HuggingChat
|
|
|
|
| 771 |
with gr.Tab(label="Parameter"):
|
| 772 |
#gr.Markdown("# Parameters")
|
| 773 |
rag_option = gr.Radio(["Aus", "An"], label="LI Erweiterungen (RAG)", value = "Aus")
|
| 774 |
+
model_option = gr.Radio(["OpenAI", "HuggingFace"], label="Modellauswahl", value = "HuggingFace")
|
| 775 |
websuche = gr.Radio(["Aus", "An"], label="Web-Suche", value = "Aus")
|
| 776 |
|
| 777 |
|