Update app.py
Browse files
app.py
CHANGED
@@ -36,9 +36,72 @@ file_path_download = ""
|
|
36 |
|
37 |
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
|
41 |
-
|
42 |
def get_rag_response(question):
|
43 |
# Abfrage der relevanten Dokumente aus Chroma DB
|
44 |
docs = chroma_db.search(question, top_k=5)
|
@@ -117,19 +180,202 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, top_p=0.6,
|
|
117 |
|
118 |
|
119 |
|
120 |
-
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
|
38 |
|
39 |
+
|
40 |
+
####################################################
|
41 |
+
#aus einem Text-Prompt die Antwort von KI bekommen
|
42 |
+
#mit oder ohne RAG möglich
|
43 |
+
def generate_text (prompt, chatbot, history, rag_option, model_option, openai_api_key, vektordatenbank, websuche, k=3, top_p=0.6, temperature=0.2, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3, top_k=35):
|
44 |
+
print("Text pur..............................")
|
45 |
+
if (prompt == ""):
|
46 |
+
raise gr.Error("Prompt ist erforderlich.")
|
47 |
+
|
48 |
+
try:
|
49 |
+
#oder an Hugging Face --------------------------
|
50 |
+
print("HF Anfrage.......................")
|
51 |
+
model_kwargs={"temperature": 0.5, "max_length": 512, "num_return_sequences": 1, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty}
|
52 |
+
llm = HuggingFaceHub(repo_id=repo_id, model_kwargs=model_kwargs)
|
53 |
+
llm = HuggingFaceChain(model=MODEL_NAME_HF, model_kwargs={"temperature": 0.5, "max_length": 128})
|
54 |
+
|
55 |
+
#Prompt an history anhängen und einen Text daraus machen
|
56 |
+
history_text_und_prompt = generate_prompt_with_history(prompt, history)
|
57 |
+
|
58 |
+
#zusätzliche Dokumenten Splits aus DB zum Prompt hinzufügen (aus VektorDB - Chroma oder Mongo DB)
|
59 |
+
print("LLM aufrufen mit RAG: ...........")
|
60 |
+
result = rag_chain(llm, history_text_und_prompt, retriever) #für hugchat noch kein rag möglich...
|
61 |
+
#weitere Möglichkeit für Rag-Chain - dann auch für HF Modelle möglich, da kein llm in Langchain übergeben werden muss...
|
62 |
+
#result = rag_chain2(history_text_und_prompt, db, 5)
|
63 |
+
print("result regchain.....................")
|
64 |
+
print(result)
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
raise gr.Error(e)
|
68 |
+
|
69 |
+
return result, suche_im_Netz
|
70 |
+
|
71 |
+
|
72 |
+
########################################
|
73 |
+
# Bot- test gegen schädliche Bots die die Anwendung testen...
|
74 |
+
# Funktion zur Überprüfung der Benutzereingabe
|
75 |
+
# Funktion zur Überprüfung der Eingabe und Aktivierung der Hauptanwendung
|
76 |
+
def validate_input(user_input_validate, validate=False):
|
77 |
+
user_input_hashed = hash_input(user_input_validate)
|
78 |
+
if user_input_hashed == hash_input(ANTI_BOT_PW):
|
79 |
+
return "Richtig! Weiter gehts... ", True, gr.Textbox(visible=False), gr.Button(visible=False)
|
80 |
+
else:
|
81 |
+
return "Falsche Antwort!!!!!!!!!", False, gr.Textbox(label = "", placeholder="Bitte tippen Sie das oben im Moodle Kurs angegebene Wort ein, um zu beweisen, dass Sie kein Bot sind.", visible=True, scale= 5), gr.Button("Validieren", visible = True)
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
def custom_css():
|
86 |
+
return """
|
87 |
+
body, html {
|
88 |
+
background-color: #303030; /* Dunkler Hintergrund */
|
89 |
+
color:#353535;
|
90 |
+
}
|
91 |
+
"""
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
|
103 |
|
104 |
+
#nicht in Gebrauch...................................
|
105 |
def get_rag_response(question):
|
106 |
# Abfrage der relevanten Dokumente aus Chroma DB
|
107 |
docs = chroma_db.search(question, top_k=5)
|
|
|
180 |
|
181 |
|
182 |
|
183 |
+
#############################################################################################
|
184 |
+
# Start Gui Vorabfrage
|
185 |
+
# Validierungs-Interface - Bots weghalten...
|
186 |
+
print ("Start GUI Vorabfrage")
|
187 |
+
#################################################################################################
|
188 |
+
print ("Start GUI Hauptanwendung")
|
189 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
190 |
+
customCSS = f.read()
|
191 |
+
|
192 |
+
#Add Inputs für Tab 2
|
193 |
+
additional_inputs = [
|
194 |
+
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),
|
195 |
+
gr.Slider(label="Max new tokens", value=1024, minimum=0, maximum=4096, step=64, interactive=True, info="Maximale Anzahl neuer Tokens", visible=True),
|
196 |
+
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),
|
197 |
+
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)
|
198 |
+
]
|
199 |
+
with gr.Blocks(css=customCSS, theme=themeAlex) as demo:
|
200 |
+
#validiert speichern
|
201 |
+
validate = gr.State(False)
|
202 |
+
#Session Variablen, um Weete zu speichern, auch wenn die Felder in der GUI bereits wieder leer sind
|
203 |
+
# history parallel zu chatbot speichern - da in chatbot bei Bildern zum Anzeigen in der GUI die Bilder speziell formatiert werden,
|
204 |
+
# für die Übergabe an die ki aber der Pfad zum Bild behalten werden muss - was in der history der Fall ist!
|
205 |
+
history = gr.State([])
|
206 |
+
uploaded_file_paths= gr.State([])
|
207 |
+
history3 = gr.State([])
|
208 |
+
uploaded_file_paths3= gr.State([])
|
209 |
+
#alle chats einer Session sammeln
|
210 |
+
chats = gr.State({})
|
211 |
+
#damit der Prompt auch nach dem upload in die History noch für predicts_args verfügbar ist
|
212 |
+
user_question = gr.State("")
|
213 |
+
#für die anderen Tabs auch...
|
214 |
+
#damit der Prompt auch nach dem upload in die History noch für predicts_args verfügbar ist
|
215 |
+
user_question2 = gr.State("")
|
216 |
+
user_question3 = gr.State("")
|
217 |
+
attached_file = gr.State(None)
|
218 |
+
attached_file_history = gr.State(None)
|
219 |
+
attached_file3 = gr.State(None)
|
220 |
+
attached_file_history3 = gr.State(None)
|
221 |
+
status_display = gr.State("")
|
222 |
+
status_display2 = gr.State("")
|
223 |
+
status_display3 = gr.State("")
|
224 |
+
################################################
|
225 |
+
# Tab zum Chatbot mit Text oder Bildeingabe
|
226 |
+
################################################
|
227 |
+
gr.Markdown(description_top)
|
228 |
+
with gr.Row():
|
229 |
+
user_input_validate =gr.Textbox(label= "Bitte das oben im Moodle Kurs angegebene Wort eingeben, um die Anwendung zu starten", visible=True, interactive=True, scale= 7)
|
230 |
+
validate_btn = gr.Button("Validieren", visible = True)
|
231 |
+
#validation_result = gr.Text(label="Validierungsergebnis")
|
232 |
+
|
233 |
+
with gr.Tab("KKG Chatbot"):
|
234 |
+
with gr.Row():
|
235 |
+
#gr.HTML("LI Chatot")
|
236 |
+
status_display = gr.Markdown("Antwort der KI ...", visible = True) #, elem_id="status_display")
|
237 |
+
with gr.Row():
|
238 |
+
with gr.Column(scale=5):
|
239 |
+
with gr.Row():
|
240 |
+
chatbot = gr.Chatbot(elem_id="li-chat",show_copy_button=True)
|
241 |
+
with gr.Row():
|
242 |
+
with gr.Column(scale=12):
|
243 |
+
user_input = gr.Textbox(
|
244 |
+
show_label=False, placeholder="Gib hier deinen Prompt ein...",
|
245 |
+
container=False
|
246 |
+
)
|
247 |
+
with gr.Column(min_width=70, scale=1):
|
248 |
+
submitBtn = gr.Button("Senden")
|
249 |
+
with gr.Column(min_width=70, scale=1):
|
250 |
+
cancelBtn = gr.Button("Stop")
|
251 |
+
with gr.Row():
|
252 |
+
image_display = gr.Image( visible=False)
|
253 |
+
upload = gr.UploadButton("📁", file_types=["image", "pdf", "docx", "pptx", "xlsx"], scale = 10)
|
254 |
+
emptyBtn = gr.ClearButton([user_input, chatbot, history, attached_file, attached_file_history, image_display], value="🧹 Neue Session", scale=10)
|
255 |
+
|
256 |
+
with gr.Column():
|
257 |
+
with gr.Column(min_width=50, scale=1):
|
258 |
+
with gr.Tab(label="Chats ..."):
|
259 |
+
#Geht nicht, da für alle gleichzeitig sichtbar
|
260 |
+
#chat_selector = gr.CheckboxGroup(label="", choices=update_chat_options())
|
261 |
+
#download_button = gr.Button("Download ausgewählte Chats")
|
262 |
+
file_download = gr.File(label="Noch keine Chatsverläufe", visible=True, interactive = False, file_count="multiple",)
|
263 |
+
|
264 |
+
with gr.Tab(label="Parameter"):
|
265 |
+
#gr.Markdown("# Parameters")
|
266 |
+
rag_option = gr.Radio(["Aus", "An"], label="KKG Erweiterungen (RAG)", value = "Aus")
|
267 |
+
model_option = gr.Radio(["OpenAI", "HuggingFace"], label="Modellauswahl", value = "OpenAI")
|
268 |
+
websuche = gr.Radio(["Aus", "An"], label="Web-Suche", value = "Aus")
|
269 |
+
|
270 |
+
|
271 |
+
top_p = gr.Slider(
|
272 |
+
minimum=-0,
|
273 |
+
maximum=1.0,
|
274 |
+
value=0.95,
|
275 |
+
step=0.05,
|
276 |
+
interactive=True,
|
277 |
+
label="Top-p",
|
278 |
+
visible=False,
|
279 |
+
)
|
280 |
+
top_k = gr.Slider(
|
281 |
+
minimum=1,
|
282 |
+
maximum=100,
|
283 |
+
value=35,
|
284 |
+
step=1,
|
285 |
+
interactive=True,
|
286 |
+
label="Top-k",
|
287 |
+
visible=False,
|
288 |
+
)
|
289 |
+
temperature = gr.Slider(
|
290 |
+
minimum=0.1,
|
291 |
+
maximum=2.0,
|
292 |
+
value=0.2,
|
293 |
+
step=0.1,
|
294 |
+
interactive=True,
|
295 |
+
label="Temperature",
|
296 |
+
visible=False
|
297 |
+
)
|
298 |
+
max_length_tokens = gr.Slider(
|
299 |
+
minimum=0,
|
300 |
+
maximum=512,
|
301 |
+
value=512,
|
302 |
+
step=8,
|
303 |
+
interactive=True,
|
304 |
+
label="Max Generation Tokens",
|
305 |
+
visible=False,
|
306 |
+
)
|
307 |
+
max_context_length_tokens = gr.Slider(
|
308 |
+
minimum=0,
|
309 |
+
maximum=4096,
|
310 |
+
value=2048,
|
311 |
+
step=128,
|
312 |
+
interactive=True,
|
313 |
+
label="Max History Tokens",
|
314 |
+
visible=False,
|
315 |
+
)
|
316 |
+
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=False)
|
317 |
+
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=False)
|
318 |
+
openai_key = gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1, visible = False)
|
319 |
+
|
320 |
+
|
321 |
+
gr.Markdown(description)
|
322 |
+
|
323 |
+
######################################
|
324 |
+
# Events und Übergabe Werte an Funktionen
|
325 |
+
#######################################
|
326 |
+
######################################
|
327 |
+
# Für Tab 1: Chatbot
|
328 |
+
#Argumente für generate Funktion als Input
|
329 |
+
predict_args = dict(
|
330 |
+
fn=generate_auswahl,
|
331 |
+
inputs=[
|
332 |
+
user_question,
|
333 |
+
attached_file,
|
334 |
+
attached_file_history,
|
335 |
+
chatbot,
|
336 |
+
history,
|
337 |
+
rag_option,
|
338 |
+
model_option,
|
339 |
+
openai_key,
|
340 |
+
anzahl_docs,
|
341 |
+
top_p,
|
342 |
+
temperature,
|
343 |
+
max_length_tokens,
|
344 |
+
max_context_length_tokens,
|
345 |
+
repetition_penalty,
|
346 |
+
top_k,
|
347 |
+
websuche,
|
348 |
+
validate
|
349 |
+
],
|
350 |
+
outputs=[chatbot, history, attached_file, attached_file_history, status_display],
|
351 |
+
show_progress=True,
|
352 |
+
)
|
353 |
+
|
354 |
+
reset_args = dict(
|
355 |
+
fn=reset_textbox, inputs=[], outputs=[user_input, status_display]
|
356 |
+
)
|
357 |
+
|
358 |
+
# Chatbot
|
359 |
+
transfer_input_args = dict(
|
360 |
+
fn=add_text, inputs=[chatbot, history, user_input, attached_file, attached_file_history], outputs=[chatbot, history, user_question, attached_file, attached_file_history, image_display , user_input], show_progress=True
|
361 |
+
)
|
362 |
+
|
363 |
+
##############################################
|
364 |
+
# Button Events....
|
365 |
+
#Validation Button
|
366 |
+
# Event-Handler für die Validierung
|
367 |
+
validate_btn.click(validate_input, inputs=[user_input_validate, validate], outputs=[status_display, validate, user_input_validate, validate_btn])
|
368 |
+
user_input_validate.submit(validate_input, inputs=[user_input_validate, validate], outputs=[status_display, validate, user_input_validate, validate_btn])
|
369 |
+
|
370 |
+
predict_event1 = user_input.submit(**transfer_input_args, queue=False,).then(**predict_args)
|
371 |
+
predict_event2 = submitBtn.click(**transfer_input_args, queue=False,).then(**predict_args)
|
372 |
+
predict_event3 = upload.upload(file_anzeigen, [upload], [image_display, image_display, attached_file] ) #.then(**predict_args)
|
373 |
+
emptyBtn.click(clear_all, [history, uploaded_file_paths, chats], [attached_file, image_display, uploaded_file_paths, history, file_download, chats])
|
374 |
+
#Bild Anzeige neben dem Button wieder entfernen oder austauschen..
|
375 |
+
image_display.select(file_loeschen, [], [attached_file, image_display])
|
376 |
+
#download_button.click(fn=download_chats, inputs=chat_selector, outputs=[file_download])
|
377 |
+
|
378 |
+
|
379 |
+
#Berechnung oder Ausgabe anhalten (kann danach fortgesetzt werden)
|
380 |
+
cancelBtn.click(cancel_outputing, [], [status_display], cancels=[predict_event1,predict_event2, predict_event3])
|
381 |
+
|