shamimjony1000 commited on
Commit
ed3c183
·
verified ·
1 Parent(s): 830a5b7

Update voice_input.py

Browse files
Files changed (1) hide show
  1. voice_input.py +65 -105
voice_input.py CHANGED
@@ -1,105 +1,65 @@
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!")
 
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
+ # Display voice input instructions based on language
10
+ if language_option == "Arabic":
11
+ st.markdown("""
12
+ ### تعليمات الإدخال الصوتي:
13
+ 1. اضغط على زر "بدء التسجيل"
14
+ 2. تحدث بوضوح في الميكروفون
15
+ 3. سيتم معالجة كلامك تلقائياً
16
+ """)
17
+ else:
18
+ st.markdown("""
19
+ ### Voice Input Instructions:
20
+ 1. Click the "Start Recording" button
21
+ 2. Speak clearly into your microphone
22
+ 3. Your speech will be processed automatically
23
+ """)
24
+
25
+ # Check microphone access before showing the record button
26
+ mic_status, mic_message = voice_handler.check_microphone_access()
27
+ if not mic_status:
28
+ st.error(mic_message)
29
+ st.markdown("""
30
+ ### 🎤 Microphone Access Required
31
+
32
+ Please ensure:
33
+ 1. Your browser allows microphone access
34
+ 2. Your microphone is properly connected
35
+ 3. No other application is using the microphone
36
+
37
+ To grant microphone access:
38
+ 1. Click the lock/info icon in your browser's address bar
39
+ 2. Allow microphone access for this site
40
+ 3. Refresh the page after granting access
41
+ """)
42
+ return
43
+
44
+ col1, col2 = st.columns([1, 4])
45
+ with col1:
46
+ if st.button("🎤 Start Recording"):
47
+ selected_language = language_mapping.get(language_option, "en-US")
48
+ with st.spinner("Listening..."):
49
+ voice_input = voice_handler.listen_for_voice(selected_language)
50
+
51
+ if not voice_input.startswith("Error") and not voice_input.startswith("Could not"):
52
+ memory_handler.add_interaction(voice_input)
53
+ with st.spinner("Processing voice input..."):
54
+ context = memory_handler.get_context()
55
+ details = gemini_processor.extract_request_details(voice_input, context)
56
+
57
+ if details:
58
+ st.session_state['voice_details'] = details
59
+ if 'translated_text' in details:
60
+ st.info(f"Translated text: {details['translated_text']}")
61
+ if details.get('missing_fields'):
62
+ missing = ", ".join(details['missing_fields'])
63
+ st.warning(f"Please provide the following missing information: {missing}")
64
+ else:
65
+ st.success("Voice input processed! Please verify the details below.")