nailarais1 commited on
Commit
695d82a
·
verified ·
1 Parent(s): d70a172

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -4,25 +4,22 @@ import speech_recognition as sr
4
  from gtts import gTTS
5
  import os
6
 
7
- # Function to process calculus queries
8
- def process_calculus(query):
9
  try:
10
- # Convert the query into a symbolic expression
11
  expr = sp.sympify(query)
12
 
13
- # Calculate derivative and integral
14
- derivative = sp.diff(expr)
15
- integral = sp.integrate(expr)
16
-
17
- # Format the results as text
18
- result = f"Derivative: {derivative}\nIntegral: {integral}"
19
 
20
  # Convert the result to speech and save as mp3
21
- tts = gTTS(result, 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
26
  except Exception as e:
27
  return f"Error: Unable to process the query. Details: {e}"
28
 
@@ -37,18 +34,30 @@ def voice_to_text(audio):
37
  except Exception as e:
38
  return f"Error in voice recognition: {e}"
39
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Gradio interface setup
41
  def start_interface():
42
  # Gradio Interface with Voice Input, Text Input, and Output
43
  interface = gr.Interface(
44
- fn=process_calculus,
45
  inputs=[
46
- gr.Audio(type="filepath", label="Speak a Math Expression (e.g., x**2 + 3*x + 5)"), # Voice input (required)
47
  gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...") # Optional text input
48
  ],
49
  outputs="text",
50
- title="Accessible Calculus Solver",
51
- description="Solve calculus problems using speech or text. The result will be read aloud for accessibility.",
52
  live=True
53
  )
54
 
 
4
  from gtts import gTTS
5
  import os
6
 
7
+ # Function to process general math queries (basic and advanced)
8
+ def process_math(query):
9
  try:
10
+ # Try to parse the input query into a mathematical expression
11
  expr = sp.sympify(query)
12
 
13
+ # Evaluate the expression
14
+ result = expr.evalf()
 
 
 
 
15
 
16
  # Convert the result to speech and save as mp3
17
+ result_text = f"The result is: {result}"
18
+ tts = gTTS(result_text, lang="en")
19
  tts.save("response.mp3")
20
  os.system("start response.mp3" if os.name == "nt" else "afplay response.mp3") # Play the speech output
21
 
22
+ return result_text
23
  except Exception as e:
24
  return f"Error: Unable to process the query. Details: {e}"
25
 
 
34
  except Exception as e:
35
  return f"Error in voice recognition: {e}"
36
 
37
+ # Main function to handle input and process it for math calculation (text or voice)
38
+ def calculator(audio=None, text_input=None):
39
+ if audio:
40
+ query = voice_to_text(audio)
41
+ elif text_input:
42
+ query = text_input
43
+ else:
44
+ return "No valid input provided."
45
+
46
+ # Process the math problem
47
+ return process_math(query)
48
+
49
  # Gradio interface setup
50
  def start_interface():
51
  # Gradio Interface with Voice Input, Text Input, and Output
52
  interface = gr.Interface(
53
+ fn=calculator,
54
  inputs=[
55
+ gr.Audio(type="filepath", label="Speak a Math Expression (e.g., factorial(5), permutation(5, 3), log(10))"), # Voice input (required)
56
  gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...") # Optional text input
57
  ],
58
  outputs="text",
59
+ title="Advanced Math Solver",
60
+ description="Solve advanced math problems including permutations, combinations, factorials, powers, and logarithms. Use speech or text input for accessibility.",
61
  live=True
62
  )
63