nailarais1 commited on
Commit
491e7b3
·
verified ·
1 Parent(s): fba424d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sympy as sp
2
+ import gradio as gr
3
+ import speech_recognition as sr
4
+ from transformers import pipeline
5
+ from gtts import gTTS
6
+ import os
7
+ import mediapipe as mp
8
+ import cv2
9
+ import numpy as np
10
+
11
+ # Load the ASR model
12
+ asr_pipeline = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
13
+
14
+ # Mediapipe setup for hand tracking (sign language recognition)
15
+ mp_hands = mp.solutions.hands
16
+ hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5)
17
+ mp_drawing = mp.solutions.drawing_utils
18
+
19
+ # Sign language processing function
20
+ def process_sign_language(image):
21
+ """
22
+ Process a hand gesture image for basic recognition.
23
+ """
24
+ try:
25
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
+ results = hands.process(image_rgb)
27
+
28
+ if not results.multi_hand_landmarks:
29
+ return "No hand detected."
30
+
31
+ landmarks = results.multi_hand_landmarks[0]
32
+ coords = [(lm.x, lm.y) for lm in landmarks.landmark]
33
+
34
+ # A placeholder example: Gesture detection logic
35
+ thumb_tip = coords[4]
36
+ index_tip = coords[8]
37
+ distance = np.linalg.norm(np.array(thumb_tip) - np.array(index_tip))
38
+
39
+ # Simple threshold-based recognition (customize as needed)
40
+ if distance < 0.05:
41
+ return "Gesture: 'Thumb and index touching' detected (e.g., OK gesture)."
42
+ else:
43
+ return "Gesture detected, but not recognized."
44
+
45
+ except Exception as e:
46
+ return f"Error in sign language processing: {e}"
47
+
48
+ # Function to process calculus queries
49
+ def process_calculus(query):
50
+ try:
51
+ expr = sp.sympify(query)
52
+ derivative = sp.diff(expr)
53
+ integral = sp.integrate(expr)
54
+ return f"Derivative: {derivative}\nIntegral: {integral}"
55
+ except Exception as e:
56
+ return f"Error: Unable to process the query. Details: {e}"
57
+
58
+ # Voice-to-text processing
59
+ def voice_to_text(audio_file):
60
+ try:
61
+ transcription = asr_pipeline(audio_file)["text"]
62
+ return transcription
63
+ except Exception as e:
64
+ return f"Error in voice recognition: {e}"
65
+
66
+ # Text-to-speech function
67
+ def text_to_speech(response):
68
+ try:
69
+ tts = gTTS(response)
70
+ tts.save("response.mp3")
71
+ os.system("start response.mp3" if os.name == "nt" else "afplay response.mp3")
72
+ except Exception as e:
73
+ return f"Error in generating speech: {e}"
74
+
75
+ # Main calculator function
76
+ def calculator(input_audio=None, input_text=None, input_image=None):
77
+ if input_audio is not None:
78
+ query = voice_to_text(input_audio)
79
+ elif input_image is not None:
80
+ query = process_sign_language(input_image)
81
+ elif input_text:
82
+ query = input_text
83
+ else:
84
+ return "No valid input provided."
85
+
86
+ response = process_calculus(query)
87
+ text_to_speech(response)
88
+ return response
89
+
90
+ # Gradio interface setup
91
+ interface = gr.Interface(
92
+ fn=calculator,
93
+ inputs=[
94
+ gr.Audio(source="microphone", optional=True, label="Voice Input"),
95
+ gr.Textbox(label="Text Input (Math Expression)", optional=True),
96
+ gr.Image(source="webcam", optional=True, label="Sign Language Input"),
97
+ ],
98
+ outputs="text",
99
+ title="Accessible Calculus Solver",
100
+ description="Solve calculus problems using voice, text, or sign language input. Designed for accessibility.",
101
+ )
102
+
103
+ if __name__ == "__main__":
104
+ interface.launch()