Hey! I recently tried using an API inside a streamlit app.
My only problem is that if I execute this API - as a standalone script (for example, from a jupyter notebook, or even a single python script.) then it works perfectly fine, without any issues, however if I call this api from a streamlit app - it returns an error.
The error is the following (returned by the api) :
â Some internal processing error occurred. Please give us some time, we are looking into â
I know it says it is an internal server error, but trust me, every single time i run it as a standalone script, it works fine. Except when i run it from a streamlit web app. Why does this happen ? How can I possibly fix it ?
hereâs the code that calls the api:
def call_api():
import requests
import json
import streamlit as st
payload = { some parameters that arent relevant in any way}
print("executing....")
with open(filename, 'rb') as f:
r = requests.post('https://'api.endpoint...,
files={filename: f},
data=payload)
st.markdown("Executed!")
x = r.content.decode()
Does streamlit make some sort of a private environment that blocks communication from outside or what ? Its pretty annoying. This only happens if Iâm using streamlit. Both python & streamlit are in the newest version
Hi, it does not matter what kind of api I use. So far anything i tried to run - which communicates with an outside endpoint - doesnt work.
However if this might help you then its just a OCR recognition api called SpaceOCR
I tried both and it doesnât work in any of the cases. Also tried to run the streamlit code a different site hosting platform. It doesnât work there either.
What happens if you remove the Streamlit pieces and just run the Python file?
def call_api():
import requests
import json
payload = { some parameters that arent relevant in any way}
print("executing....")
with open(filename, 'rb') as f:
r = requests.post('https://'api.endpoint...,
files={filename: f},
data=payload)
x = r.content.decode()
What type of error are you getting? When you change the API, what error do you get? I know you mentioned âSome internal processing error occurred. Please give us some time, we are looking intoââ, but there should be an error code in addition to that, e.g. 400, 500, etc.
uh this is the output im getting (not sure which one is the error code):
{âOCRExitCodeâ:99,âIsErroredOnProcessingâ
ErrorMessage":["E103: Some internal processing error occurredâŚ
If you want I could give you the entire code (its barely 50 lines), but only in private - i really dont want to share it here
well yes - to explain it simply : i get a 200 response back all the time when there is no streamlit involved.
if there is a streamlit app running when i send the request i always get back this error.
I wonder if you tried passing an explicit User-Agent within the headers of your request? This is how youâre identified when throwing HTTP requests. I canât see why the request within a Streamlit app wouldnât work (as @Caroline mentions this is something a lot of users leverage) but you may try that:
Copy your user agent, e.g. visit this website. It should look something like this:
Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
Pass it to your requests.post() call by doing:
r = requests.post("htps://...", ..., headers={"User-Agent": "Mozilla/5.0..."})