nailarais1 commited on
Commit
6ff812d
·
verified ·
1 Parent(s): 3180a06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -44
app.py CHANGED
@@ -1,47 +1,15 @@
1
  import sympy as sp
2
  import gradio as gr
3
- import re
4
- import os
5
  import speech_recognition as sr
6
  from gtts import gTTS
 
7
 
8
- # Function to intelligently detect and fix input errors using simple AI-based rules
9
- def intelligent_error_correction(query):
10
- """
11
- A simple AI-based function to suggest corrections for common math expression mistakes.
12
- """
13
- suggestions = []
14
-
15
- # Detect missing parentheses for functions like factorial, permutation, etc.
16
- if "factorial" in query and "(" not in query:
17
- suggestions.append("Did you mean factorial(x)? Please make sure to enclose the number inside parentheses like factorial(5).")
18
- if "permutation" in query and "(" not in query:
19
- suggestions.append("Did you mean permutation(n, k)? Ensure both numbers are inside parentheses like permutation(5, 3).")
20
- if "combinations" in query and "(" not in query:
21
- suggestions.append("Did you mean combinations(n, k)? Ensure both numbers are inside parentheses like combinations(5, 3).")
22
-
23
- # Check for spaces between function name and parentheses
24
- if re.search(r"\w\s*\(", query):
25
- suggestions.append("Make sure there are no spaces between the function name and parentheses (e.g., factorial(5) not factorial (5)).")
26
-
27
- # Handle common syntax issues like missing operators or numbers
28
- if re.search(r"[\+\-\*\/]\s*$", query): # Ends with an operator without a number after
29
- suggestions.append("It seems you have an operator at the end of the expression, but no number to apply it to. Please fix it.")
30
-
31
- if not suggestions:
32
- suggestions.append("Your expression looks good, but if you're unsure, check for spaces or parentheses.")
33
-
34
- return suggestions
35
-
36
- # Function to process math queries, with AI to guide error resolution
37
  def process_math(query):
38
  try:
39
  # Clean up the input by removing unnecessary spaces
40
  query = query.replace(" ", "") # Removing extra spaces around operators or parentheses
41
 
42
- # Use intelligent error correction
43
- error_suggestions = intelligent_error_correction(query)
44
-
45
  # Try to process the query using sympy
46
  expr = sp.sympify(query)
47
 
@@ -52,14 +20,11 @@ def process_math(query):
52
  result_text = f"The result is: {result}"
53
  tts = gTTS(result_text, lang="en")
54
  tts.save("response.mp3")
 
55
 
56
- # Return the result as audio
57
- return result_text, "response.mp3"
58
-
59
  except Exception as e:
60
- # If there is an error, suggest corrections
61
- suggestions = "\n".join(intelligent_error_correction(query))
62
- return f"Error: Unable to process the query. Details: {e}. Suggestions to fix:\n{suggestions}", None
63
 
64
  # Function to handle voice input (speech-to-text)
65
  def voice_to_text(audio):
@@ -76,8 +41,10 @@ def voice_to_text(audio):
76
  def calculator(audio=None, text_input=None):
77
  if audio:
78
  query = voice_to_text(audio)
 
79
  elif text_input:
80
  query = text_input
 
81
  else:
82
  return "No valid input provided."
83
 
@@ -93,9 +60,9 @@ def start_interface():
93
  gr.Audio(type="filepath", label="Speak a Math Expression (e.g., factorial(5), permutation(5, 3), log(10))"), # Voice input (required)
94
  gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...") # Optional text input
95
  ],
96
- outputs=["text", "audio"],
97
  title="Advanced Math Solver",
98
- description="Solve advanced math problems including permutations, combinations, factorials, powers, and logarithms. Use speech or text input for accessibility.",
99
  live=True
100
  )
101
 
@@ -103,5 +70,3 @@ def start_interface():
103
 
104
  if __name__ == "__main__":
105
  start_interface()
106
-
107
-
 
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
 
7
+ # Function to process math queries using sympy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def process_math(query):
9
  try:
10
  # Clean up the input by removing unnecessary spaces
11
  query = query.replace(" ", "") # Removing extra spaces around operators or parentheses
12
 
 
 
 
13
  # Try to process the query using sympy
14
  expr = sp.sympify(query)
15
 
 
20
  result_text = f"The result is: {result}"
21
  tts = gTTS(result_text, lang="en")
22
  tts.save("response.mp3")
23
+ os.system("start response.mp3" if os.name == "nt" else "afplay response.mp3") # Play the speech output
24
 
25
+ return result_text
 
 
26
  except Exception as e:
27
+ return f"Error: Unable to process the query. Please check the syntax. Error: {e}"
 
 
28
 
29
  # Function to handle voice input (speech-to-text)
30
  def voice_to_text(audio):
 
41
  def calculator(audio=None, text_input=None):
42
  if audio:
43
  query = voice_to_text(audio)
44
+ print(f"Voice Input Detected: {query}") # Debugging: Print detected voice input
45
  elif text_input:
46
  query = text_input
47
+ print(f"Text Input: {query}") # Debugging: Print detected text input
48
  else:
49
  return "No valid input provided."
50
 
 
60
  gr.Audio(type="filepath", label="Speak a Math Expression (e.g., factorial(5), permutation(5, 3), log(10))"), # Voice input (required)
61
  gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...") # Optional text input
62
  ],
63
+ outputs="text",
64
  title="Advanced Math Solver",
65
+ description="Solve advanced math problems including factorials, permutations, combinations, logarithms, and more. Use speech or text input for accessibility.",
66
  live=True
67
  )
68
 
 
70
 
71
  if __name__ == "__main__":
72
  start_interface()