Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -767,45 +767,69 @@ def classify(platform, UserInput, Images, Textbox2, Textbox3):
|
|
| 767 |
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 768 |
return encoded_image
|
| 769 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
def vision():
|
| 771 |
-
with open("image.png", "wb") as file1_write:
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
|
|
|
| 776 |
|
| 777 |
-
image = Image.open(io.BytesIO(file_content))
|
| 778 |
|
| 779 |
-
base64_image_str = encode_image(image)
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 786 |
}
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
return result_text
|
| 805 |
-
except:
|
| 806 |
-
pass
|
| 807 |
else:
|
| 808 |
return "Answer: not found in the string."
|
|
|
|
|
|
|
|
|
|
| 809 |
|
| 810 |
if "vision" in UserInput.lower():
|
| 811 |
vision()
|
|
|
|
| 767 |
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 768 |
return encoded_image
|
| 769 |
|
| 770 |
+
def url_to_base64(image_url):
|
| 771 |
+
try:
|
| 772 |
+
# Download the image from the URL
|
| 773 |
+
response = requests.get(image_url)
|
| 774 |
+
response.raise_for_status()
|
| 775 |
+
|
| 776 |
+
# Convert the image content to base64
|
| 777 |
+
base64_data = base64.b64encode(response.content).decode('utf-8')
|
| 778 |
+
|
| 779 |
+
return base64_data
|
| 780 |
+
except Exception as e:
|
| 781 |
+
print(f"Error: {e}")
|
| 782 |
+
return None
|
| 783 |
+
|
| 784 |
+
|
| 785 |
def vision():
|
| 786 |
+
# with open("image.png", "wb") as file1_write:
|
| 787 |
+
# file1_write.write(image_data)
|
| 788 |
+
|
| 789 |
+
# Example usage
|
| 790 |
+
# image_url = 'https://example.com/path/to/image.jpg'
|
| 791 |
+
# base64_data = url_to_base64(image_url)
|
| 792 |
|
| 793 |
+
# image = Image.open(io.BytesIO(file_content))
|
| 794 |
|
| 795 |
+
# base64_image_str = encode_image(image)
|
| 796 |
+
|
| 797 |
+
if image_data:
|
| 798 |
+
with io.BytesIO(base64.b64decode(image_data)) as buffer:
|
| 799 |
+
image = Image.open(buffer)
|
| 800 |
+
base64_image_str = encode_image(image)
|
| 801 |
+
|
| 802 |
+
payload = {
|
| 803 |
+
"content": [
|
| 804 |
+
{
|
| 805 |
+
"prompt": "What's this image about? or What does this image contains?",
|
| 806 |
+
"image": base64_image_str,
|
| 807 |
+
}
|
| 808 |
+
],
|
| 809 |
+
"token": vis_auth,
|
| 810 |
}
|
| 811 |
+
|
| 812 |
+
url = vis_url
|
| 813 |
+
headers = {"Content-Type": "application/json"}
|
| 814 |
+
|
| 815 |
+
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
| 816 |
+
results = response.json()
|
| 817 |
+
results = results["result"]
|
| 818 |
+
|
| 819 |
+
answer_index = results.find("Answer:")
|
| 820 |
+
|
| 821 |
+
if answer_index != -1:
|
| 822 |
+
try:
|
| 823 |
+
result_text = results[answer_index + len("Answer:"):].strip()
|
| 824 |
+
print(result_text)
|
| 825 |
+
return result_text
|
| 826 |
+
except:
|
| 827 |
+
pass
|
|
|
|
|
|
|
|
|
|
| 828 |
else:
|
| 829 |
return "Answer: not found in the string."
|
| 830 |
+
else:
|
| 831 |
+
print("Error: Image data is not available.")
|
| 832 |
+
return None
|
| 833 |
|
| 834 |
if "vision" in UserInput.lower():
|
| 835 |
vision()
|