nailarais1 commited on
Commit
e602a12
·
verified ·
1 Parent(s): 6c7fe6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -90
app.py CHANGED
@@ -1,132 +1,122 @@
1
  import sympy as sp
 
2
  import speech_recognition as sr
3
  from gtts import gTTS
4
  import os
5
- import gradio as gr
6
-
7
- # Function to convert speech to text using CMU Sphinx
8
- def voice_to_text_from_microphone(audio=None):
9
- recognizer = sr.Recognizer()
10
-
11
- # If audio file is provided, use it. Otherwise, record from the microphone.
12
- if audio:
13
- # Using uploaded audio file
14
- with sr.AudioFile(audio) as source:
15
- audio_data = recognizer.record(source)
16
- else:
17
- # Using the default microphone as the audio source
18
- with sr.Microphone() as source:
19
- print("Please speak a math expression...")
20
- recognizer.adjust_for_ambient_noise(source)
21
- audio_data = recognizer.listen(source)
22
 
23
- try:
24
- # Using CMU Sphinx for offline recognition
25
- text = recognizer.recognize_sphinx(audio_data)
26
- print(f"Recognized text: {text}")
27
- return text
28
- except sr.UnknownValueError:
29
- return "Sorry, I could not understand the audio."
30
- except sr.RequestError:
31
- return "Could not request results from Sphinx service."
32
-
33
- # Function to map speech to mathematical symbols
34
- def convert_speech_to_math(text):
35
- # Mapping words to math operators
36
- mapping = {
37
- "plus": "+",
38
- "minus": "-",
39
- "times": "*",
40
- "divided by": "/",
41
- "squared": "**2",
42
- "cube": "**3",
43
- "square root of": "sqrt",
44
- "integrate": "integrate",
45
- "derivative of": "diff",
46
- "factorial of": "factorial",
47
- "log of": "log",
48
- "e to the power of": "exp",
49
- "sin of": "sin",
50
- "cos of": "cos",
51
- "tan of": "tan",
52
- "logarithm of": "log"
53
- }
54
-
55
- # Replace speech terms with math symbols
56
- for word, symbol in mapping.items():
57
- text = text.replace(word, symbol)
58
-
59
- # Return the updated expression
60
- return text
61
-
62
- # Function to process the math expression using SymPy
63
  def process_math(query):
64
  try:
65
  # Clean up the input by removing unnecessary spaces
66
- query = query.replace(" ", "") # Removing extra spaces
67
-
68
  # Try to process the query using sympy
69
  expr = sp.sympify(query)
 
 
70
  result = expr
71
 
72
  # Convert the result to speech and save as mp3
73
  result_text = f"The result is: {result}"
74
  tts = gTTS(result_text, lang="en")
75
  tts.save("response.mp3")
76
-
77
- # Return the result text and audio file path
78
  return result_text, "response.mp3"
79
  except Exception as e:
80
  return f"Error: Unable to process the query. Please check the syntax. Error: {e}", None
81
 
82
- # Gradio interface function to handle the input
83
- def calculator(text_input=None, audio=None):
84
- # Check if an audio file is provided, then process it
85
- if audio:
86
- query = voice_to_text_from_microphone(audio)
87
- if query.lower() == "sorry, i could not understand the audio.":
88
- return query, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- print(f"Original Query (Audio): {query}")
 
 
 
 
91
 
92
- # Convert speech to math expression
93
- query = convert_speech_to_math(query)
94
- print(f"Converted Math Expression: {query}")
95
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  elif text_input:
97
- # If text input is provided directly
98
  query = text_input
99
- print(f"Original Query (Text): {query}")
100
  else:
101
  return "No valid input provided.", None
102
 
103
- # Process the math query
104
  return process_math(query)
105
 
106
- # Define the Gradio interface
107
  def start_interface():
 
108
  interface = gr.Interface(
109
  fn=calculator,
110
  inputs=[
111
- gr.Textbox(
112
- label="Enter a Math Expression (Text Input)",
113
- placeholder="Type your math expression here..."
114
- ), # Option for text input
115
- gr.Audio(
116
- label="Upload an Audio File with a Math Expression",
117
- type="filepath"
118
- ), # Option for uploading audio file
119
  ],
120
  outputs=[
121
- "text", # Display the result text
122
  gr.Audio(label="Listen to the result") # Provide the audio result
123
  ],
124
  title="Advanced Math Solver",
125
- description="Solve advanced math problems including factorials, permutations, combinations, logarithms, differentiation, integration, matrices, trigonometric functions, and more.",
126
- theme="huggingface", # Use Hugging Face theme
127
- live=True # Allow live updating as the user interacts
128
  )
129
-
130
  interface.launch()
131
 
132
  if __name__ == "__main__":
 
1
  import sympy as sp
2
+ import gradio as gr
3
  import speech_recognition as sr
4
  from gtts import gTTS
5
  import os
6
+ import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Function to process math queries using sympy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def process_math(query):
10
  try:
11
  # Clean up the input by removing unnecessary spaces
12
+ query = query.replace(" ", "") # Removing extra spaces around operators or parentheses
13
+
14
  # Try to process the query using sympy
15
  expr = sp.sympify(query)
16
+
17
+ # Basic operations handled automatically by sympy
18
  result = expr
19
 
20
  # Convert the result to speech and save as mp3
21
  result_text = f"The result is: {result}"
22
  tts = gTTS(result_text, lang="en")
23
  tts.save("response.mp3")
24
+
25
+ # Return the result text for display and audio for listening
26
  return result_text, "response.mp3"
27
  except Exception as e:
28
  return f"Error: Unable to process the query. Please check the syntax. Error: {e}", None
29
 
30
+ # Function to handle voice input (speech-to-text)
31
+ def voice_to_text(audio):
32
+ recognizer = sr.Recognizer()
33
+ try:
34
+ with sr.AudioFile(audio) as source:
35
+ audio_data = recognizer.record(source)
36
+ text = recognizer.recognize_google(audio_data)
37
+ return text
38
+ except Exception as e:
39
+ return f"Error in voice recognition: {e}"
40
+
41
+ # Mapping words to their corresponding numerical values
42
+ word_to_num = {
43
+ "one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
44
+ "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10,
45
+ "eleven": 11, "twelve": 12, "thirteen": 13, "fourteen": 14,
46
+ "fifteen": 15, "sixteen": 16, "seventeen": 17, "eighteen": 18,
47
+ "nineteen": 19, "twenty": 20, "thirty": 30, "forty": 40,
48
+ "fifty": 50, "sixty": 60, "seventy": 70, "eighty": 80, "ninety": 90,
49
+ "hundred": 100, "thousand": 1000
50
+ }
51
+
52
+ # Function to convert speech input into valid math expressions
53
+ def convert_speech_to_math(text):
54
+ # Replace words with correct math functions (e.g., "factorial" -> "factorial()")
55
+ text = text.lower()
56
 
57
+ # Handle factorial, permutation, combinations, log, etc.
58
+ text = re.sub(r'factorial of (\w+)', lambda m: f'factorial({word_to_num.get(m.group(1), m.group(1))})', text)
59
+ text = re.sub(r'permutation of (\d+), (\d+)', r'permutation(\1, \2)', text)
60
+ text = re.sub(r'combinations of (\d+), (\d+)', r'combinations(\1, \2)', text)
61
+ text = re.sub(r'log of (\d+)', r'log(\1)', text)
62
 
63
+ # Handle calculus terms like differentiation, integration, limit
64
+ text = re.sub(r'differentiate (.+)', r'diff(\1, x)', text)
65
+ text = re.sub(r'integrate (.+)', r'integrate(\1, x)', text)
66
+ text = re.sub(r'limit of (.+)', r'limit(\1, x, 0)', text)
67
+
68
+ # Handle trigonometric functions
69
+ text = re.sub(r'sin of (.+)', r'sin(\1)', text)
70
+ text = re.sub(r'cos of (.+)', r'cos(\1)', text)
71
+ text = re.sub(r'tan of (.+)', r'tan(\1)', text)
72
+
73
+ # Handle inverse trigonometric functions
74
+ text = re.sub(r'asin of (.+)', r'asin(\1)', text)
75
+ text = re.sub(r'acos of (.+)', r'acos(\1)', text)
76
+ text = re.sub(r'atan of (.+)', r'atan(\1)', text)
77
+
78
+ # Handle matrix-related terms
79
+ text = re.sub(r'matrix (\[.*\])', r'Matrix(\1)', text)
80
+
81
+ # Ensure all recognized functions are in the correct format
82
+ text = re.sub(r'(\w+)\s*\(', r'\1(', text) # Remove spaces before parentheses
83
+
84
+ # If there's any common mis-formatting, clean it
85
+ return text
86
+
87
+ # Main function to handle input and process it for math calculation (text or voice)
88
+ def calculator(audio=None, text_input=None):
89
+ if audio:
90
+ query = voice_to_text(audio)
91
+ print(f"Voice Input Detected: {query}") # Debugging: Print detected voice input
92
+ query = convert_speech_to_math(query) # Convert the voice input into valid math expression
93
  elif text_input:
 
94
  query = text_input
95
+ print(f"Text Input: {query}") # Debugging: Print detected text input
96
  else:
97
  return "No valid input provided.", None
98
 
99
+ # Process the math problem
100
  return process_math(query)
101
 
102
+ # Gradio interface setup
103
  def start_interface():
104
+ # Gradio Interface with Voice Input, Text Input, and Output
105
  interface = gr.Interface(
106
  fn=calculator,
107
  inputs=[
108
+ gr.Audio(type="filepath", label="Speak a Math Expression (e.g., factorial(5), differentiate x squared, integrate sin(x), matrix([[1, 2], [3, 4]]) )"), # Voice input (required)
109
+ gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...") # Optional text input
 
 
 
 
 
 
110
  ],
111
  outputs=[
112
+ "text", # Show the text result
113
  gr.Audio(label="Listen to the result") # Provide the audio result
114
  ],
115
  title="Advanced Math Solver",
116
+ description="Solve advanced math problems including factorials, permutations, combinations, logarithms, differentiation, integration, matrices, trigonometric functions, and more. Use speech or text input for accessibility.",
117
+ live=True
 
118
  )
119
+
120
  interface.launch()
121
 
122
  if __name__ == "__main__":