shamimjony1000 commited on
Commit
0b4e35f
1 Parent(s): c8b7640

Update voice_input.py

Browse files
Files changed (1) hide show
  1. voice_input.py +92 -59
voice_input.py CHANGED
@@ -1,72 +1,105 @@
1
  import streamlit as st
2
- from voice_handler import VoiceHandler
3
- from memory_handler import MemoryHandler
4
- from gemini_processor import GeminiProcessor
5
 
6
- def render_voice_input(voice_handler, language_option, language_mapping, memory_handler, gemini_processor):
7
- """Render the voice input section with improved error handling"""
8
-
9
- st.markdown("""
10
- ### 🎤 Voice Input Requirements
11
- - Use Chrome, Firefox, or Edge browser
12
- - Grant microphone permissions when prompted
13
- - Ensure microphone is connected and working
14
- """)
15
-
16
- # Display voice input instructions based on language
17
  if language_option == "Arabic":
18
  st.markdown("""
19
- ### تعليمات الإدخال الصوتي:
20
- 1. اضغط على زر "بدء التسجيل"
21
- 2. تحدث بوضوح في الميكروفون
22
- 3. سيتم معالجة كلامك تلقائياً
23
  """)
24
  else:
25
  st.markdown("""
26
- ### Voice Input Instructions:
27
- 1. Click the "Start Recording" button
28
- 2. Speak clearly into your microphone
29
- 3. Your speech will be processed automatically
30
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Check microphone access before showing the record button
33
- mic_status, mic_message = voice_handler.check_microphone_access()
34
- if not mic_status:
35
- st.error(mic_message)
36
- st.markdown("""
37
- ### 🔍 Microphone Access Guide
38
 
39
- 1. Look for the camera/lock icon in your browser's address bar
40
- 2. Click it and select "Allow" for microphone access
41
- 3. Refresh the page
42
 
43
- If still not working:
44
- - Try a different browser (Chrome recommended)
45
- - Check if microphone works in other applications
46
- - Verify system sound settings
47
- - Restart your browser
48
- """)
49
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- col1, col2 = st.columns([1, 4])
52
  with col1:
53
- if st.button("🎤 Start Recording"):
54
- selected_language = language_mapping.get(language_option, "en-US")
55
- with st.spinner("Listening..."):
56
- voice_input = voice_handler.listen_for_voice(selected_language)
57
-
58
- if not voice_input.startswith("Error") and not voice_input.startswith("Could not"):
59
- memory_handler.add_interaction(voice_input)
60
- with st.spinner("Processing voice input..."):
61
- context = memory_handler.get_context()
62
- details = gemini_processor.extract_request_details(voice_input, context)
63
-
64
- if details:
65
- st.session_state['voice_details'] = details
66
- if 'translated_text' in details:
67
- st.info(f"Translated text: {details['translated_text']}")
68
- if details.get('missing_fields'):
69
- missing = ", ".join(details['missing_fields'])
70
- st.warning(f"Please provide the following missing information: {missing}")
71
- else:
72
- st.success("Voice input processed! Please verify the details below.")
 
1
  import streamlit as st
2
+ from typing import Dict, Optional
 
 
3
 
4
+ def render_voice_examples(language_option: str) -> None:
5
+ """Render voice input examples based on language"""
 
 
 
 
 
 
 
 
 
6
  if language_option == "Arabic":
7
  st.markdown("""
8
+ جرب أن تقول شيئاً مثل:
9
+ > "أحتاج إلى طلب 500 ريال للمشروع 223 المسمى جامعة أبها لشراء بعض الأدوات"
 
 
10
  """)
11
  else:
12
  st.markdown("""
13
+ Try saying something like:
14
+ > "I need to request 500 riyals for project 223 named Abha University to buy some tools"
 
 
15
  """)
16
+
17
+ def handle_voice_input(voice_handler, language: str, memory_handler, gemini_processor) -> Optional[Dict]:
18
+ """Handle voice input and processing"""
19
+ with st.spinner("Checking microphone access..."):
20
+ if not voice_handler.check_microphone_access():
21
+ st.error("""
22
+ Could not access microphone. Please ensure:
23
+ 1. You're using a secure (HTTPS) connection
24
+ 2. You've granted microphone permissions in your browser
25
+ 3. Your microphone is properly connected and working
26
+
27
+ Try refreshing the page and allowing microphone access when prompted.
28
+ """)
29
+ return None
30
 
31
+ with st.spinner("Listening... Please speak clearly"):
32
+ voice_text = voice_handler.listen_for_voice(language)
 
 
 
 
33
 
34
+ if voice_text.startswith("Error:"):
35
+ st.error(voice_text)
36
+ return None
37
 
38
+ if voice_text.startswith("Could not"):
39
+ st.warning(voice_text)
40
+ return None
41
+
42
+ st.success("Voice captured!")
43
+ st.write("You said:", voice_text)
44
+
45
+ # Add to memory
46
+ memory_handler.add_interaction(voice_text)
47
+
48
+ with st.spinner("Processing voice input..."):
49
+ context = memory_handler.get_context()
50
+ details = gemini_processor.extract_request_details(voice_text, context)
51
+
52
+ if not details:
53
+ st.error("Could not extract request details. Please try again or use manual input.")
54
+ return None
55
+
56
+ if 'translated_text' in details:
57
+ st.info(f"Translated text: {details['translated_text']}")
58
+
59
+ if details.get('missing_fields'):
60
+ missing = ", ".join(details['missing_fields'])
61
+ st.warning(f"Please provide the following missing information: {missing}")
62
+ else:
63
+ st.success("Voice input processed! Please verify the details below.")
64
+
65
+ return details
66
+
67
+ def render_voice_input(voice_handler, language_option: str, language_mapping: Dict[str, str],
68
+ memory_handler, gemini_processor) -> None:
69
+ """Render the voice input section"""
70
+ if not voice_handler.permission_granted:
71
+ st.info("Microphone access is required for voice input. Click below to enable it.")
72
+ if st.button("Enable Microphone Access"):
73
+ if voice_handler.request_permissions():
74
+ st.success("Microphone access granted! You can now use voice input.")
75
+ st.rerun()
76
+ else:
77
+ st.error("""
78
+ Could not access microphone. Please:
79
+ 1. Check if your browser blocks microphone access
80
+ 2. Allow microphone access in your browser settings
81
+ 3. Ensure your microphone is properly connected
82
+ """)
83
+
84
+ col1, col2, col3 = st.columns([3, 1, 1])
85
 
 
86
  with col1:
87
+ render_voice_examples(language_option)
88
+
89
+ with col2:
90
+ if st.button("🎤 Start Voice Input"):
91
+ details = handle_voice_input(
92
+ voice_handler,
93
+ language_mapping[language_option],
94
+ memory_handler,
95
+ gemini_processor
96
+ )
97
+ if details:
98
+ st.session_state['voice_details'] = details
99
+
100
+ with col3:
101
+ if st.button("🗑️ Clear Memory"):
102
+ memory_handler.clear_memory()
103
+ if 'voice_details' in st.session_state:
104
+ del st.session_state['voice_details']
105
+ st.success("Memory cleared!")