Spaces:
Sleeping
Sleeping
import torch | |
from flask import Flask, render_template, request, jsonify, redirect, url_for | |
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__) | |
# Use whisper-small for faster processing and better speed | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
# Create config object to set timeout and other parameters | |
config = AutoConfig.from_pretrained("openai/whisper-small") | |
config.update({"timeout": 60}) # Set timeout to 60 seconds | |
# Salesforce credentials (Replace with actual values) | |
try: | |
print("Attempting to connect to Salesforce...") | |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') | |
print("Connected to Salesforce successfully!") | |
except Exception as e: | |
print(f"Failed to connect to Salesforce: {str(e)}") | |
# Function for Salesforce operations | |
def create_salesforce_record(name, email, phone_number): | |
try: | |
customer_login = sf.Customer_Login__c.create({ | |
'Name': name, | |
'Email__c': email, | |
'Phone_Number__c': phone_number | |
}) | |
return customer_login | |
except Exception as e: | |
raise Exception(f"Failed to create record: {str(e)}") | |
def get_menu_items(): | |
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c" | |
result = sf.query(query) | |
return result['records'] | |
# Login validation function | |
def validate_user(email, phone): | |
query = f"SELECT Id, Name, Email__c, Phone_Number__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}'" | |
result = sf.query(query) | |
if result['records']: | |
return result['records'][0] # Return user record if found | |
return None # Return None if user not found | |
# Routes and Views | |
def index(): | |
return render_template("index.html") | |
def login(): | |
# Get data from the form | |
email = request.form.get("email") | |
phone = request.form.get("phone") | |
if not email or not phone: | |
return jsonify({"success": False, "message": "Email and phone number are required."}), 400 | |
# Validate user | |
user = validate_user(email, phone) | |
if user: | |
# User found, redirect to menu or dashboard | |
return redirect(url_for("menu_page")) # Assuming login redirects to the menu | |
else: | |
return jsonify({"success": False, "message": "Invalid credentials. Please try again."}), 401 | |
def menu_page(): | |
menu_items = get_menu_items() | |
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items] | |
return render_template("menu_page.html", menu_items=menu_data) | |
def place_order(): | |
item_name = request.json.get('item_name') | |
quantity = request.json.get('quantity') | |
order_data = {"Item__c": item_name, "Quantity__c": quantity} | |
sf.Order__c.create(order_data) | |
return jsonify({"success": True, "message": f"Order for {item_name} placed successfully."}) | |
def cart(): | |
cart_items = [] # Placeholder for cart items | |
return render_template("cart_page.html", cart_items=cart_items) | |
def order_summary(): | |
order_details = [] # Placeholder for order details | |
return render_template("order_summary.html", order_details=order_details) | |
def submit(): | |
try: | |
data = request.get_json() | |
if not data: | |
return jsonify({"success": False, "message": "Invalid or empty JSON received"}), 400 | |
# Get the values from the JSON data | |
name = data.get("name") | |
email = data.get("email") | |
phone = data.get("phone") | |
# Validate if all fields are present | |
if not name or not email or not phone: | |
return jsonify({"success": False, "message": "Missing required fields"}), 400 | |
salesforce_data = { | |
"Name": name, | |
"Email__c": email, | |
"Phone_Number__c": phone | |
} | |
try: | |
result = sf.Customer_Login__c.create(salesforce_data) | |
return jsonify({"success": True, "message": "Data submitted successfully"}) | |
except Exception as e: | |
print(f"Salesforce Insertion Error: {str(e)}") | |
return jsonify({"success": False, "message": "Salesforce submission failed", "error": str(e)}), 500 | |
except Exception as e: | |
print(f"Server Error: {str(e)}") | |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500 | |
# Main function to run the application | |
if __name__ == "__main__": | |
serve(app, host="0.0.0.0", port=7860) | |