Spaces:
Runtime error
Runtime error
| import speech_recognition as sr | |
| import whisper | |
| from simple_salesforce import Salesforce | |
| from flask import Flask, jsonify | |
| app = Flask(__name__) | |
| # Initialize Whisper Model | |
| model = whisper.load_model("base") | |
| # Salesforce Authentication | |
| sf = Salesforce( | |
| username="diggavalli98@gmail.com", | |
| password="Sati@1020", | |
| security_token="sSSjyhInIsUohKpG8sHzty2q" | |
| ) | |
| def print_text(text): | |
| """Debugging function to replace TTS output""" | |
| print(text) | |
| def listen(): | |
| """Capture live voice and return text""" | |
| recognizer = sr.Recognizer() | |
| with sr.Microphone() as source: | |
| print_text("Listening...") | |
| recognizer.adjust_for_ambient_noise(source) # Adjust for background noise | |
| audio = recognizer.listen(source) | |
| try: | |
| # Convert speech to text using Whisper | |
| file_path = "temp.wav" | |
| with open(file_path, "wb") as f: | |
| f.write(audio.get_wav_data()) | |
| result = model.transcribe(file_path) | |
| return result["text"].strip() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def extract_name(text): | |
| """Extract name dynamically from sentence (e.g., 'My name is John' -> 'John')""" | |
| words = text.split() | |
| if "name" in words: | |
| idx = words.index("name") | |
| if idx + 1 < len(words): | |
| return words[idx + 1] # Assume name follows "name is" | |
| return words[-1] # If format is unknown, use last word | |
| def extract_phone(text): | |
| """Extract phone number dynamically from sentence""" | |
| words = text.split() | |
| for word in words: | |
| if word.isdigit() and len(word) >= 7: # Phone numbers are usually >=7 digits | |
| return word | |
| return None | |
| def check_salesforce(name, phone): | |
| """Check if the given name and phone exist in Salesforce""" | |
| query = f"SELECT Id FROM Traveller__c WHERE Name = '{name}' AND Phone__c = '{phone}'" | |
| result = sf.query(query) | |
| return result["totalSize"] > 0 # True if record exists | |
| def voice_login(): | |
| """Main voice authentication process""" | |
| # Step 1: Ask Name | |
| print_text("What is your name?") | |
| name_response = listen() | |
| print_text(f"User said: {name_response}") | |
| name = extract_name(name_response) | |
| if not name: | |
| return jsonify({"error": "Name not recognized"}), 400 | |
| print_text(f"Hello {name}, please say your phone number.") | |
| # Step 2: Ask Phone Number | |
| phone_response = listen() | |
| print_text(f"User said: {phone_response}") | |
| phone = extract_phone(phone_response) | |
| if not phone: | |
| return jsonify({"error": "Phone number not recognized"}), 400 | |
| # Step 3: Authenticate in Salesforce | |
| if check_salesforce(name, phone): | |
| print_text("Login successful! Welcome.") | |
| return jsonify({"message": "Login Successful"}) | |
| print_text("Login failed. Invalid credentials.") | |
| return jsonify({"error": "Invalid Credentials"}), 401 | |
| if __name__ == "__main__": | |
| app.run(debug=True) | |