shamimjony1000 commited on
Commit
7486ea3
·
verified ·
1 Parent(s): fb96f20

Upload voice_input.py

Browse files
Files changed (1) hide show
  1. voice_input.py +105 -0
voice_input.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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!")