taskswithcode
commited on
Commit
·
b60cda7
1
Parent(s):
2f0a627
Fixes
Browse files- app.py +31 -17
- curl_usage.txt +5 -0
- python_usage.txt +7 -0
- sod_apis.json +7 -0
app.py
CHANGED
|
@@ -25,7 +25,6 @@ mask_types = {
|
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
-
|
| 29 |
APP_NAME = "hf/salient_object_detection"
|
| 30 |
INFO_URL = "https://www.taskswithcode.com/stats/"
|
| 31 |
TMP_DIR="tmp_dir"
|
|
@@ -46,21 +45,26 @@ def get_views(action):
|
|
| 46 |
res = requests.post(INFO_URL, json = app_info).json()
|
| 47 |
print(res)
|
| 48 |
data = res["count"]
|
| 49 |
-
except:
|
| 50 |
data = 0
|
|
|
|
| 51 |
ret_val = data
|
| 52 |
st.session_state["view_count"] = data
|
| 53 |
else:
|
| 54 |
ret_val = st.session_state["view_count"]
|
| 55 |
if (action != "init"):
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return "{:,}".format(ret_val)
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
-
def construct_model_info_for_display(model_names):
|
| 64 |
options_arr = []
|
| 65 |
#markdown_str = f"<div style=\"font-size:16px; color: #2f2f2f; text-align: left\"><br/><b>Models evaluated ({len(model_names)})</b><br/></div>"
|
| 66 |
markdown_str = f"<div style=\"font-size:16px; color: #2f2f2f; text-align: left\"><br/><b>Model evaluated </b><br/></div>"
|
|
@@ -72,14 +76,23 @@ def construct_model_info_for_display(model_names):
|
|
| 72 |
if ("Note" in node):
|
| 73 |
markdown_str += f"<div style=\"font-size:16px; color: #a91212; text-align: left\"> {node['Note']}<a href=\'{node['alt_url']}\' target='_blank'>link</a></div>"
|
| 74 |
markdown_str += "<div style=\"font-size:16px; color: #5f5f5f; text-align: left\"><br/></div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
markdown_str += "<div style=\"font-size:12px; color: #9f9f9f; text-align: left\"><b
|
| 77 |
markdown_str += "<div style=\"font-size:12px; color: #9f9f9f; text-align: left\"><br/><a href=\'https://github.com/taskswithcode/salient_object_detection_app.git\' target='_blank'>Github code</a> for this app</div>"
|
|
|
|
| 78 |
return options_arr,markdown_str
|
| 79 |
|
| 80 |
|
| 81 |
def init_page():
|
| 82 |
-
st.set_page_config(page_title='TWC -
|
| 83 |
menu_items={
|
| 84 |
'About': 'This app was created by taskswithcode. http://taskswithcode.com'
|
| 85 |
|
|
@@ -114,8 +127,8 @@ def run_test(config,input_file_name,display_area,uploaded_file,mask_type):
|
|
| 114 |
return {"error":f"API request failed {r.status_code}"}
|
| 115 |
except Exception as e:
|
| 116 |
st.error("Some error occurred during prediction" + str(e))
|
| 117 |
-
st.stop()
|
| 118 |
-
return {"error":f"Exception in performing
|
| 119 |
return {}
|
| 120 |
|
| 121 |
|
|
@@ -127,13 +140,12 @@ def display_results(results,response_info,mask):
|
|
| 127 |
download_data = {}
|
| 128 |
main_sent = main_sent + "\n" + '\n'.join(body_sent)
|
| 129 |
st.markdown(main_sent,unsafe_allow_html=True)
|
| 130 |
-
st.image(results["response"], caption=f'Output of
|
| 131 |
st.session_state["download_ready"] = results["response"]
|
| 132 |
get_views("submit")
|
| 133 |
|
| 134 |
|
| 135 |
def init_session():
|
| 136 |
-
print("Init session")
|
| 137 |
init_page()
|
| 138 |
st.session_state["model_name"] = "insprynet"
|
| 139 |
st.session_state["download_ready"] = None
|
|
@@ -141,7 +153,7 @@ def init_session():
|
|
| 141 |
st.session_state["file_name"] = "default"
|
| 142 |
st.session_state["mask_type"] = "rgba"
|
| 143 |
|
| 144 |
-
def app_main(app_mode,example_files,model_name_files,config_file):
|
| 145 |
init_session()
|
| 146 |
with open(example_files) as fp:
|
| 147 |
example_file_names = json.load(fp)
|
|
@@ -149,10 +161,12 @@ def app_main(app_mode,example_files,model_name_files,config_file):
|
|
| 149 |
model_names = json.load(fp)
|
| 150 |
with open(config_file) as fp:
|
| 151 |
config = json.load(fp)
|
|
|
|
|
|
|
| 152 |
curr_use_case = use_case[app_mode].split(".")[0]
|
| 153 |
curr_use_case = use_case[app_mode].split(".")[0]
|
| 154 |
-
st.markdown("<h5 style='text-align: center;'>
|
| 155 |
-
st.markdown(f"<div style='color: #4f4f4f; text-align: left'>
|
| 156 |
st.markdown(f"<div style='color: #9f9f9f; text-align: right'>views: {get_views('init')}</div>", unsafe_allow_html=True)
|
| 157 |
|
| 158 |
|
|
@@ -173,7 +187,7 @@ def app_main(app_mode,example_files,model_name_files,config_file):
|
|
| 173 |
mask_type = mask_types[mask_type]
|
| 174 |
st.write("")
|
| 175 |
submit_button = st.form_submit_button('Run')
|
| 176 |
-
options_arr,markdown_str = construct_model_info_for_display(model_names)
|
| 177 |
|
| 178 |
|
| 179 |
input_status_area = st.empty()
|
|
@@ -208,12 +222,12 @@ def app_main(app_mode,example_files,model_name_files,config_file):
|
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
st.error("Some error occurred during loading" + str(e))
|
| 211 |
-
st.stop()
|
| 212 |
|
| 213 |
st.markdown(markdown_str, unsafe_allow_html=True)
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if __name__ == "__main__":
|
| 218 |
-
app_main("1","sod_app_examples.json","sod_app_models.json","config.json")
|
| 219 |
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
|
|
|
|
| 28 |
APP_NAME = "hf/salient_object_detection"
|
| 29 |
INFO_URL = "https://www.taskswithcode.com/stats/"
|
| 30 |
TMP_DIR="tmp_dir"
|
|
|
|
| 45 |
res = requests.post(INFO_URL, json = app_info).json()
|
| 46 |
print(res)
|
| 47 |
data = res["count"]
|
| 48 |
+
except Exception as e:
|
| 49 |
data = 0
|
| 50 |
+
print(f"Exception in get_views - uncached case. view count not cached: {str(e)}")
|
| 51 |
ret_val = data
|
| 52 |
st.session_state["view_count"] = data
|
| 53 |
else:
|
| 54 |
ret_val = st.session_state["view_count"]
|
| 55 |
if (action != "init"):
|
| 56 |
+
try:
|
| 57 |
+
app_info = {'name': APP_NAME,"action":action,"host":hostname,"ip":ip_address}
|
| 58 |
+
print(app_info)
|
| 59 |
+
res = requests.post(INFO_URL, json = app_info).json()
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Exception in get_views - Non init case. view count not cached: {str(e)}")
|
| 62 |
return "{:,}".format(ret_val)
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
+
def construct_model_info_for_display(model_names,api_info):
|
| 68 |
options_arr = []
|
| 69 |
#markdown_str = f"<div style=\"font-size:16px; color: #2f2f2f; text-align: left\"><br/><b>Models evaluated ({len(model_names)})</b><br/></div>"
|
| 70 |
markdown_str = f"<div style=\"font-size:16px; color: #2f2f2f; text-align: left\"><br/><b>Model evaluated </b><br/></div>"
|
|
|
|
| 76 |
if ("Note" in node):
|
| 77 |
markdown_str += f"<div style=\"font-size:16px; color: #a91212; text-align: left\"> {node['Note']}<a href=\'{node['alt_url']}\' target='_blank'>link</a></div>"
|
| 78 |
markdown_str += "<div style=\"font-size:16px; color: #5f5f5f; text-align: left\"><br/></div>"
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
markdown_str += f"<div style=\"font-size:16px; color: #2f2f2f; text-align: left\"><b>{api_info['desc']}</b><br/></div>"
|
| 82 |
+
for method in api_info["methods"]:
|
| 83 |
+
lang = method["lang"]
|
| 84 |
+
example = open(method["usage"]).read()
|
| 85 |
+
markdown_str += f"<div style=\"font-size:16px; color: #5f5f5f; text-align: center\"><b>{lang} usage</b></div>"
|
| 86 |
+
markdown_str += f"<div style=\"font-size:14px; color: #bfbfbf; text-align: left\">{example}<br/></div>"
|
| 87 |
|
| 88 |
+
markdown_str += "<div style=\"font-size:12px; color: #9f9f9f; text-align: left\"><b><br/>Note:</b><br/>• Uploaded files are loaded into non-persistent memory for the duration of the computation. They are not cached</div>"
|
| 89 |
markdown_str += "<div style=\"font-size:12px; color: #9f9f9f; text-align: left\"><br/><a href=\'https://github.com/taskswithcode/salient_object_detection_app.git\' target='_blank'>Github code</a> for this app</div>"
|
| 90 |
+
|
| 91 |
return options_arr,markdown_str
|
| 92 |
|
| 93 |
|
| 94 |
def init_page():
|
| 95 |
+
st.set_page_config(page_title='TWC - Image foreground masking or background removal with state-of-the-art models', page_icon="logo.jpg", layout='centered', initial_sidebar_state='auto',
|
| 96 |
menu_items={
|
| 97 |
'About': 'This app was created by taskswithcode. http://taskswithcode.com'
|
| 98 |
|
|
|
|
| 127 |
return {"error":f"API request failed {r.status_code}"}
|
| 128 |
except Exception as e:
|
| 129 |
st.error("Some error occurred during prediction" + str(e))
|
| 130 |
+
#st.stop()
|
| 131 |
+
return {"error":f"Exception in performing image masking: {str(e)}"}
|
| 132 |
return {}
|
| 133 |
|
| 134 |
|
|
|
|
| 140 |
download_data = {}
|
| 141 |
main_sent = main_sent + "\n" + '\n'.join(body_sent)
|
| 142 |
st.markdown(main_sent,unsafe_allow_html=True)
|
| 143 |
+
st.image(results["response"], caption=f'Output of Image background removal with mask: {mask}')
|
| 144 |
st.session_state["download_ready"] = results["response"]
|
| 145 |
get_views("submit")
|
| 146 |
|
| 147 |
|
| 148 |
def init_session():
|
|
|
|
| 149 |
init_page()
|
| 150 |
st.session_state["model_name"] = "insprynet"
|
| 151 |
st.session_state["download_ready"] = None
|
|
|
|
| 153 |
st.session_state["file_name"] = "default"
|
| 154 |
st.session_state["mask_type"] = "rgba"
|
| 155 |
|
| 156 |
+
def app_main(app_mode,example_files,model_name_files,api_info_files,config_file):
|
| 157 |
init_session()
|
| 158 |
with open(example_files) as fp:
|
| 159 |
example_file_names = json.load(fp)
|
|
|
|
| 161 |
model_names = json.load(fp)
|
| 162 |
with open(config_file) as fp:
|
| 163 |
config = json.load(fp)
|
| 164 |
+
with open(api_info_files) as fp:
|
| 165 |
+
api_info = json.load(fp)
|
| 166 |
curr_use_case = use_case[app_mode].split(".")[0]
|
| 167 |
curr_use_case = use_case[app_mode].split(".")[0]
|
| 168 |
+
st.markdown("<h5 style='text-align: center;'>Image foreground masking or background removal</h5>", unsafe_allow_html=True)
|
| 169 |
+
st.markdown(f"<div style='color: #4f4f4f; text-align: left'>Image masking using state-of-the-art models for salient object detection(SOD). SOD use cases are<br/> • {use_case['1']}<br/> • {use_case['2']}</div>", unsafe_allow_html=True)
|
| 170 |
st.markdown(f"<div style='color: #9f9f9f; text-align: right'>views: {get_views('init')}</div>", unsafe_allow_html=True)
|
| 171 |
|
| 172 |
|
|
|
|
| 187 |
mask_type = mask_types[mask_type]
|
| 188 |
st.write("")
|
| 189 |
submit_button = st.form_submit_button('Run')
|
| 190 |
+
options_arr,markdown_str = construct_model_info_for_display(model_names,api_info)
|
| 191 |
|
| 192 |
|
| 193 |
input_status_area = st.empty()
|
|
|
|
| 222 |
|
| 223 |
except Exception as e:
|
| 224 |
st.error("Some error occurred during loading" + str(e))
|
| 225 |
+
#st.stop()
|
| 226 |
|
| 227 |
st.markdown(markdown_str, unsafe_allow_html=True)
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|
| 232 |
+
app_main("1","sod_app_examples.json","sod_app_models.json","sod_apis.json","config.json")
|
| 233 |
|
curl_usage.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
curl <br/>
|
| 2 |
+
-o [output file] <br/>
|
| 3 |
+
-F "mask=[mask_type]" <br/>
|
| 4 |
+
-F "test=@./[input_file]" <br/>
|
| 5 |
+
https://www.taskswithcode.com/salient_object_detection_api/
|
python_usage.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
<br/>
|
| 3 |
+
file_data = open("sample.jpg", "rb") <br/>
|
| 4 |
+
mask_type="blur" #rgba,green,blur,map <br/>
|
| 5 |
+
r = requests.post("https://www.taskswithcode.com/salient_object_detection_api/", <br/>
|
| 6 |
+
data={"mask":mask_type}, files={"test":file_data}) <br/>
|
| 7 |
+
results = {"response":r.content,"size":len(r.content)} <br/>
|
sod_apis.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"desc":"🔥 Free evaluation API 📡 available for this task now",
|
| 3 |
+
"methods":[
|
| 4 |
+
{"lang": "Python", "usage":"python_usage.txt"},
|
| 5 |
+
{"lang":"Curl", "usage":"curl_usage.txt"}
|
| 6 |
+
]
|
| 7 |
+
}
|