Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -718,6 +718,7 @@ content = os.environ.get("content")
|
|
| 718 |
state = os.environ.get("state")
|
| 719 |
system = os.environ.get("system")
|
| 720 |
auth = os.environ.get("auth")
|
|
|
|
| 721 |
auth2 = os.environ.get("auth2")
|
| 722 |
openai.api_key = auth
|
| 723 |
openai.api_base = host
|
|
@@ -746,6 +747,7 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
| 746 |
|
| 747 |
if Textbox3 == code:
|
| 748 |
imageData = None
|
|
|
|
| 749 |
if Images != "None":
|
| 750 |
output = []
|
| 751 |
headers = {
|
|
@@ -755,6 +757,20 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
| 755 |
get_image = requests.get(Images, headers=headers)
|
| 756 |
if get_image.status_code == 200:
|
| 757 |
image_data = get_image.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 758 |
elif platform == "web":
|
| 759 |
# print("WEB")
|
| 760 |
url = requests.get(Images)
|
|
@@ -765,18 +781,14 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
| 765 |
def encode_image(image: Image.Image, format: str = "PNG") -> str:
|
| 766 |
with io.BytesIO() as buffer:
|
| 767 |
image.save(buffer, format=format)
|
| 768 |
-
encoded_image = buffer.getvalue().decode("latin-1")
|
| 769 |
return encoded_image
|
| 770 |
|
| 771 |
def url_to_base64(image_url):
|
| 772 |
try:
|
| 773 |
-
# Download the image from the URL
|
| 774 |
response = requests.get(image_url)
|
| 775 |
response.raise_for_status()
|
| 776 |
-
|
| 777 |
-
# Convert the image content to base64
|
| 778 |
base64_data = base64.b64encode(response.content).decode('utf-8')
|
| 779 |
-
|
| 780 |
return base64_data
|
| 781 |
except Exception as e:
|
| 782 |
print(f"Error: {e}")
|
|
@@ -842,7 +854,7 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
| 842 |
|
| 843 |
{
|
| 844 |
"type": "image_url",
|
| 845 |
-
"image_url":
|
| 846 |
}
|
| 847 |
]
|
| 848 |
}],
|
|
|
|
| 718 |
state = os.environ.get("state")
|
| 719 |
system = os.environ.get("system")
|
| 720 |
auth = os.environ.get("auth")
|
| 721 |
+
g_auth = os.environ.get("g_auth")
|
| 722 |
auth2 = os.environ.get("auth2")
|
| 723 |
openai.api_key = auth
|
| 724 |
openai.api_base = host
|
|
|
|
| 747 |
|
| 748 |
if Textbox3 == code:
|
| 749 |
imageData = None
|
| 750 |
+
global image_url
|
| 751 |
if Images != "None":
|
| 752 |
output = []
|
| 753 |
headers = {
|
|
|
|
| 757 |
get_image = requests.get(Images, headers=headers)
|
| 758 |
if get_image.status_code == 200:
|
| 759 |
image_data = get_image.content
|
| 760 |
+
byt_headers = {
|
| 761 |
+
"Authorization": f"Bearer {g_auth}"
|
| 762 |
+
}
|
| 763 |
+
upload_response = requests.post("https://api.bytescale.com/v2/accounts/12a1yeH/vis_upload/form_data", data=image_data, headers=byt_headers)
|
| 764 |
+
if upload_response.status_code == 200:
|
| 765 |
+
response_json = upload_response.json()
|
| 766 |
+
image_url = response_json.get('url')
|
| 767 |
+
if image_url:
|
| 768 |
+
print(f"Image uploaded successfully. URL: {image_url}")
|
| 769 |
+
else:
|
| 770 |
+
print("Failed to get image URL from Byteslash response.")
|
| 771 |
+
else:
|
| 772 |
+
print(f"Failed to upload image to Byteslash. Status Code: {upload_response.status_code}")
|
| 773 |
+
|
| 774 |
elif platform == "web":
|
| 775 |
# print("WEB")
|
| 776 |
url = requests.get(Images)
|
|
|
|
| 781 |
def encode_image(image: Image.Image, format: str = "PNG") -> str:
|
| 782 |
with io.BytesIO() as buffer:
|
| 783 |
image.save(buffer, format=format)
|
| 784 |
+
encoded_image = buffer.getvalue().decode("latin-1")
|
| 785 |
return encoded_image
|
| 786 |
|
| 787 |
def url_to_base64(image_url):
|
| 788 |
try:
|
|
|
|
| 789 |
response = requests.get(image_url)
|
| 790 |
response.raise_for_status()
|
|
|
|
|
|
|
| 791 |
base64_data = base64.b64encode(response.content).decode('utf-8')
|
|
|
|
| 792 |
return base64_data
|
| 793 |
except Exception as e:
|
| 794 |
print(f"Error: {e}")
|
|
|
|
| 854 |
|
| 855 |
{
|
| 856 |
"type": "image_url",
|
| 857 |
+
"image_url": image_url
|
| 858 |
}
|
| 859 |
]
|
| 860 |
}],
|