Skip to content

Commit 7a6291f

Browse files
committed
add document intelligence example
1 parent 508bb6d commit 7a6291f

File tree

7 files changed

+170
-1
lines changed

7 files changed

+170
-1
lines changed

.env.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ AZURE_COSMOS_DB_CONTAINER_NAME="chat"
1717
AZURE_AI_SEARCH_ENDPOINT="https://<YOUR_AZURE_SEARCH_NAME>.search.windows.net/"
1818
AZURE_AI_SEARCH_API_KEY="<YOUR_API_KEY>"
1919
AZURE_AI_SEARCH_INDEX_NAME="chat"
20+
21+
# Azure Document Intelligence: https://github.com/Azure/azure-rest-api-specs/tree/main/specification/ai/data-plane/DocumentIntelligence
22+
AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT="https://<YOUR_AZURE_DOCUMENT_INTELLIGENCE_NAME>.cognitiveservices.azure.com/"
23+
AZURE_DOCUMENT_INTELLIGENCE_API_VERSION="2024-07-31-preview"
24+
AZURE_DOCUMENT_INTELLIGENCE_API_KEY="<YOUR_API_KEY>"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Call Azure Document Intelligence API with Streamlit
2+
3+
This Streamlit app demonstrates how to call Azure Document Intelligence API with Streamlit.
4+
5+
## Prerequisites
6+
7+
- Python 3.10 or later
8+
- Azure Document Intelligence API key
9+
10+
## Usage
11+
12+
1. Get Azure Document Intelligence API key
13+
1. Copy [.env.template](../../.env.template) to `.env` in the same directory
14+
1. Set credentials in `.env`
15+
1. Run [main.py](./main.py)
16+
17+
```shell
18+
# Create a virtual environment
19+
$ python -m venv .venv
20+
21+
# Activate the virtual environment
22+
$ source .venv/bin/activate
23+
24+
# Install dependencies
25+
$ pip install -r requirements.txt
26+
27+
# Run the script
28+
$ python -m streamlit run apps/9_streamlit_azure_document_intelligence/main.py
29+
```
30+
31+
### Example
32+
33+
![Streamlit Chat](../../docs/images/9_streamlit_azure_document_intelligence.main.png)
34+
35+
## References
36+
37+
- [Azure AI Document Intelligence client library for Python - version 1.0.0b3](https://learn.microsoft.com/en-us/python/api/overview/azure/ai-documentintelligence-readme?view=azure-python-preview)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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)
101 KB
Loading

poetry.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ azure-search-documents = "^11.5.1"
2222
azure-identity = "^1.17.1"
2323
ultralytics = "^8.2.77"
2424
mediapipe = "^0.10.14"
25+
azure-ai-documentintelligence = "^1.0.0b3"
2526

2627
[tool.poetry.group.dev.dependencies]
2728
pre-commit = "^3.8.0"

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ azure-identity==1.17.1
1515

1616
# To run 99_streamlit_examples/pages/11_Pose_Estimation.py
1717
# mediapipe==0.10.14
18+
19+
azure-ai-documentintelligence==1.0.0b3

0 commit comments

Comments
 (0)