Support URL for image in the app
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
import PIL
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
import google.generativeai as genai
|
| 6 |
|
|
@@ -65,14 +66,6 @@ def get_gemini_model():
|
|
| 65 |
)
|
| 66 |
|
| 67 |
|
| 68 |
-
def load_image(image_file: st.runtime.uploaded_file_manager.UploadedFile):
|
| 69 |
-
img = PIL.Image.open(image_file)
|
| 70 |
-
if img.mode in ("RGBA", "P"):
|
| 71 |
-
img = img.convert("RGB")
|
| 72 |
-
|
| 73 |
-
return img
|
| 74 |
-
|
| 75 |
-
|
| 76 |
def get_image_description(image: PIL.Image) -> str:
|
| 77 |
"""
|
| 78 |
Use Gemini Pro Vision LMM to generate a response.
|
|
@@ -100,18 +93,35 @@ uploaded_file = st.file_uploader(
|
|
| 100 |
type=SUPPORTED_FILE_EXTENSIONS
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
'file_name': uploaded_file.name,
|
| 107 |
-
'file_type': uploaded_file.type,
|
| 108 |
-
'file_size': uploaded_file.size
|
| 109 |
-
}
|
| 110 |
-
st.header('Image')
|
| 111 |
-
st.write(file_details)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
try:
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
st.image(the_img, width=250)
|
| 116 |
description = get_image_description(the_img)
|
| 117 |
st.header('Description')
|
|
@@ -122,6 +132,8 @@ if uploaded_file is not None:
|
|
| 122 |
st.error(f'An error occurred while loading the image: {uie}')
|
| 123 |
logging.debug(f'An error occurred while loading the image: {uie}\n'
|
| 124 |
f'File details: {file_details}')
|
|
|
|
|
|
|
| 125 |
finally:
|
| 126 |
st.divider()
|
| 127 |
st.write('Sys2Doc is an experimental prototype, with no guarantee provided whatsoever.'
|
|
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
import PIL
|
| 4 |
+
import requests
|
| 5 |
import streamlit as st
|
| 6 |
import google.generativeai as genai
|
| 7 |
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def get_image_description(image: PIL.Image) -> str:
|
| 70 |
"""
|
| 71 |
Use Gemini Pro Vision LMM to generate a response.
|
|
|
|
| 93 |
type=SUPPORTED_FILE_EXTENSIONS
|
| 94 |
)
|
| 95 |
|
| 96 |
+
st.write('OR provide the URL of the image:')
|
| 97 |
+
img_url = st.text_input('URL of the image')
|
| 98 |
+
st.markdown('(*If an image is uploaded and a URL is also provided, Sys2Doc will consider the uploaded image*)')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
if uploaded_file is not None or (img_url is not None and len(img_url) > 0):
|
| 101 |
+
# Show the uploaded image & related info
|
| 102 |
+
print(f'{img_url=}')
|
| 103 |
try:
|
| 104 |
+
if uploaded_file:
|
| 105 |
+
the_img = PIL.Image.open(uploaded_file)
|
| 106 |
+
file_details = {
|
| 107 |
+
'file_name': uploaded_file.name,
|
| 108 |
+
'file_type': uploaded_file.type,
|
| 109 |
+
'file_size': uploaded_file.size
|
| 110 |
+
}
|
| 111 |
+
elif img_url:
|
| 112 |
+
the_img = PIL.Image.open(requests.get(img_url, stream=True).raw)
|
| 113 |
+
file_details = {
|
| 114 |
+
'file_name': os.path.basename(img_url),
|
| 115 |
+
'file_type': the_img.format,
|
| 116 |
+
'file_info': the_img.info
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
if the_img.mode in ('RGBA', 'P'):
|
| 120 |
+
the_img = the_img.convert('RGB')
|
| 121 |
+
|
| 122 |
+
st.header('Image')
|
| 123 |
+
st.write(file_details)
|
| 124 |
+
|
| 125 |
st.image(the_img, width=250)
|
| 126 |
description = get_image_description(the_img)
|
| 127 |
st.header('Description')
|
|
|
|
| 132 |
st.error(f'An error occurred while loading the image: {uie}')
|
| 133 |
logging.debug(f'An error occurred while loading the image: {uie}\n'
|
| 134 |
f'File details: {file_details}')
|
| 135 |
+
except requests.exceptions.MissingSchema as ms:
|
| 136 |
+
st.error(f'Please specify a proper URL for the image.')
|
| 137 |
finally:
|
| 138 |
st.divider()
|
| 139 |
st.write('Sys2Doc is an experimental prototype, with no guarantee provided whatsoever.'
|