Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
import torch
|
2 |
-
from flask import Flask, render_template, request, jsonify
|
3 |
import json
|
4 |
import os
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
7 |
from pydub import AudioSegment
|
8 |
from pydub.silence import detect_nonsilent
|
9 |
-
from transformers import AutoConfig
|
10 |
import time
|
11 |
from waitress import serve
|
12 |
from simple_salesforce import Salesforce
|
13 |
-
import requests
|
14 |
|
15 |
app = Flask(__name__)
|
16 |
|
@@ -32,15 +32,12 @@ except Exception as e:
|
|
32 |
# Function for Salesforce operations
|
33 |
def create_salesforce_record(name, email, phone_number):
|
34 |
try:
|
35 |
-
# Create a new record in Salesforce's Customer_Login__c object
|
36 |
customer_login = sf.Customer_Login__c.create({
|
37 |
'Name': name,
|
38 |
'Email__c': email,
|
39 |
'Phone_Number__c': phone_number
|
40 |
})
|
41 |
return customer_login
|
42 |
-
except SalesforceResourceNotFound as e:
|
43 |
-
raise Exception(f"Salesforce resource not found: {str(e)}")
|
44 |
except Exception as e:
|
45 |
raise Exception(f"Failed to create record: {str(e)}")
|
46 |
|
@@ -49,41 +46,35 @@ def get_menu_items():
|
|
49 |
result = sf.query(query)
|
50 |
return result['records']
|
51 |
|
52 |
-
#
|
53 |
-
def
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
print("Retrying after 5 seconds...")
|
60 |
-
time.sleep(5) # Wait for 5 seconds before retrying
|
61 |
-
generate_audio_prompt(text, filename)
|
62 |
-
|
63 |
-
# Utility functions
|
64 |
-
def convert_to_wav(input_path, output_path):
|
65 |
-
try:
|
66 |
-
audio = AudioSegment.from_file(input_path)
|
67 |
-
audio = audio.set_frame_rate(16000).set_channels(1) # Convert to 16kHz, mono
|
68 |
-
audio.export(output_path, format="wav")
|
69 |
-
except Exception as e:
|
70 |
-
print(f"Error: {str(e)}")
|
71 |
-
raise Exception(f"Audio conversion failed: {str(e)}")
|
72 |
-
|
73 |
-
def is_silent_audio(audio_path):
|
74 |
-
audio = AudioSegment.from_wav(audio_path)
|
75 |
-
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) # Reduced silence duration
|
76 |
-
print(f"Detected nonsilent parts: {nonsilent_parts}")
|
77 |
-
return len(nonsilent_parts) == 0 # If no speech detected
|
78 |
|
79 |
# Routes and Views
|
80 |
@app.route("/")
|
81 |
def index():
|
82 |
return render_template("index.html")
|
83 |
|
84 |
-
@app.route("/
|
85 |
-
def
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
@app.route("/menu", methods=["GET"])
|
89 |
def menu_page():
|
@@ -91,6 +82,14 @@ def menu_page():
|
|
91 |
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
92 |
return render_template("menu_page.html", menu_items=menu_data)
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
@app.route("/cart", methods=["GET"])
|
95 |
def cart():
|
96 |
cart_items = [] # Placeholder for cart items
|
@@ -101,67 +100,39 @@ def order_summary():
|
|
101 |
order_details = [] # Placeholder for order details
|
102 |
return render_template("order_summary.html", order_details=order_details)
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
def validate_login():
|
107 |
try:
|
108 |
data = request.get_json()
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
#
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
except Exception as e:
|
|
|
127 |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500
|
128 |
|
129 |
-
#
|
130 |
-
@app.route("/place-order", methods=["POST"])
|
131 |
-
def place_order():
|
132 |
-
# Automatically use login email and phone number from validated user
|
133 |
-
login_email = "[email protected]" # Replace with actual login email from Salesforce
|
134 |
-
login_phone = "1234567890" # Replace with actual login phone from Salesforce
|
135 |
-
|
136 |
-
order_data = request.json
|
137 |
-
total_amount = sum([item['price'] * item['quantity'] for item in order_data['items']])
|
138 |
-
|
139 |
-
# Prepare order data for Salesforce submission
|
140 |
-
salesforce_order_data = {
|
141 |
-
"Customer_Email__c": login_email,
|
142 |
-
"Customer_Phone__c": login_phone,
|
143 |
-
"Total_Amount__c": total_amount
|
144 |
-
}
|
145 |
-
|
146 |
-
# Create the order in Salesforce
|
147 |
-
try:
|
148 |
-
order = sf.Order__c.create(salesforce_order_data)
|
149 |
-
|
150 |
-
# Create order items in Salesforce
|
151 |
-
for item in order_data['items']:
|
152 |
-
sf.Order_Item__c.create({
|
153 |
-
"Order__c": order['id'], # Link to the parent order
|
154 |
-
"Item__c": item['name'],
|
155 |
-
"Quantity__c": item['quantity'],
|
156 |
-
"Price__c": item['price']
|
157 |
-
})
|
158 |
-
|
159 |
-
return jsonify({"success": True, "message": "Order placed successfully.", "orderId": order['id']})
|
160 |
-
|
161 |
-
except Exception as e:
|
162 |
-
print(f"Error placing order: {str(e)}")
|
163 |
-
return jsonify({"success": False, "message": f"Error placing order: {str(e)}"}), 500
|
164 |
-
|
165 |
-
# Start Production Server
|
166 |
if __name__ == "__main__":
|
167 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
1 |
import torch
|
2 |
+
from flask import Flask, render_template, request, jsonify, redirect, url_for
|
3 |
import json
|
4 |
import os
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
7 |
from pydub import AudioSegment
|
8 |
from pydub.silence import detect_nonsilent
|
9 |
+
from transformers import AutoConfig
|
10 |
import time
|
11 |
from waitress import serve
|
12 |
from simple_salesforce import Salesforce
|
13 |
+
import requests
|
14 |
|
15 |
app = Flask(__name__)
|
16 |
|
|
|
32 |
# Function for Salesforce operations
|
33 |
def create_salesforce_record(name, email, phone_number):
|
34 |
try:
|
|
|
35 |
customer_login = sf.Customer_Login__c.create({
|
36 |
'Name': name,
|
37 |
'Email__c': email,
|
38 |
'Phone_Number__c': phone_number
|
39 |
})
|
40 |
return customer_login
|
|
|
|
|
41 |
except Exception as e:
|
42 |
raise Exception(f"Failed to create record: {str(e)}")
|
43 |
|
|
|
46 |
result = sf.query(query)
|
47 |
return result['records']
|
48 |
|
49 |
+
# Login validation function
|
50 |
+
def validate_user(email, phone):
|
51 |
+
query = f"SELECT Id, Name, Email__c, Phone_Number__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}'"
|
52 |
+
result = sf.query(query)
|
53 |
+
if result['records']:
|
54 |
+
return result['records'][0] # Return user record if found
|
55 |
+
return None # Return None if user not found
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Routes and Views
|
58 |
@app.route("/")
|
59 |
def index():
|
60 |
return render_template("index.html")
|
61 |
|
62 |
+
@app.route("/login", methods=["POST"])
|
63 |
+
def login():
|
64 |
+
# Get data from the form
|
65 |
+
email = request.form.get("email")
|
66 |
+
phone = request.form.get("phone")
|
67 |
+
|
68 |
+
if not email or not phone:
|
69 |
+
return jsonify({"success": False, "message": "Email and phone number are required."}), 400
|
70 |
+
|
71 |
+
# Validate user
|
72 |
+
user = validate_user(email, phone)
|
73 |
+
if user:
|
74 |
+
# User found, redirect to menu or dashboard
|
75 |
+
return redirect(url_for("menu_page")) # Assuming login redirects to the menu
|
76 |
+
else:
|
77 |
+
return jsonify({"success": False, "message": "Invalid credentials. Please try again."}), 401
|
78 |
|
79 |
@app.route("/menu", methods=["GET"])
|
80 |
def menu_page():
|
|
|
82 |
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
83 |
return render_template("menu_page.html", menu_items=menu_data)
|
84 |
|
85 |
+
@app.route("/order", methods=["POST"])
|
86 |
+
def place_order():
|
87 |
+
item_name = request.json.get('item_name')
|
88 |
+
quantity = request.json.get('quantity')
|
89 |
+
order_data = {"Item__c": item_name, "Quantity__c": quantity}
|
90 |
+
sf.Order__c.create(order_data)
|
91 |
+
return jsonify({"success": True, "message": f"Order for {item_name} placed successfully."})
|
92 |
+
|
93 |
@app.route("/cart", methods=["GET"])
|
94 |
def cart():
|
95 |
cart_items = [] # Placeholder for cart items
|
|
|
100 |
order_details = [] # Placeholder for order details
|
101 |
return render_template("order_summary.html", order_details=order_details)
|
102 |
|
103 |
+
@app.route('/submit', methods=['POST'])
|
104 |
+
def submit():
|
|
|
105 |
try:
|
106 |
data = request.get_json()
|
107 |
+
if not data:
|
108 |
+
return jsonify({"success": False, "message": "Invalid or empty JSON received"}), 400
|
109 |
+
|
110 |
+
# Get the values from the JSON data
|
111 |
+
name = data.get("name")
|
112 |
+
email = data.get("email")
|
113 |
+
phone = data.get("phone")
|
114 |
+
|
115 |
+
# Validate if all fields are present
|
116 |
+
if not name or not email or not phone:
|
117 |
+
return jsonify({"success": False, "message": "Missing required fields"}), 400
|
118 |
+
|
119 |
+
salesforce_data = {
|
120 |
+
"Name": name,
|
121 |
+
"Email__c": email,
|
122 |
+
"Phone_Number__c": phone
|
123 |
+
}
|
124 |
+
|
125 |
+
try:
|
126 |
+
result = sf.Customer_Login__c.create(salesforce_data)
|
127 |
+
return jsonify({"success": True, "message": "Data submitted successfully"})
|
128 |
+
except Exception as e:
|
129 |
+
print(f"Salesforce Insertion Error: {str(e)}")
|
130 |
+
return jsonify({"success": False, "message": "Salesforce submission failed", "error": str(e)}), 500
|
131 |
|
132 |
except Exception as e:
|
133 |
+
print(f"Server Error: {str(e)}")
|
134 |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500
|
135 |
|
136 |
+
# Main function to run the application
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
if __name__ == "__main__":
|
138 |
serve(app, host="0.0.0.0", port=7860)
|