import streamlit as st from voice_handler import VoiceHandler from memory_handler import MemoryHandler from gemini_processor import GeminiProcessor def render_voice_input(voice_handler, language_option, language_mapping, memory_handler, gemini_processor): """Render the voice input section with improved error handling""" st.markdown(""" ### 🎤 Voice Input Requirements - Use Chrome, Firefox, or Edge browser - Grant microphone permissions when prompted - Ensure microphone is connected and working """) # Display voice input instructions based on language if language_option == "Arabic": st.markdown(""" ### تعليمات الإدخال الصوتي: 1. اضغط على زر "بدء التسجيل" 2. تحدث بوضوح في الميكروفون 3. سيتم معالجة كلامك تلقائياً """) else: st.markdown(""" ### Voice Input Instructions: 1. Click the "Start Recording" button 2. Speak clearly into your microphone 3. Your speech will be processed automatically """) # Check microphone access before showing the record button mic_status, mic_message = voice_handler.check_microphone_access() if not mic_status: st.error(mic_message) st.markdown(""" ### 🔍 Microphone Access Guide 1. Look for the camera/lock icon in your browser's address bar 2. Click it and select "Allow" for microphone access 3. Refresh the page If still not working: - Try a different browser (Chrome recommended) - Check if microphone works in other applications - Verify system sound settings - Restart your browser """) return col1, col2 = st.columns([1, 4]) with col1: if st.button("🎤 Start Recording"): selected_language = language_mapping.get(language_option, "en-US") with st.spinner("Listening..."): voice_input = voice_handler.listen_for_voice(selected_language) if not voice_input.startswith("Error") and not voice_input.startswith("Could not"): memory_handler.add_interaction(voice_input) with st.spinner("Processing voice input..."): context = memory_handler.get_context() details = gemini_processor.extract_request_details(voice_input, context) if details: st.session_state['voice_details'] = details if 'translated_text' in details: st.info(f"Translated text: {details['translated_text']}") if details.get('missing_fields'): missing = ", ".join(details['missing_fields']) st.warning(f"Please provide the following missing information: {missing}") else: st.success("Voice input processed! Please verify the details below.")