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.title('Passport Recognition Demo') image_path = st.file_uploader("Upload Passport Image", type=["jpg", "jpeg", "png"]) if image_path is not None: st.image(image_path, caption="Uploaded Image.", use_column_width=True) st.write("") st.write("Classifying...") with open("temp_image.jpg", "wb") as f: f.write(image_path.read()) passport_info = recognize_passport("temp_image.jpg") st.markdown(f'## Passport Recognition Results') st.write(json.dumps(passport_info, indent=2))