alexkueck commited on
Commit
66a55fe
1 Parent(s): 2b258fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -22
app.py CHANGED
@@ -29,8 +29,10 @@ from beschreibungen import *
29
 
30
 
31
  #Konstanten
32
- ANTI_BOT_PW = os.getenv("VALIDTAE_PW")
33
- print("Anti......................"+str(ANTI_BOT_PW))
 
 
34
  PATH_WORK = "."
35
  CHROMA_DIR = "/chroma/kkg"
36
  CHROMA_PDF = './chroma/kkg/pdf'
@@ -125,13 +127,9 @@ def add_text(chatbot, history, prompt, file, file_history):
125
  if (prompt == ""):
126
  chatbot=chatbot + [((file.name,), "Prompt fehlt!")]
127
  else:
128
- ext = analyze_file(file)
129
- if (ext == "png" or ext == "PNG" or ext == "jpg" or ext == "jpeg" or ext == "JPG" or ext == "JPEG"):
130
- chatbot = chatbot +[((file.name,), None), (prompt, None)]
131
- else:
132
- chatbot = chatbot +[("Hochgeladenes Dokument: "+ get_filename(file) +"\n" + prompt, None)]
133
 
134
- 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)
135
 
136
  def add_text2(chatbot, prompt):
137
  if (prompt == ""):
@@ -187,7 +185,7 @@ def generate_text (prompt, chatbot, history, vektordatenbank, top_p=0.6, temper
187
  #oder an Hugging Face --------------------------
188
  print("HF Anfrage.......................")
189
  model_kwargs={"temperature": 0.5, "max_length": 512, "num_return_sequences": 1, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty}
190
- llm = HuggingFaceHub(repo_id=repo_id, model_kwargs=model_kwargs)
191
  llm = HuggingFaceChain(model=MODEL_NAME_HF, model_kwargs={"temperature": 0.5, "max_length": 128})
192
 
193
  #Prompt an history anhängen und einen Text daraus machen
@@ -195,14 +193,14 @@ def generate_text (prompt, chatbot, history, vektordatenbank, top_p=0.6, temper
195
 
196
  #zusätzliche Dokumenten Splits aus DB zum Prompt hinzufügen (aus VektorDB - Chroma oder Mongo DB)
197
  print("LLM aufrufen mit RAG: ...........")
198
- result = rag_chain(history_text_und_prompt, db, 5)
199
  print("result regchain.....................")
200
  print(result)
201
 
202
  except Exception as e:
203
  raise gr.Error(e)
204
 
205
- return result, suche_im_Netz
206
 
207
 
208
  ########################################
@@ -274,6 +272,7 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, anzahl_doc
274
  prompt = normalise_prompt(prompt_in)
275
 
276
  #muss nur einmal ausgeführt werden...
 
277
  if vektordatenbank == None:
278
  print("db neu aufbauen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1")
279
  splits = document_loading_splitting()
@@ -283,7 +282,6 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, anzahl_doc
283
  status = "Antwort der KI ..."
284
  if (file == None and file_history == None):
285
  result, status = generate_text(prompt, chatbot, history,vektordatenbank, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3, top_k=3)
286
- history = history + [[prompt, result]]
287
  else:
288
  #Es wurde ein File neu angehängt -> das hochladen und dann Prompt bearbeiten
289
  #das history_fiel muss neu gesetzt werden
@@ -293,15 +291,30 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, anzahl_doc
293
 
294
  #File hochladen in Chroma und dann Antwort generieren
295
  result = generate_text_zu_doc(neu_file, prompt, k, rag_option, chatbot, history, vektordatenbank)
296
-
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  #die history erweitern - abhängig davon, ob gerade ein file hochgeladen wurde oder nicht
298
  if (file != None):
299
- history = history + [[(file,), None],[prompt, result]]
300
  else:
301
- history = history + [[prompt, result]]
302
-
 
 
303
  chatbot[-1][1] = ""
304
- for character in result:
305
  chatbot[-1][1] += character
306
  time.sleep(0.03)
307
  yield chatbot, history, None, neu_file, status
@@ -391,17 +404,17 @@ with gr.Blocks(css=customCSS, theme=themeAlex) as demo:
391
 
392
  with gr.Column():
393
  with gr.Column(min_width=50, scale=1):
394
- with gr.Tab(label="Chats ..."):
395
  #Geht nicht, da für alle gleichzeitig sichtbar
396
  #chat_selector = gr.CheckboxGroup(label="", choices=update_chat_options())
397
  #download_button = gr.Button("Download ausgewählte Chats")
398
- file_download = gr.File(label="Noch keine Chatsverläufe", visible=True, interactive = False, file_count="multiple",)
399
 
400
  with gr.Tab(label="Parameter"):
401
  #gr.Markdown("# Parameters")
402
- rag_option = gr.Radio(["Aus", "An"], label="KKG Erweiterungen (RAG)", value = "Aus")
403
- model_option = gr.Radio(["OpenAI", "HuggingFace"], label="Modellauswahl", value = "OpenAI")
404
- websuche = gr.Radio(["Aus", "An"], label="Web-Suche", value = "Aus")
405
 
406
 
407
  top_p = gr.Slider(
 
29
 
30
 
31
  #Konstanten
32
+ #Validieren des PW
33
+ ANTI_BOT_PW = os.getenv("VALIDATE_PW")
34
+ #max Anzahl der zurückgelieferten Dokumente
35
+ ANZAHL_DOCS = 5
36
  PATH_WORK = "."
37
  CHROMA_DIR = "/chroma/kkg"
38
  CHROMA_PDF = './chroma/kkg/pdf'
 
127
  if (prompt == ""):
128
  chatbot=chatbot + [((file.name,), "Prompt fehlt!")]
129
  else:
130
+ chatbot = chatbot +[("Hochgeladenes Dokument: "+ get_filename(file) +"\n" + prompt, None)]
 
 
 
 
131
 
132
+ return chatbot, history, prompt, file, file_history, gr.Image(visible = False), ""
133
 
134
  def add_text2(chatbot, prompt):
135
  if (prompt == ""):
 
185
  #oder an Hugging Face --------------------------
186
  print("HF Anfrage.......................")
187
  model_kwargs={"temperature": 0.5, "max_length": 512, "num_return_sequences": 1, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty}
188
+ #llm = HuggingFaceHub(repo_id=repo_id, model_kwargs=model_kwargs)
189
  llm = HuggingFaceChain(model=MODEL_NAME_HF, model_kwargs={"temperature": 0.5, "max_length": 128})
190
 
191
  #Prompt an history anhängen und einen Text daraus machen
 
193
 
194
  #zusätzliche Dokumenten Splits aus DB zum Prompt hinzufügen (aus VektorDB - Chroma oder Mongo DB)
195
  print("LLM aufrufen mit RAG: ...........")
196
+ result = rag_chain(history_text_und_prompt, db, ANZAHL_DOCS)
197
  print("result regchain.....................")
198
  print(result)
199
 
200
  except Exception as e:
201
  raise gr.Error(e)
202
 
203
+ return result, False
204
 
205
 
206
  ########################################
 
272
  prompt = normalise_prompt(prompt_in)
273
 
274
  #muss nur einmal ausgeführt werden...
275
+ #?????????????????????????????????????????????? Nicht passend zum Promt???????????????????????????
276
  if vektordatenbank == None:
277
  print("db neu aufbauen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1")
278
  splits = document_loading_splitting()
 
282
  status = "Antwort der KI ..."
283
  if (file == None and file_history == None):
284
  result, status = generate_text(prompt, chatbot, history,vektordatenbank, top_p=0.6, temperature=0.5, max_new_tokens=4048, max_context_length_tokens=2048, repetition_penalty=1.3, top_k=3)
 
285
  else:
286
  #Es wurde ein File neu angehängt -> das hochladen und dann Prompt bearbeiten
287
  #das history_fiel muss neu gesetzt werden
 
291
 
292
  #File hochladen in Chroma und dann Antwort generieren
293
  result = generate_text_zu_doc(neu_file, prompt, k, rag_option, chatbot, history, vektordatenbank)
294
+ #########################################
295
+ #result verarbeiten
296
+ #aus result die Ergebnisse aufteilen und der History anhängen
297
+ #passages = [doc['text'] for doc in docs]
298
+ #links = [doc.get('url', 'No URL available') for doc in docs]
299
+
300
+
301
+ # Zusammenstellen der Ausgabe
302
+ response = {
303
+ "answer": result,
304
+ #"documents": [{"link": link, "passage": passage} for link, passage in zip(links, passages)]
305
+ }
306
+
307
+
308
  #die history erweitern - abhängig davon, ob gerade ein file hochgeladen wurde oder nicht
309
  if (file != None):
310
+ history = history + [[(file,), None],[prompt, response]]
311
  else:
312
+ history = history + [[prompt, response]]
313
+
314
+
315
+
316
  chatbot[-1][1] = ""
317
+ for character in response:
318
  chatbot[-1][1] += character
319
  time.sleep(0.03)
320
  yield chatbot, history, None, neu_file, status
 
404
 
405
  with gr.Column():
406
  with gr.Column(min_width=50, scale=1):
407
+ #with gr.Tab(label="Chats ..."):
408
  #Geht nicht, da für alle gleichzeitig sichtbar
409
  #chat_selector = gr.CheckboxGroup(label="", choices=update_chat_options())
410
  #download_button = gr.Button("Download ausgewählte Chats")
411
+ #file_download = gr.File(label="Noch keine Chatsverläufe", visible=True, interactive = False, file_count="multiple",)
412
 
413
  with gr.Tab(label="Parameter"):
414
  #gr.Markdown("# Parameters")
415
+ #rag_option = gr.Radio(["Aus", "An"], label="KKG Erweiterungen (RAG)", value = "Aus")
416
+ model_option = gr.Radio(["HuggingFace"], label="Modellauswahl", value = "HuggingFace")
417
+ #websuche = gr.Radio(["Aus", "An"], label="Web-Suche", value = "Aus")
418
 
419
 
420
  top_p = gr.Slider(