From ce43e662decdc01a625a64db5ab2effab9a575ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pelletier?= Date: Wed, 5 Jul 2023 01:48:59 -0400 Subject: [PATCH] permettre l'envoi de fichiers image --- main.py | 44 +++++++++++++++++++++++++++++++++++++++++--- requirements.txt | 6 +++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 60eda23..e6dfb36 100644 --- a/main.py +++ b/main.py @@ -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.") diff --git a/requirements.txt b/requirements.txt index c7a7d05..93e9fb5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file