alexkueck commited on
Commit
40991ee
1 Parent(s): 3e9eb10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -100
app.py CHANGED
@@ -10,7 +10,6 @@ from PIL import Image, ImageDraw, ImageOps, ImageFont
10
  import base64
11
  import tempfile
12
 
13
- from tavily import TavilyClient
14
 
15
  from langchain.chains import LLMChain, RetrievalQA
16
  from langchain.chat_models import ChatOpenAI
@@ -69,22 +68,12 @@ file_path_download = ""
69
 
70
  #Plattform Keys aus den Secrets holen zu diesem Space
71
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HF_ACCESS_READ")
72
- OAI_API_KEY=os.getenv("OPENAI_API_KEY")
73
  HEADERS = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
74
- TAVILY_KEY = os.getenv("TAVILY_KEY")
75
- os.environ["TAVILY_API_KEY"] = TAVILY_KEY
76
- ANTI_BOT_PW = os.getenv("CORRECT_VALIDATE")
77
-
78
 
79
  ################################################
80
  #LLM Model mit dem gearbeitet wird
81
- #openai-------------------------------------
82
- #MODEL_NAME = "gpt-3.5-turbo-16k"
83
- #MODEL_NAME = "gpt-3.5-turbo-1106"
84
- MODEL_NAME= "gpt-4-1106-preview"
85
- MODEL_NAME_IMAGE = "gpt-4-vision-preview"
86
-
87
-
88
  #verfügbare Modelle anzeigen lassen
89
  #HuggingFace Reop ID--------------------------------
90
  #repo_id = "meta-llama/Llama-2-13b-chat-hf"
@@ -106,14 +95,6 @@ repo_id = "google/gemma-7b"
106
  #HuggingFace Model name--------------------------------
107
  #MODEL_NAME_HF = "mistralai/Mixtral-8x7B-Instruct-v0.1"
108
  MODEL_NAME_HF = "google/gemma-7b"
109
- MODEL_NAME_OAI_ZEICHNEN = "dall-e-3"
110
- #Alternativ zeichnen: Stabe Diffusion from HF:
111
- API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
112
-
113
- ################################################
114
- #Inference - Endpoint
115
- ################################################
116
- ENDPOINT_URL = "https://ih7lj8onsvp1wbh0.us-east-1.aws.endpoints.huggingface.cloud"
117
 
118
  ################################################
119
  #HF Hub Zugriff ermöglichen
@@ -121,14 +102,6 @@ ENDPOINT_URL = "https://ih7lj8onsvp1wbh0.us-east-1.aws.endpoints.huggingface.clo
121
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN
122
 
123
 
124
- ################################################
125
- #OpenAI Zugang, client und Assistant einmal erzeugen.
126
- ################################################
127
- #zentral einmal erzeugen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
128
- client = OpenAI()
129
- general_assistant_file = client.beta.assistants.create(name="File Analysator",instructions=template, model="gpt-4-1106-preview",)
130
- thread_file = client.beta.threads.create()
131
- general_assistant_suche= openai_assistant_suche(client)
132
 
133
 
134
  #################################################
@@ -232,14 +205,7 @@ def cancel_outputing():
232
  def reset_textbox():
233
  return gr.update(value=""),""
234
 
235
-
236
- ##########################################
237
- #Hilfsfunktion, um ein von Stable Diffusion erzeugtes Bild für die Ausgabe in der History vorzubereiten
238
- def umwandeln_fuer_anzeige(image):
239
- buffer = io.BytesIO()
240
- image.save(buffer, format='PNG')
241
- return buffer.getvalue()
242
-
243
 
244
 
245
  ##################################################
@@ -343,71 +309,9 @@ def generate_auswahl(prompt_in, file, file_history, chatbot, history, rag_option
343
  except:
344
  pass
345
 
346
- ##################################################
347
- #zu einem Text-Prompt ein Bild via Stable Diffusion generieren
348
- def generate_bild(prompt, chatbot, model_option_zeichnen='HuggingFace', temperature=0.5, max_new_tokens=4048,top_p=0.6, repetition_penalty=1.3, validate=False):
349
- global client
350
- if (model_option_zeichnen == "Stable Diffusion"):
351
- print("Bild Erzeugung HF..............................")
352
- #Bild nach Anweisung zeichnen und in History darstellen...
353
- data = {"inputs": prompt}
354
- response = requests.post(API_URL, headers=HEADERS, json=data)
355
- print("fertig Bild")
356
- result = response.content
357
- #Bild ausgeben
358
- image = Image.open(io.BytesIO(result))
359
- image_64 = umwandeln_fuer_anzeige(image)
360
- chatbot[-1][1]= "<img src='data:image/png;base64,{0}'/>".format(base64.b64encode(image_64).decode('utf-8'))
361
- else:
362
- print("Bild Erzeugung DallE..............................")
363
- #als Format ginge auch 'url', n - Anz. der erzeugten Bilder
364
- response = client.images.generate(model="dall-e-3",prompt=prompt,size="1024x1024",quality="standard",n=1, response_format='b64_json')
365
- #chatbot[-1][1]= "<img src='data:image/png;base64,{0}'/>".format(base64.b64encode(image_64).decode('utf-8'))
366
- chatbot[-1][1] = "<img src='data:image/png;base64,{0}'/>".format(response.data[0].b64_json)
367
-
368
- return chatbot, "Antwort KI: Success"
369
 
370
 
371
- ##################################################
372
- #zu einem Bild und Text-Prompt eine Analyse generieren
373
- def generate_text_zu_bild(file, prompt, k, rag_option, chatbot, history, db):
374
- global splittet
375
- print("Text mit Bild ..............................")
376
- prompt_neu = generate_prompt_with_history(prompt, history)
377
- if (rag_option == "An"):
378
- print("Bild mit RAG..............................")
379
- neu_text_mit_chunks = rag_chain2(prompt, db, k)
380
- #für Chat LLM:
381
- #prompt = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
382
- #als reiner prompt:
383
- prompt_neu = generate_prompt_with_history(neu_text_mit_chunks, history)
384
-
385
- headers, payload = process_image(file, prompt_neu, MODEL_NAME_IMAGE, OAI_API_KEY)
386
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
387
- #als json ausgeben
388
- data = response.json()
389
- # Den "content" auswählen, da dort die Antwort der Ki enthalten ist
390
- result = data['choices'][0]['message']['content']
391
- return result
392
-
393
-
394
- ##################################################
395
- #zu einem Bild und Text-Prompt eine Analyse generieren
396
- def generate_text_zu_doc(file, prompt, k, rag_option, chatbot, history, db):
397
- global splittet
398
- print("text mit doc ..............................")
399
-
400
- prompt_neu = generate_prompt_with_history(prompt, history)
401
- if (rag_option == "An"):
402
- print("Doc mit RAG..............................")
403
- neu_text_mit_chunks = rag_chain2(prompt, db, k)
404
- #für Chat LLM:
405
- #prompt_neu = generate_prompt_with_history_openai(neu_text_mit_chunks, history)
406
- #als reiner prompt:
407
- prompt_neu = generate_prompt_with_history(neu_text_mit_chunks, history)
408
-
409
- result = create_assistant_file(prompt_neu, file)
410
- return result
411
 
412
 
413
  ####################################################
 
10
  import base64
11
  import tempfile
12
 
 
13
 
14
  from langchain.chains import LLMChain, RetrievalQA
15
  from langchain.chat_models import ChatOpenAI
 
68
 
69
  #Plattform Keys aus den Secrets holen zu diesem Space
70
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HF_ACCESS_READ")
 
71
  HEADERS = {"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"}
72
+ #CORREKT_VALIDATE
73
+ ANTI_BOT_PW = os.getenv("VALIDATE_PW")
 
 
74
 
75
  ################################################
76
  #LLM Model mit dem gearbeitet wird
 
 
 
 
 
 
 
77
  #verfügbare Modelle anzeigen lassen
78
  #HuggingFace Reop ID--------------------------------
79
  #repo_id = "meta-llama/Llama-2-13b-chat-hf"
 
95
  #HuggingFace Model name--------------------------------
96
  #MODEL_NAME_HF = "mistralai/Mixtral-8x7B-Instruct-v0.1"
97
  MODEL_NAME_HF = "google/gemma-7b"
 
 
 
 
 
 
 
 
98
 
99
  ################################################
100
  #HF Hub Zugriff ermöglichen
 
102
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN
103
 
104
 
 
 
 
 
 
 
 
 
105
 
106
 
107
  #################################################
 
205
  def reset_textbox():
206
  return gr.update(value=""),""
207
 
208
+
 
 
 
 
 
 
 
209
 
210
 
211
  ##################################################
 
309
  except:
310
  pass
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
 
313
 
314
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
 
317
  ####################################################