Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
@@ -417,6 +417,73 @@ def cancel_outputing():
|
|
417 |
return "Stop Done"
|
418 |
|
419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
class State:
|
421 |
interrupted = False
|
422 |
|
|
|
417 |
return "Stop Done"
|
418 |
|
419 |
|
420 |
+
#########################################
|
421 |
+
#Bildbearbeitung
|
422 |
+
#########################################
|
423 |
+
#########################################
|
424 |
+
#nicht im Einsatz, da Stable Diffusion die Bilder erzeugt
|
425 |
+
def create_picture(history, prompt):
|
426 |
+
client = OpenAI()
|
427 |
+
response = client.images.generate(model="dall-e-3", prompt=prompt,size="1024x1024",quality="standard",n=1,)
|
428 |
+
image_url = response.data[0].url
|
429 |
+
# using requests library to get the image in bytes
|
430 |
+
response2 = requests.get(image_url)
|
431 |
+
# using the Image module from PIL library to view the image
|
432 |
+
image = Image.open(response2.raw)
|
433 |
+
return image
|
434 |
+
|
435 |
+
##########################################
|
436 |
+
#ein hochgeladenes Bild so vorbereiten, dass OpenAI API es annehmen kann und bearbeiten
|
437 |
+
#muss ein base64 Bils sein und header und payload entsprechend konfigurieren
|
438 |
+
def process_image(image_path, prompt):
|
439 |
+
# Convert image to base64
|
440 |
+
with open(image_path, "rb") as image_file:
|
441 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
442 |
+
|
443 |
+
|
444 |
+
# Prepare the data for the API request (specific to the API you're using)
|
445 |
+
headers = {
|
446 |
+
"Content-Type": "application/json",
|
447 |
+
"Authorization": f"Bearer {OAI_API_KEY}"
|
448 |
+
}
|
449 |
+
|
450 |
+
payload = {
|
451 |
+
"model": MODEL_NAME_IMAGE,
|
452 |
+
"messages": [
|
453 |
+
{
|
454 |
+
"role": "user",
|
455 |
+
"content": [
|
456 |
+
{
|
457 |
+
"type": "text",
|
458 |
+
"text": prompt
|
459 |
+
},
|
460 |
+
{
|
461 |
+
"type": "image_url",
|
462 |
+
"image_url": {
|
463 |
+
"url": f"data:image/jpeg;base64,{encoded_string}"
|
464 |
+
}
|
465 |
+
}
|
466 |
+
]
|
467 |
+
}
|
468 |
+
],
|
469 |
+
"max_tokens": 300
|
470 |
+
}
|
471 |
+
return headers, payload
|
472 |
+
|
473 |
+
###################################################
|
474 |
+
#zur Zeit nicht im Gebrauch
|
475 |
+
def transfer_input(inputs):
|
476 |
+
textbox = reset_textbox()
|
477 |
+
return (
|
478 |
+
inputs,
|
479 |
+
gr.update(value=""),
|
480 |
+
gr.Button.update(visible=True),
|
481 |
+
)
|
482 |
+
|
483 |
+
|
484 |
+
#################################################
|
485 |
+
#Klasse mit zuständen - z.B. für interrupt wenn Stop gedrückt...
|
486 |
+
#################################################
|
487 |
class State:
|
488 |
interrupted = False
|
489 |
|