Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,119 +1,117 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
time.sleep(0.5)
|
| 119 |
-
st.rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# ----------------- Streamlit Setup ----------------- #
|
| 5 |
+
st.set_page_config(page_title="Lease Agreement Extractor (OCR + AI)", layout="centered")
|
| 6 |
+
st.title("π Lease Agreement Extractor (OCR + AI)")
|
| 7 |
+
|
| 8 |
+
# ----------------- Assistant & API Config ----------------- #
|
| 9 |
+
OPENAI_ASSISTANT_ID = "asst_xBnNfiyWmVa4iF3CgXwJnmBt" # Replace with your assistant ID
|
| 10 |
+
OPENAI_API_KEY = "YOUR_OPENAI_API_KEY" # Replace with your OpenAI key
|
| 11 |
+
|
| 12 |
+
HEADERS = {
|
| 13 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 14 |
+
"Content-Type": "application/json"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# ----------------- Upload Interface ----------------- #
|
| 18 |
+
uploaded_file = st.file_uploader("Upload a lease agreement (PDF, PNG, JPG)", type=["pdf", "png", "jpg", "jpeg"])
|
| 19 |
+
|
| 20 |
+
def upload_file_to_openai(file_bytes, filename):
|
| 21 |
+
files = {
|
| 22 |
+
"file": (filename, file_bytes, "application/octet-stream")
|
| 23 |
+
}
|
| 24 |
+
response = requests.post("https://api.openai.com/v1/files", headers={"Authorization": f"Bearer {OPENAI_API_KEY}"}, files=files)
|
| 25 |
+
return response.json().get("id")
|
| 26 |
+
|
| 27 |
+
def create_thread_and_run(file_id):
|
| 28 |
+
# Create thread
|
| 29 |
+
thread_res = requests.post("https://api.openai.com/v1/threads", headers=HEADERS, json={})
|
| 30 |
+
thread_data = thread_res.json()
|
| 31 |
+
thread_id = thread_data.get("id")
|
| 32 |
+
|
| 33 |
+
# Run assistant with structured lease extraction prompt
|
| 34 |
+
run_payload = {
|
| 35 |
+
"assistant_id": OPENAI_ASSISTANT_ID,
|
| 36 |
+
"instructions": """
|
| 37 |
+
You are an AI assistant designed to extract structured lease data from any uploaded agreement, even if it is scanned. Your responsibilities are:
|
| 38 |
+
|
| 39 |
+
- Accept PDF or image-based lease documents.
|
| 40 |
+
- Use OCR (Optical Character Recognition) if the document is non-selectable or scanned.
|
| 41 |
+
- Parse and extract key structured data fields from lease agreements.
|
| 42 |
+
- Output results in JSON format following a fixed schema.
|
| 43 |
+
|
| 44 |
+
OCR & Extraction Rules:
|
| 45 |
+
1. If text cannot be extracted normally, perform OCR using image-to-text on each page.
|
| 46 |
+
2. Preserve paragraph structure, detect headers, signature blocks, tables, and dates.
|
| 47 |
+
|
| 48 |
+
Expected JSON Output:
|
| 49 |
+
{
|
| 50 |
+
"document_title": "Lease Agreement Title or File Name",
|
| 51 |
+
"parties": {
|
| 52 |
+
"lessor": "Name of Lessor",
|
| 53 |
+
"lessee": "Name of Lessee"
|
| 54 |
+
},
|
| 55 |
+
"property_description": "Detailed description of leased property",
|
| 56 |
+
"term": {
|
| 57 |
+
"start_date": "YYYY-MM-DD",
|
| 58 |
+
"end_date": "YYYY-MM-DD",
|
| 59 |
+
"renewal_options": "Yes/No or Clause Text"
|
| 60 |
+
},
|
| 61 |
+
"financials": {
|
| 62 |
+
"monthly_rent": "Amount",
|
| 63 |
+
"deposit": "Amount",
|
| 64 |
+
"escalation_clause": "Text if present"
|
| 65 |
+
},
|
| 66 |
+
"obligations": {
|
| 67 |
+
"maintenance": "Lessor/Lessee",
|
| 68 |
+
"insurance": "Lessor/Lessee",
|
| 69 |
+
"subletting": "Allowed/Not allowed"
|
| 70 |
+
},
|
| 71 |
+
"signatures": [
|
| 72 |
+
{
|
| 73 |
+
"party": "Party Name",
|
| 74 |
+
"signed_by": "Full Name",
|
| 75 |
+
"date": "YYYY-MM-DD",
|
| 76 |
+
"position": "Title if listed"
|
| 77 |
+
}
|
| 78 |
+
],
|
| 79 |
+
"raw_text_per_page": {
|
| 80 |
+
"page_1": "Full OCR text of page 1",
|
| 81 |
+
"page_2": "Full OCR text of page 2"
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
π Behaviors:
|
| 86 |
+
- Automatically detect if OCR is needed.
|
| 87 |
+
- Normalize dates to YYYY-MM-DD.
|
| 88 |
+
- Use "Not Found" or null where info is missing.
|
| 89 |
+
- Return JSON only.
|
| 90 |
+
- If OCR text is low-confidence, include: "low_confidence": true
|
| 91 |
+
""",
|
| 92 |
+
"file_ids": [file_id]
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
run_res = requests.post(f"https://api.openai.com/v1/threads/{thread_id}/runs", headers=HEADERS, json=run_payload)
|
| 96 |
+
run_data = run_res.json()
|
| 97 |
+
run_id = run_data.get("id")
|
| 98 |
+
|
| 99 |
+
return thread_id, run_id
|
| 100 |
+
|
| 101 |
+
# ----------------- Main UI Flow ----------------- #
|
| 102 |
+
if uploaded_file is not None:
|
| 103 |
+
st.success("π File uploaded successfully.")
|
| 104 |
+
|
| 105 |
+
if st.button("π Run Lease Extraction"):
|
| 106 |
+
with st.spinner("Uploading and invoking assistant..."):
|
| 107 |
+
file_id = upload_file_to_openai(uploaded_file.getvalue(), uploaded_file.name)
|
| 108 |
+
if file_id:
|
| 109 |
+
thread_id, run_id = create_thread_and_run(file_id)
|
| 110 |
+
st.success("β
Assistant run started!")
|
| 111 |
+
st.code(f"Thread ID: {thread_id}", language="text")
|
| 112 |
+
st.code(f"Run ID: {run_id}", language="text")
|
| 113 |
+
st.markdown("π You can retrieve results via OpenAI API using these IDs.")
|
| 114 |
+
else:
|
| 115 |
+
st.error("β File upload to OpenAI failed.")
|
| 116 |
+
else:
|
| 117 |
+
st.info("Please upload a lease agreement file to begin.")
|
|
|
|
|
|