Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Streamlit app setup
|
6 |
+
st.title('Llama Cloud API Document Extraction')
|
7 |
+
|
8 |
+
# API Key Input
|
9 |
+
api_key = st.text_input('Enter your Llama Cloud API Key:', type='password')
|
10 |
+
|
11 |
+
# File Upload
|
12 |
+
uploaded_file = st.file_uploader('Choose a PDF file', type='pdf')
|
13 |
+
|
14 |
+
if uploaded_file is not None:
|
15 |
+
# Upload file to Llama Cloud
|
16 |
+
headers = {
|
17 |
+
'accept': 'application/json',
|
18 |
+
'Authorization': f'Bearer {api_key}'
|
19 |
+
}
|
20 |
+
files = {'upload_file': (uploaded_file.name, uploaded_file, 'application/pdf')}
|
21 |
+
response = requests.post('https://api.cloud.llamaindex.ai/api/v1/files', headers=headers, files=files)
|
22 |
+
file_id = response.json()['id']
|
23 |
+
st.write(f'File uploaded with ID: {file_id}')
|
24 |
+
|
25 |
+
# Infer Schema
|
26 |
+
schema_data = {
|
27 |
+
'name': 'Inferred Schema',
|
28 |
+
'file_ids': [file_id]
|
29 |
+
}
|
30 |
+
schema_response = requests.post('https://api.cloud.llamaindex.ai/api/v1/extraction/schemas/infer', headers=headers, json=schema_data)
|
31 |
+
schema_id = schema_response.json()['id']
|
32 |
+
st.write(f'Schema inferred with ID: {schema_id}')
|
33 |
+
|
34 |
+
# Start Extraction Job
|
35 |
+
job_data = {
|
36 |
+
'schema_id': schema_id,
|
37 |
+
'file_id': file_id
|
38 |
+
}
|
39 |
+
job_response = requests.post('https://api.cloud.llamaindex.ai/api/v1/extraction/jobs', headers=headers, json=job_data)
|
40 |
+
job_id = job_response.json()['id']
|
41 |
+
st.write(f'Extraction job started with ID: {job_id}')
|
42 |
+
|
43 |
+
# Check Job Status
|
44 |
+
status_response = requests.get(f'https://api.cloud.llamaindex.ai/api/v1/extraction/jobs/{job_id}', headers=headers)
|
45 |
+
status = status_response.json()['status']
|
46 |
+
st.write(f'Job Status: {status}')
|
47 |
+
|
48 |
+
# Display Results
|
49 |
+
if status == 'completed':
|
50 |
+
results_response = requests.get(f'https://api.cloud.llamaindex.ai/api/v1/extraction/jobs/{job_id}/result', headers=headers)
|
51 |
+
results = results_response.json()
|
52 |
+
st.write('Extraction Results:')
|
53 |
+
st.json(results)
|
54 |
+
else:
|
55 |
+
st.write('Extraction job is still in progress or has failed.')
|