Spaces:
Runtime error
Runtime error
| import os | |
| from flask import Flask, render_template, jsonify, request, redirect, url_for | |
| from flask import session | |
| from flask_session import Session | |
| import logging | |
| from simple_salesforce import Salesforce | |
| app = Flask(__name__) | |
| # Set the secret key to handle sessions securely | |
| app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q") | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO) | |
| # Salesforce connection setup | |
| sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') | |
| def index(): | |
| return render_template("index.html") | |
| # Route to display login page and validate login | |
| def login(): | |
| if request.method == "POST": | |
| # Get the email and mobile number from the form | |
| email = request.form.get("email") | |
| mobile = request.form.get("mobile") | |
| # Validate the presence of email and mobile | |
| if not email or not mobile: | |
| return jsonify({'error': 'Email and mobile number are required.'}), 400 | |
| try: | |
| # Salesforce query to check if the email and mobile exist | |
| query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{mobile}'" | |
| result = sf.query(query) | |
| if result['totalSize'] > 0: | |
| return jsonify({'success': True, 'message': 'User authenticated successfully.'}), 200 | |
| else: | |
| return jsonify({'success': False, 'error': 'Invalid email or mobile number.'}), 400 | |
| except Exception as e: | |
| logging.error(f"Error: {str(e)}") | |
| return jsonify({'error': 'Something went wrong. Please try again later.'}), 500 | |
| else: | |
| return render_template("login.html") | |
| # Route to handle signup | |
| # Route to handle signup | |
| def signup(): | |
| if request.method == "POST": | |
| try: | |
| # Get the name, email, and phone from the request | |
| data = request.json | |
| name = data.get("name") | |
| email = data.get("email") | |
| mobile = data.get("mobile") | |
| # Check if all required fields are provided | |
| if not name or not email or not mobile: | |
| return jsonify({"error": "Name, email, and mobile number are required."}), 400 | |
| # Salesforce query to check if the email already exists | |
| query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" | |
| existing_user = sf.query(query) | |
| if existing_user['totalSize'] > 0: | |
| return jsonify({"error": "Email already exists. Please use a different email."}), 400 | |
| # Create a new record in Salesforce (adjust the object and field names if necessary) | |
| customer = sf.Customer_Login__c.create({ | |
| 'Name': name, | |
| 'Email__c': email, | |
| 'Phone_Number__c': mobile | |
| }) | |
| return jsonify({"success": True, "message": "Signup successful."}), 201 | |
| except Exception as e: | |
| logging.error(f"Error: {str(e)}") | |
| return jsonify({"error": "Something went wrong while processing."}), 500 | |
| else: | |
| # Return signup page for GET requests | |
| return render_template("Signup.html") | |
| # Start Production Server | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860, debug=True) | |
| # Initialize Flask app | |
| app = Flask(__name__) | |
| # Set the secret key to handle sessions securely | |
| app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q") # Replace with a secure key | |
| # Configure the session type | |
| app.config["SESSION_TYPE"] = "filesystem" # Use filesystem for session storage | |
| app.config["SESSION_COOKIE_NAME"] = "my_session" # Optional: Change session cookie name | |
| app.config["SESSION_COOKIE_SECURE"] = True # Ensure cookies are sent over HTTPS | |
| app.config["SESSION_COOKIE_SAMESITE"] = "None" # Allow cross-site cookies | |
| # Initialize the session | |
| Session(app) | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO) | |
| def index(): | |
| # Serve the HTML page for the voice-based login | |
| return render_template("index.html") | |
| def capture_email_and_mobile(): | |
| try: | |
| # Get the voice captured email and mobile number from the request | |
| data = request.json | |
| email = data.get("email") | |
| mobile = data.get("mobile") | |
| # Validate the captured email and mobile number | |
| if not email or not mobile: | |
| return jsonify({"error": "Email or mobile number is missing."}), 400 | |
| # Log the captured data for now (you can replace it with actual processing logic) | |
| logging.info(f"Captured Email: {email}, Mobile: {mobile}") | |
| # For simplicity, we'll assume the capture was successful. | |
| return jsonify({"success": True, "message": "Email and mobile captured successfully."}), 200 | |
| return render_template("https://huggingface.co/spaces/geethareddy/AIvoicemenu") | |
| except Exception as e: | |
| logging.error(f"Error: {str(e)}") | |
| return jsonify({"error": "Something went wrong while processing."}), 500 | |
| # Start Production Server | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860, debug=True) | |