voicemenu143 / app.py
lokesh341's picture
Update app.py
96f1a3c verified
raw
history blame
2.76 kB
import torch
from flask import Flask, render_template, request, jsonify
import json
import os
from transformers import pipeline
from gtts import gTTS
from pydub import AudioSegment
from pydub.silence import detect_nonsilent
from transformers import AutoConfig
import time
from waitress import serve
from simple_salesforce import Salesforce
import requests
app = Flask(__name__)
# Check GPU availability
device = "cuda" if torch.cuda.is_available() else "cpu"
# Whisper ASR config
config = AutoConfig.from_pretrained("openai/whisper-small")
config.update({"timeout": 60})
# Salesforce connection
try:
print("Connecting to Salesforce...")
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
print("Connected successfully!")
except Exception as e:
print(f"Salesforce connection failed: {str(e)}")
# API Routes
@app.route('/login', methods=['POST'])
def login():
data = request.json
email = data.get('email')
phone_number = data.get('phone_number')
if not email or not phone_number:
return jsonify({'error': 'Missing required fields'}), 400
try:
existing_customer = sf.query(f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone_number}'")
if existing_customer['totalSize'] > 0:
return jsonify({'success': True, 'message': 'Login successful!', 'customer': existing_customer['records'][0]})
else:
return jsonify({'error': 'No user found. Please register first.'}), 404
except Exception as e:
return jsonify({'error': f'Failed to fetch user from Salesforce: {str(e)}'}), 500
@app.route("/submit", methods=["POST"])
def submit():
data = request.json
name = data.get('name')
email = data.get('email')
phone = data.get('phone')
if not name or not email or not phone:
return jsonify({'error': 'Missing data'}), 400
try:
existing_customer = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}'")
if existing_customer['totalSize'] > 0:
return jsonify({'success': False, 'message': 'User already registered. Please log in.'}), 409
customer_login = sf.Customer_Login__c.create({
'Name': name,
'Email__c': email,
'Phone_Number__c': phone
})
return jsonify({'success': True, 'message': 'Registration successful!', 'id': customer_login['id']}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route("/")
def index():
return render_template("index.html")
# Start server
if __name__ == "__main__":
serve(app, host="0.0.0.0", port=7860)