Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -513,14 +513,6 @@ with open("labels.txt", "r") as file:
|
|
513 |
|
514 |
messages = [{"role": "system", "content": system}]
|
515 |
|
516 |
-
def save_image_as_base64(image_content, file_extension=".png"):
|
517 |
-
# Encode the image content as base64
|
518 |
-
image_base64 = base64.b64encode(image_content).decode("utf-8")
|
519 |
-
|
520 |
-
# Construct the data URL
|
521 |
-
data_url = f"data:image/{file_extension};base64,{image_base64}"
|
522 |
-
return data_url
|
523 |
-
|
524 |
def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
525 |
if Textbox3 == code:
|
526 |
imageData = None
|
@@ -532,7 +524,10 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
532 |
if platform == "wh":
|
533 |
get_image = requests.get(Images, headers=headers)
|
534 |
if get_image.status_code == 200:
|
535 |
-
#
|
|
|
|
|
|
|
536 |
random_id = random.randint(1000, 9999)
|
537 |
file_extension = ".png"
|
538 |
filename = f"image_{random_id}{file_extension}"
|
@@ -543,9 +538,6 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
543 |
full_path = os.path.join(os.getcwd(), filename)
|
544 |
print(f"Saved image as: {full_path}")
|
545 |
|
546 |
-
# Convert the image content to base64
|
547 |
-
image_data_url = save_image_as_base64(get_image.content, file_extension)
|
548 |
-
print(f"Data URL of the image: {image_data_url}")
|
549 |
elif platform == "web":
|
550 |
print("WEB")
|
551 |
# Handle web case if needed
|
@@ -609,6 +601,7 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
609 |
elif max_rounded_prediction < 0.5:
|
610 |
output.append({"Mode": "Image", "type": "Not predictable", "prediction_value": max_rounded_prediction, "content": "Seems like the prediction rate is too low due to that won't be able to predict the type of material. Try again with a cropped image or different one"})
|
611 |
|
|
|
612 |
return output
|
613 |
else:
|
614 |
output = []
|
@@ -648,7 +641,7 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
648 |
user_inputs = [
|
649 |
gr.Textbox(label="Platform", type="text"),
|
650 |
gr.Textbox(label="User Input", type="text"),
|
651 |
-
gr.Textbox(label="
|
652 |
gr.Textbox(label="Textbox2", type="text"),
|
653 |
gr.Textbox(label="Textbox3", type="password")
|
654 |
]
|
|
|
513 |
|
514 |
messages = [{"role": "system", "content": system}]
|
515 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
516 |
def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
517 |
if Textbox3 == code:
|
518 |
imageData = None
|
|
|
524 |
if platform == "wh":
|
525 |
get_image = requests.get(Images, headers=headers)
|
526 |
if get_image.status_code == 200:
|
527 |
+
# Convert the image content to a base64 data URL
|
528 |
+
image_base64 = base64.b64encode(get_image.content).decode("utf-8")
|
529 |
+
image_data_url = f"data:image/png;base64,{image_base64}"
|
530 |
+
|
531 |
random_id = random.randint(1000, 9999)
|
532 |
file_extension = ".png"
|
533 |
filename = f"image_{random_id}{file_extension}"
|
|
|
538 |
full_path = os.path.join(os.getcwd(), filename)
|
539 |
print(f"Saved image as: {full_path}")
|
540 |
|
|
|
|
|
|
|
541 |
elif platform == "web":
|
542 |
print("WEB")
|
543 |
# Handle web case if needed
|
|
|
601 |
elif max_rounded_prediction < 0.5:
|
602 |
output.append({"Mode": "Image", "type": "Not predictable", "prediction_value": max_rounded_prediction, "content": "Seems like the prediction rate is too low due to that won't be able to predict the type of material. Try again with a cropped image or different one"})
|
603 |
|
604 |
+
output.append({"Mode": "Image", "type": "Data URL", "data_url": image_data_url})
|
605 |
return output
|
606 |
else:
|
607 |
output = []
|
|
|
641 |
user_inputs = [
|
642 |
gr.Textbox(label="Platform", type="text"),
|
643 |
gr.Textbox(label="User Input", type="text"),
|
644 |
+
gr.Textbox(label="Images", type="text"),
|
645 |
gr.Textbox(label="Textbox2", type="text"),
|
646 |
gr.Textbox(label="Textbox3", type="password")
|
647 |
]
|