Spaces:
Running
Running
| import streamlit as st | |
| import os | |
| from google import genai | |
| from google.genai import types | |
| from pydantic import BaseModel, Field | |
| from PIL import Image | |
| # 1. Define the Data Structure (The "Schema") | |
| # This tells the AI exactly which fields to find on the license. | |
| class ProviderLicense(BaseModel): | |
| provider_name: str = Field(description="Full name of the healthcare provider") | |
| license_number: str = Field(description="The professional license number") | |
| state: str = Field(description="The state where the license was issued") | |
| expiration_date: str = Field(description="Format: YYYY-MM-DD") | |
| is_valid: bool = Field(description="True if the license is not expired based on today's date") | |
| # 2. Page Configuration | |
| st.set_page_config(page_title="AI Credentialing Assistant", layout="wide") | |
| st.title("🩺 Provider Credentialing AI") | |
| st.markdown("Automated Intelligent Document Processing for Medical Licenses.") | |
| # 3. Initialize Gemini Client | |
| # Uses the 'Secret' you saved in Hugging Face Settings | |
| api_key = os.environ.get("GEMINI_API_KEY") | |
| if not api_key: | |
| st.error("API Key not found. Please add GEMINI_API_KEY to your Space Secrets.") | |
| st.stop() | |
| client = genai.Client(api_key=api_key) | |
| # 4. Sidebar - File Upload | |
| st.sidebar.header("Document Upload") | |
| uploaded_file = st.sidebar.file_uploader("Upload a medical license (JPG/PNG)", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file: | |
| # Create two columns for a professional dashboard look | |
| col1, col2 = st.columns(2) | |
| image = Image.open(uploaded_file) | |
| with col1: | |
| st.subheader("Document Preview") | |
| st.image(image, use_container_width=True) | |
| with col2: | |
| st.subheader("Extracted Credentials") | |
| with st.spinner("AI is analyzing the document..."): | |
| try: | |
| # The AI Logic | |
| response = client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=["Extract the credentialing details from this document.", image], | |
| config=types.GenerateContentConfig( | |
| response_mime_type="application/json", | |
| response_schema=ProviderLicense, | |
| ), | |
| ) | |
| # Retrieve the structured data | |
| data = response.parsed | |
| # Display results in a clean format | |
| st.success("Extraction Complete") | |
| st.metric("Provider Name", data.provider_name) | |
| st.write(f"**License Number:** {data.license_number}") | |
| st.write(f"**State:** {data.state}") | |
| st.write(f"**Expires:** {data.expiration_date}") | |
| if data.is_valid: | |
| st.info("✅ Status: Valid/Active") | |
| else: | |
| st.warning("⚠️ Status: Expired or Invalid") | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| else: | |
| st.info("Please upload a document to begin the automated verification process.") |