permettre l'envoi de fichiers image

This commit is contained in:
François Pelletier 2023-07-05 01:48:59 -04:00
parent 0be09de410
commit ce43e662de
2 changed files with 44 additions and 6 deletions

44
main.py
View file

@ -32,7 +32,7 @@ st.title("Fabrique à documents")
fabriquedoc_endpoint = os.environ.get("FABRIQUEDOC_ENDPOINT", "http://0.0.0.0:8000")
tab1, tab2, tab3 = st.tabs(["Markdown", "Aperçu", "Paramètres"])
tab1, tab2, tab3, tab4 = st.tabs(["Markdown", "Aperçu", "Paramètres", "Images"])
with tab1:
def button1_callback():
@ -41,7 +41,15 @@ with tab1:
content = st.text_area("Enter Markdown text to send:",
"# Ceci est un titre\n## Ceci est un sous-titre\n\nCeci est un paragraphe\n\n## Ceci est un autre sous-titre\n\n> Ceci est du code\n\nCeci est un emoji :heart_eyes:\n\n::: {.center}\nCeci est centré\n:::",
"""
# Ceci est un titre\n
## Ceci est un sous-titre\n\n
Ceci est un paragraphe\n\n
## Ceci est un autre sous-titre\n\n
> Ceci est du code\n\n
Ceci est un emoji :heart_eyes:\n\n
::: {.center}\nCeci est centré\n:::
""",
height=450,
key='markdown')
st.button("Formater le texte", on_click=button1_callback)
@ -127,7 +135,9 @@ with tab3:
headers = {"Content-Type": "application/json"}
# Send the POST request with the JSON data in the request body
response = requests.get(f"{fabriquedoc_endpoint}/generer/", json=document_specs.dict(), headers=headers)
response = requests.get(f"{fabriquedoc_endpoint}/generer/",
json=document_specs.dict(),
headers=headers)
# Check the response status code
if 200 <= response.status_code <= 299:
# If the request is successful, get the file data from the response
@ -145,3 +155,31 @@ with tab3:
else:
# If the request is unsuccessful, display the error status code and message
st.error(f"Request failed with status code {response.status_code}: {response.text}")
with tab4:
st.header("Images")
st.write("Uploaded image")
# list uploaded files with a request to the GET /images endpoint
response = requests.get(f"{fabriquedoc_endpoint}/images/")
images = response.json()["images"]
selected_image = st.selectbox("Select an image:", images)
image_response = requests.get(f"{fabriquedoc_endpoint}/images/{selected_image}")
image_data = image_response.content
st.image(image_data)
st.write("Upload image")
uploaded_files = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
if uploaded_files is not None:
url = f"{fabriquedoc_endpoint}/images/"
# Create a FormData object
files = {"file": uploaded_files}
# Submit the file to the endpoint
response = requests.post(url, files=files)
# Check the response status
if response.status_code < 300:
st.write("File sent successfully!")
else:
st.write("File upload failed.")

View file

@ -1,4 +1,4 @@
streamlit~=1.16.0
requests~=2.28.1
pydantic~=1.10.2
streamlit~=1.24.0
requests~=2.28.2
pydantic~=2.0.1
mdformat~=0.7.16