|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +from os import getenv |
| 4 | + |
| 5 | +import streamlit as st |
| 6 | +from azure.ai.documentintelligence import DocumentIntelligenceClient |
| 7 | +from azure.ai.documentintelligence.models import AnalyzeResult, ContentFormat |
| 8 | +from azure.core.credentials import AzureKeyCredential |
| 9 | +from dotenv import load_dotenv |
| 10 | + |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | +with st.sidebar: |
| 14 | + azure_document_intelligence_endpoint = st.text_input( |
| 15 | + label="AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT", |
| 16 | + value=getenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT"), |
| 17 | + key="AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT", |
| 18 | + type="default", |
| 19 | + ) |
| 20 | + azure_document_intelligence_api_version = st.text_input( |
| 21 | + label="AZURE_DOCUMENT_INTELLIGENCE_API_VERSION", |
| 22 | + value=getenv("AZURE_DOCUMENT_INTELLIGENCE_API_VERSION"), |
| 23 | + key="AZURE_DOCUMENT_INTELLIGENCE_API_VERSION", |
| 24 | + type="default", |
| 25 | + ) |
| 26 | + azure_document_intelligence_api_key = st.text_input( |
| 27 | + label="AZURE_DOCUMENT_INTELLIGENCE_API_KEY", |
| 28 | + key="AZURE_DOCUMENT_INTELLIGENCE_API_KEY", |
| 29 | + type="password", |
| 30 | + ) |
| 31 | + "[Azure Portal](https://portal.azure.com/)" |
| 32 | + "[Azure OpenAI Studio](https://oai.azure.com/resource/overview)" |
| 33 | + "[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/9_streamlit_azure_document_intelligence/main.py)" |
| 34 | + |
| 35 | + |
| 36 | +def is_configured(): |
| 37 | + return ( |
| 38 | + azure_document_intelligence_endpoint |
| 39 | + and azure_document_intelligence_api_version |
| 40 | + and azure_document_intelligence_api_key |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def get_client(): |
| 45 | + return DocumentIntelligenceClient( |
| 46 | + credential=AzureKeyCredential(azure_document_intelligence_api_key), |
| 47 | + endpoint=azure_document_intelligence_endpoint, |
| 48 | + api_version=azure_document_intelligence_api_version, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +st.title("9_streamlit_azure_document_intelligence") |
| 53 | + |
| 54 | +if not is_configured(): |
| 55 | + st.warning("Please fill in the required fields at the sidebar.") |
| 56 | + |
| 57 | +# --------------- |
| 58 | +# Upload file |
| 59 | +# --------------- |
| 60 | +st.header("Upload file") |
| 61 | +st.info("Upload a file") |
| 62 | +uploaded_file = st.file_uploader( |
| 63 | + "Upload an input file", |
| 64 | + type=( |
| 65 | + "txt", |
| 66 | + "text", |
| 67 | + "pdf", |
| 68 | + "pptx", |
| 69 | + "jpg", |
| 70 | + "jpeg", |
| 71 | + "png", |
| 72 | + ), |
| 73 | +) |
| 74 | +if uploaded_file: |
| 75 | + bytes_data = uploaded_file.read() |
| 76 | + st.write(f"File name: {uploaded_file.name}") |
| 77 | + st.write(f"File type: {uploaded_file.type}") |
| 78 | + st.write(f"File size: {len(bytes_data)} bytes") |
| 79 | + |
| 80 | +if st.button( |
| 81 | + "Analyze", |
| 82 | + key="Analyze", |
| 83 | + disabled=not is_configured(), |
| 84 | +): |
| 85 | + with st.spinner("Analyzing..."): |
| 86 | + try: |
| 87 | + poller = get_client().begin_analyze_document( |
| 88 | + model_id="prebuilt-layout", |
| 89 | + analyze_request=bytes_data, |
| 90 | + content_type="application/octet-stream", |
| 91 | + output_content_format=ContentFormat.TEXT, |
| 92 | + ) |
| 93 | + result: AnalyzeResult = poller.result() |
| 94 | + output_encoded = base64.b64encode( |
| 95 | + json.dumps( |
| 96 | + result.as_dict(), |
| 97 | + indent=2, |
| 98 | + ).encode() |
| 99 | + ).decode() |
| 100 | + # Generate a link to download the result |
| 101 | + st.markdown( |
| 102 | + f'<a href="data:file/txt;base64,{output_encoded}" \ |
| 103 | + download="{uploaded_file.name}.json">Download Result</a>', |
| 104 | + unsafe_allow_html=True, |
| 105 | + ) |
| 106 | + st.write(result.as_dict()) |
| 107 | + except Exception as e: |
| 108 | + st.error(e) |
0 commit comments