""" Pharma Voice Orders - Main Application Streamlit UI for simulating Distributor -> Manufacturer Voice Ordering System """ import streamlit as st import pandas as pd import time import os from pathlib import Path # Page Config st.set_page_config( page_title="Pharma Voice Orders", page_icon="๐Ÿฅ", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS - Avant-Garde Glassmorphic Design def load_css(file_name): with open(file_name) as f: st.markdown(f'', unsafe_allow_html=True) load_css("assets/styles.css") # --- Session State Initialization --- if 'model_ready' not in st.session_state: st.session_state.model_ready = False if 'orders' not in st.session_state: st.session_state.orders = [] if 'last_transcription' not in st.session_state: st.session_state.last_transcription = "" # --- Sidebar --- with st.sidebar: st.image("https://cdn-icons-png.flaticon.com/512/3063/3063167.png", width=50) st.title("PharmaVoice") st.caption("v1.0.0 | Minor Project") st.markdown("---") st.header("โš™๏ธ Configuration") distributor = st.selectbox( "Select Distributor", ["Apollo Pharmacy", "MedPlus", "Frank Ross", "Online Pharma", "Local Chemist"] ) asr_model = st.selectbox( "ASR Model", [ "google/medasr", "openai/whisper-tiny", "openai/whisper-small", "openai/whisper-medium", "openai/whisper-large-v3", ] ) # Note about MedASR - now enabled! if "medasr" in asr_model: st.success("โœ… MedASR enabled (transformers from GitHub installed)") st.markdown("---") # Inference Mode Toggle st.subheader("โšก Inference Mode") # HF Token Configuration # Token should be set via environment variable or entered by user hf_token_input = st.text_input( "๐Ÿ”‘ HF Token", value=os.environ.get("HF_TOKEN", ""), type="password", help="Required for Cloud mode and gated models. Set via HF_TOKEN env var or enter here.", ) # Check for token from input or environment hf_token = hf_token_input or os.environ.get("HF_TOKEN", "") # Mode selection based on token availability if hf_token: inference_mode = st.radio( "Select Mode", ["๐Ÿ’ป Local (Faster)", "โ˜๏ธ Cloud (No Download)"], index=0, help="Cloud uses HF servers. Local downloads model to your PC." ) use_cloud = "Cloud" in inference_mode st.success("๐Ÿ”“ Token configured" + (" โ€ข Cloud Mode" if use_cloud else " โ€ข Local Mode")) else: use_cloud = False st.warning("โš ๏ธ No token โ†’ Local mode only (requires download)") inference_mode = "๐Ÿ’ป Local (Faster)" st.markdown("---") st.info(""" **Instructions:** 1. Select a distributor. 2. Record your voice order. 3. Watch orders route to manufacturers! """) if st.button("๐Ÿ”„ Clear Session", type="secondary"): st.session_state.clear() st.rerun() # --- Cloud Inference (HuggingFace Inference API) --- def transcribe_cloud(audio_data, model_name: str, token: str): """Transcribe audio using HuggingFace Inference API (no local download).""" from huggingface_hub import InferenceClient import io client = InferenceClient(token=token) # Get audio bytes if hasattr(audio_data, 'read'): audio_bytes = audio_data.read() audio_data.seek(0) # Reset for replay else: audio_bytes = audio_data # Call HuggingFace Inference API result = client.automatic_speech_recognition( audio=audio_bytes, model=model_name ) # Result is either a string or dict with 'text' key if isinstance(result, str): return result else: return result.get("text", str(result)) # --- Local ASR Engine (Downloads Model) --- @st.cache_resource(show_spinner=False) def load_asr_engine(model_name: str, token: str = None): """Load ASR engine locally with proper status handling.""" import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline device = "cuda" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 # Login if token provided if token: from huggingface_hub import login login(token=token) # Determine model class based on model name if "medasr" in model_name: from transformers import AutoModelForCTC model_class = AutoModelForCTC else: model_class = AutoModelForSpeechSeq2Seq # Load Model with support for custom code (trust_remote_code=True) try: model = model_class.from_pretrained( model_name, dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, trust_remote_code=True ) except OSError: # Fallback for models that might not support safetensors or other issues model = model_class.from_pretrained( model_name, dtype=torch_dtype, low_cpu_mem_usage=True, trust_remote_code=True ) model.to(device) processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True) pipe = pipeline( "automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, dtype=torch_dtype, device=device, trust_remote_code=True ) return pipe # --- Other Components (Lazy Load) --- @st.cache_resource def get_db(): from simulation.manufacturer_db import ManufacturerDB return ManufacturerDB(data_dir="data") @st.cache_resource def get_preprocessor(): from core.preprocessor import AudioPreprocessor return AudioPreprocessor() @st.cache_resource def get_extractor(_db): from core.entity_extractor import EntityExtractor return EntityExtractor(_db) # --- Model Cache Checker --- def check_model_status(model_name: str) -> dict: """Check if model is cached locally and get disk space info.""" import os import shutil from pathlib import Path # HuggingFace cache directory cache_dir = Path.home() / ".cache" / "huggingface" / "hub" model_folder_name = f"models--{model_name.replace('/', '--')}" model_cache_path = cache_dir / model_folder_name # Check if model is cached is_cached = model_cache_path.exists() and any(model_cache_path.iterdir()) if model_cache_path.exists() else False # Check snapshots folder for actual model files snapshots_path = model_cache_path / "snapshots" if model_cache_path.exists() else None has_model_files = False if snapshots_path and snapshots_path.exists(): for snapshot in snapshots_path.iterdir(): # Check for safetensors or bin files if any(f.suffix in ['.safetensors', '.bin'] for f in snapshot.iterdir() if f.is_file()): has_model_files = True break # Get free disk space (C: drive on Windows) try: disk_usage = shutil.disk_usage(cache_dir if cache_dir.exists() else Path.home()) free_gb = disk_usage.free / (1024 ** 3) except: free_gb = -1 # Model sizes (approximate) model_sizes = { "openai/whisper-tiny": 0.15, "openai/whisper-small": 0.5, "openai/whisper-medium": 1.5, "openai/whisper-large-v3": 3.1, "google/medasr": 0.3, # ~300MB } required_gb = model_sizes.get(model_name, 2.0) return { "is_cached": is_cached and has_model_files, "free_gb": round(free_gb, 1), "required_gb": required_gb, "has_space": free_gb >= required_gb or is_cached, "cache_path": str(model_cache_path) if model_cache_path.exists() else None } # Load non-blocking components db = get_db() preprocessor = get_preprocessor() extractor = get_extractor(db) # --- Main Content --- st.markdown('

๐Ÿฅ Order Processing Center

', unsafe_allow_html=True) st.markdown(f'

Reviewing orders from: {distributor}

', unsafe_allow_html=True) # Smart Model Status Indicator model_status = check_model_status(asr_model) # Use a flex container for the status badge (aligned right) to avoid empty column artifacts status_html = "" if use_cloud: status_html = '''
โ˜๏ธ Cloud Ready
''' elif model_status["is_cached"]: status_html = '''
โœ… Cached (Local)
''' elif model_status["has_space"]: status_html = f'''
โฌ‡๏ธ Download ({model_status["required_gb"]}GB)
''' else: status_html = f'''
โš ๏ธ Low Space ({model_status["free_gb"]}GB free)
''' st.warning(f"Need {model_status['required_gb']}GB, only {model_status['free_gb']}GB free. Choose a smaller model or free disk space.") # Render Status aligned to right if status_html: st.markdown(f'
{status_html}
', unsafe_allow_html=True) # Download confirmation state if 'download_approved' not in st.session_state: st.session_state.download_approved = {} # Show download confirmation ONLY if Local mode AND model not cached AND not yet approved if not use_cloud and not model_status["is_cached"] and asr_model not in st.session_state.download_approved: with st.container(): # Create a placeholder for status updates status_placeholder = st.empty() # Display current status in placeholder if status_html: status_placeholder.markdown(f'
{status_html}
', unsafe_allow_html=True) st.markdown("---") st.markdown(f"### โฌ‡๏ธ Download Required") # Dynamic path display import platform user_home = Path.home() if platform.system() == "Windows": cache_path_str = f"C:\\Users\\{os.environ.get('USERNAME', 'User')}\\.cache\\huggingface\\hub\\" else: cache_path_str = f"/home/user/.cache/huggingface/hub/" st.info(f"""**{asr_model}** is not cached locally. ๐Ÿ“ฆ Size: **{model_status['required_gb']}GB** ๐Ÿ’พ Free space: **{model_status['free_gb']}GB** ๐Ÿ“‚ Cache location: `{cache_path_str}` ๐Ÿ’ก **Tip:** Switch to Cloud Mode to avoid downloading! """) col_yes, col_no = st.columns(2) with col_yes: if st.button("โœ… Yes, Download", type="primary", use_container_width=True): # UI Update: Show "Downloading" in the badge status_placeholder.markdown('''
โณ Downloading...
''', unsafe_allow_html=True) with st.spinner(f"โฌ‡๏ธ Downloading {asr_model}... This may take a while."): try: # Trigger download and load into cache load_asr_engine(asr_model, hf_token) st.session_state.download_approved[asr_model] = True st.session_state.model_ready = True st.success("โœ… Download complete! Model is ready.") time.sleep(1) st.rerun() except Exception as e: st.error(f"โŒ Download failed: {e}") with col_no: if st.button("โŒ Cancel", type="secondary", use_container_width=True): st.info("Download cancelled. Select a cached model or use Cloud Mode.") # Layout: Input (Left) vs Output (Right) col1, col2 = st.columns([1, 2]) with col1: # Voice Container st.markdown('
', unsafe_allow_html=True) st.markdown('

Voice Input

', unsafe_allow_html=True) # Example Prompt Tagline example_prompt = "Send me 50 strips of Paracetamol, 20 bottles of Ascoril syrup, and also 10 tubes of Betnovate cream." st.markdown(f'''
๐Ÿ’ก TRY SAYING:

"{example_prompt}"

''', unsafe_allow_html=True) st.markdown('
๐ŸŽ™๏ธ
', unsafe_allow_html=True) tab1, tab2 = st.tabs(["๐Ÿ”ด Record", "๐Ÿ“ Upload"]) audio_data = None with tab1: try: audio_val_rec = st.audio_input("Click to record", label_visibility="collapsed") if audio_val_rec: audio_data = audio_val_rec except AttributeError: st.warning("Update Streamlit to use `st.audio_input`.") with tab2: audio_val_up = st.file_uploader("Upload Audio", type=['wav', 'mp3', 'm4a', 'ogg'], label_visibility="collapsed") if audio_val_up: audio_data = audio_val_up st.markdown('
', unsafe_allow_html=True) # Process Audio if audio_data: st.success("โœ… Audio captured!") # Audio Conversion Logic # If it's not a WAV file or if it needs processing, we handle conversion here converted_audio = None try: # Basic check: if it's already a wav and we trust it, we might skip, # but converting ensures standardized 16kHz for ASR import io from pydub import AudioSegment # Load audio from the uploaded file type # pydub auto-detects format from file content usually, but we can hint if needed audio_segment = AudioSegment.from_file(audio_data) # Export to simplified WAV (16kHz, mono) which is best for ASR audio_segment = audio_segment.set_frame_rate(16000).set_channels(1) # Export to in-memory bytes wav_buffer = io.BytesIO() audio_segment.export(wav_buffer, format="wav") wav_buffer.seek(0) converted_audio = wav_buffer # Show audio player (use converted for consistent playback, or original) # st.audio(audio_data) # Play original except ImportError: st.error("โŒ `pydub` not installed. Please install it to support audio conversion.") converted_audio = audio_data # Fallback except Exception as e: # Likely ffmpeg missing or bad file if "ffmpeg" in str(e).lower(): st.warning("โš ๏ธ FFmpeg needed for non-WAV files. Attempting to process raw file...") else: st.warning(f"โš ๏ธ Audio conversion warning: {e}") converted_audio = audio_data # Fallback st.audio(audio_data) # Playback original file for user if st.button("๐Ÿš€ Process Order", type="primary", use_container_width=True): transcription_text = "" # Use converted audio if available, else original input_audio = converted_audio if converted_audio is not None else audio_data if use_cloud: # CLOUD MODE - Use HuggingFace Inference API (no download) with st.spinner("โ˜๏ธ Transcribing via Cloud..."): try: transcription_text = transcribe_cloud(input_audio, asr_model, hf_token) st.toast("โœ… Cloud Transcription Complete!") except Exception as e: st.error(f"โŒ Cloud API failed: {e}") st.info("๐Ÿ’ก Try Local mode or check your token/model.") st.stop() else: # LOCAL MODE - Download and run model locally with st.spinner("๐Ÿ”„ Loading Local ASR Model..."): try: asr = load_asr_engine(asr_model, hf_token) st.session_state.model_ready = True except Exception as e: st.error(f"โŒ Model load failed: {e}") st.stop() with st.spinner("๐ŸŽง Transcribing Locally..."): processed_audio = preprocessor.process(input_audio) result = asr(processed_audio) transcription_text = result["text"].replace("", "").strip() st.toast("โœ… Local Transcription Complete!") # Store transcription st.session_state.last_transcription = transcription_text with st.spinner("๐Ÿ“ฆ Extracting Orders..."): extracted_orders = extractor.extract(transcription_text) if extracted_orders: st.success(f"Found {len(extracted_orders)} items!") for order in extracted_orders: st.session_state.orders.append(order) st.rerun() else: st.warning("No medicines found. Try: 'Send 20 strips of Augmentin'") st.markdown("---") st.markdown("### ๐Ÿ“ Transcription") current_text = st.session_state.get('last_transcription', "") st.text_area( "Transcription Output", current_text, height=120, disabled=True, placeholder="Transcription will appear here...", label_visibility="collapsed" ) with col2: st.markdown("### ๐Ÿญ Manufacturer Routing") # Get grouped orders from session state from simulation.order_queue import OrderQueue queue = OrderQueue() grouped_orders = queue.get_grouped_orders(db) all_manufacturers = db.get_all_manufacturers() # Grid Layout import textwrap row1_cols = st.columns(2) row2_cols = st.columns(2) row3_cols = st.columns(2) # 6 Manufacturers -> 3 Rows of 2 for idx, mfr in enumerate(all_manufacturers): if idx < 2: col = row1_cols[idx] elif idx < 4: col = row2_cols[idx - 2] elif idx < 6: col = row3_cols[idx - 4] else: continue with col: mfr_name = mfr['name'] orders = grouped_orders.get(mfr_name, []) order_count = len(orders) # Determine Visual State is_active = order_count > 0 active_class = "active" if is_active else "" badge_class = "active" if is_active else "" # Generate HTML - Single line to prevent Markdown parsing issues html_parts = [] # 1. Header & Open Body html_parts.append(f'
') html_parts.append('
') html_parts.append(f'๐Ÿญ {mfr_name}') html_parts.append(f'{order_count} Items') html_parts.append('
') # 2. Body Content if is_active: for order in orders: # Confidence Logic conf = order.get('confidence', 0) conf_class = "conf-low" if conf >= 90: conf_class = "conf-high" elif conf >= 75: conf_class = "conf-med" med_name = order.get('medicine_standardized', order['medicine']) dosage = order.get('dosage', '-') html_parts.append(f'
') html_parts.append('
') html_parts.append(f'{med_name}') html_parts.append(f'{dosage}') html_parts.append('
') html_parts.append(f'{order["quantity"]}') html_parts.append('
') else: html_parts.append('
Waiting for data...
') # 3. Close Body & Card html_parts.append('
') st.markdown("".join(html_parts), unsafe_allow_html=True) # Unknown Orders (Quarantine Node) unknowns = grouped_orders.get('Unknown', []) if unknowns: html_parts = [] html_parts.append('
') html_parts.append('
') html_parts.append('โš ๏ธ Quarantine / Unmapped') html_parts.append(f'{len(unknowns)} Items') html_parts.append('
') for order in unknowns: html_parts.append('
') html_parts.append('
') html_parts.append(f'{order["medicine"]} (Raw)') html_parts.append(f'Confidence: {order.get("confidence", 0)}%') html_parts.append('
') html_parts.append(f'{order["quantity"]}') html_parts.append('
') html_parts.append('
') st.markdown("".join(html_parts), unsafe_allow_html=True) st.markdown("---") # Export Buttons if st.session_state.orders: from core.excel_exporter import ExcelExporter col_excel, col_csv = st.columns(2) with col_excel: excel_data = ExcelExporter.export(st.session_state.orders, db=db) st.download_button( label="๐Ÿ“ฅ Export to Excel", data=excel_data, file_name="pharma_orders.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", use_container_width=True ) with col_csv: csv_data = ExcelExporter.export_csv(st.session_state.orders, db=db) st.download_button( label="๐Ÿ“„ Export to CSV", data=csv_data, file_name="pharma_orders.csv", mime="text/csv", use_container_width=True ) # --- Informational Footer (New Section) --- # --- Informational Footer (New Section) --- footer_html = [] footer_html.append('
') footer_html.append('
') # 1. How to Use Section footer_html.append('
') footer_html.append('

๐Ÿ’ก How to use it

') footer_html.append('
    ') footer_html.append('
  • ') footer_html.append('๐Ÿ”น Mixed Manufacturers:') footer_html.append('"Send Paracetamol tablet 300 strips, also Azithromycin 50 strips and Volini spray 20 pieces."') footer_html.append('
  • ') footer_html.append('
  • ') footer_html.append('๐Ÿ”น Forms & Units:') footer_html.append('"Order 50 bottles of Ascoril syrup, 20 tubes of Betnovate cream, and 10 packs of Prega News."') footer_html.append('
  • ') footer_html.append('
  • ') footer_html.append('๐Ÿ”น Pronunciation/Noisy:') footer_html.append('"Uh, give me some Combiflam... maybe 20 strips? And... Zinetac 150."') footer_html.append('
  • ') footer_html.append('
') # 2. Medical Areas Section footer_html.append('
') footer_html.append('

๐Ÿฅ Medical Areas Covered

') footer_html.append('
    ') footer_html.append('
  • ') footer_html.append('๐Ÿฌ Syrups') footer_html.append('("50 bottles of Ascoril")') footer_html.append('
  • ') footer_html.append('
  • ') footer_html.append('๐Ÿงด Creams/Gels') footer_html.append('("20 tubes of Betnovate")') footer_html.append('
  • ') footer_html.append('
  • ') footer_html.append('๐Ÿ’‰ Injections') footer_html.append('("10 vials of Amikacin")') footer_html.append('
  • ') footer_html.append('
  • ') footer_html.append('๐Ÿ’จ Sprays/Inhalers') footer_html.append('("5 pcs of Volini spray")') footer_html.append('
  • ') footer_html.append('
  • ') footer_html.append('๐Ÿ’Š Tablets/Capsules') footer_html.append('("100 strips of Paracetamol")') footer_html.append('
  • ') footer_html.append('
') footer_html.append('
') st.markdown("".join(footer_html), unsafe_allow_html=True)