Spaces:
Paused
Paused
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -2
src/streamlit_app.py
CHANGED
|
@@ -8,6 +8,7 @@ from document_classifier import DocumentClassifier
|
|
| 8 |
import time
|
| 9 |
from typing import List, Dict
|
| 10 |
import json
|
|
|
|
| 11 |
|
| 12 |
# Page configuration
|
| 13 |
st.set_page_config(
|
|
@@ -108,6 +109,29 @@ def classify_multiple_files(file_paths: List[str]) -> List[Dict]:
|
|
| 108 |
except Exception as e:
|
| 109 |
return [{"error": str(e), "success": False}]
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def display_classification_result(result: Dict):
|
| 112 |
"""Display a single classification result."""
|
| 113 |
if not result.get('success', False):
|
|
@@ -259,7 +283,18 @@ def main():
|
|
| 259 |
tab1, tab2, tab3 = st.tabs(["π Single File", "π Batch Upload", "π Results"])
|
| 260 |
|
| 261 |
with tab1:
|
| 262 |
-
st.subheader("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
uploaded_file = st.file_uploader(
|
| 265 |
"Choose a document file",
|
|
@@ -386,4 +421,5 @@ def main():
|
|
| 386 |
)
|
| 387 |
|
| 388 |
if __name__ == "__main__":
|
| 389 |
-
main()
|
|
|
|
|
|
| 8 |
import time
|
| 9 |
from typing import List, Dict
|
| 10 |
import json
|
| 11 |
+
import requests
|
| 12 |
|
| 13 |
# Page configuration
|
| 14 |
st.set_page_config(
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
return [{"error": str(e), "success": False}]
|
| 111 |
|
| 112 |
+
def classify_with_api(file_path: str) -> Dict:
|
| 113 |
+
"""Call FastAPI classify endpoint with image file."""
|
| 114 |
+
api_url = "http://localhost:8000/classify" # Adjust if API runs elsewhere
|
| 115 |
+
try:
|
| 116 |
+
with open(file_path, "rb") as file_data:
|
| 117 |
+
files = {"file": (os.path.basename(file_path), file_data)}
|
| 118 |
+
response = requests.post(api_url, files=files)
|
| 119 |
+
if response.status_code == 200:
|
| 120 |
+
data = response.json()
|
| 121 |
+
# Some keys might not be present in API/minimal - match display_classification_result
|
| 122 |
+
data.setdefault('file_path', file_path)
|
| 123 |
+
data.setdefault('file_name', os.path.basename(file_path))
|
| 124 |
+
data.setdefault('file_extension', os.path.splitext(file_path)[1].replace('.', ''))
|
| 125 |
+
data.setdefault('content_length', 0)
|
| 126 |
+
data.setdefault('text_preview', '')
|
| 127 |
+
data.setdefault('file_type', os.path.splitext(file_path)[1].replace('.', ''))
|
| 128 |
+
data.setdefault('all_scores', {})
|
| 129 |
+
return data
|
| 130 |
+
else:
|
| 131 |
+
return {"error": response.text, "success": False}
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return {"error": str(e), "success": False}
|
| 134 |
+
|
| 135 |
def display_classification_result(result: Dict):
|
| 136 |
"""Display a single classification result."""
|
| 137 |
if not result.get('success', False):
|
|
|
|
| 283 |
tab1, tab2, tab3 = st.tabs(["π Single File", "π Batch Upload", "π Results"])
|
| 284 |
|
| 285 |
with tab1:
|
| 286 |
+
st.subheader("\U0001F4C1 Classify Single Document")
|
| 287 |
+
sample_scan_folder = os.path.join(os.path.dirname(__file__), "sample_scans")
|
| 288 |
+
sample_files = [f for f in os.listdir(sample_scan_folder) if f.lower().endswith((".png", ".jpg", ".jpeg"))]
|
| 289 |
+
sample_files.sort()
|
| 290 |
+
selected_sample = st.selectbox("Or select from example scans:", ["--- Select a sample ---"] + sample_files)
|
| 291 |
+
if selected_sample != "--- Select a sample ---":
|
| 292 |
+
sample_file_path = os.path.join(sample_scan_folder, selected_sample)
|
| 293 |
+
st.image(sample_file_path, caption=f"Sample: {selected_sample}", use_column_width=True)
|
| 294 |
+
if st.button("π Classify Sample Scan", key="classify_sample_scan"):
|
| 295 |
+
with st.spinner("Calling API to classify sample scan..."):
|
| 296 |
+
result = classify_with_api(sample_file_path)
|
| 297 |
+
display_classification_result(result)
|
| 298 |
|
| 299 |
uploaded_file = st.file_uploader(
|
| 300 |
"Choose a document file",
|
|
|
|
| 421 |
)
|
| 422 |
|
| 423 |
if __name__ == "__main__":
|
| 424 |
+
main()
|
| 425 |
+
|