import streamlit as st import json import os import requests import socket def start_server(): os.system("uvicorn server:app --port 8080 --host 0.0.0.0 --workers 2") st.session_state['server_started'] = True def is_port_in_use(port): import socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex(('0.0.0.0', port)) == 0 def recognize_passport(image_path): files = {'image': open(image_path, 'rb')} response = requests.post("http://0.0.0.0:8080/recognize_passport", files=files) return response.json() if 'server_started' not in st.session_state: st.session_state['server_started'] = False if not st.session_state['server_started']: start_server() st.sidebar.title("Server Status") st.sidebar.write("🟢 Server is running" if st.session_state['server_started'] else "🔴 Server is not running") st.title('Passport Recognition with passportEYE, paddleocr and a LLM') upload_option = st.radio("Select Image Source", ("Upload Image", "Load Example Image"), index=0) if upload_option == "Load Example Image": image_path = "sample.jpg" else: image_path = st.file_uploader("Upload Passport Image", type=["jpg", "jpeg", "png"], key="image_uploader") if image_path is not None and st.button("Read Passport"): with st.spinner("Running..."): if upload_option == "Upload Image": with open("temp_image.jpg", "wb") as f: f.write(image_path.read()) else: passport_info = recognize_passport(image_path) if upload_option == "Upload Image": passport_info = recognize_passport("temp_image.jpg") col1, col2 = st.columns(2) with col1: st.markdown(f'## Passport Recognition Results') st.json(json.dumps(passport_info, indent=2)) with col2: st.image(image_path, caption="Uploaded Image.", use_column_width=True)