Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
|
2 |
import torch
|
3 |
-
from flask import Flask, render_template, request, jsonify
|
4 |
import json
|
5 |
import os
|
6 |
from transformers import pipeline
|
@@ -13,143 +13,132 @@ from waitress import serve
|
|
13 |
from simple_salesforce import Salesforce
|
14 |
import requests
|
15 |
|
16 |
-
# Initialize Flask app
|
17 |
app = Flask(__name__)
|
18 |
-
app.secret_key = os.urandom(24) # For session handling
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
24 |
-
print("Connected to Salesforce successfully!")
|
25 |
-
except Exception as e:
|
26 |
-
print(f"Failed to connect to Salesforce: {str(e)}")
|
27 |
-
|
28 |
-
# Functions for Salesforce operations
|
29 |
-
def create_salesforce_record(sf, name, email, phone_number):
|
30 |
-
try:
|
31 |
-
customer_login = sf.Customer_Login__c.create({
|
32 |
-
'Name': name,
|
33 |
-
'Email__c': email,
|
34 |
-
'Phone_Number__c': phone_number
|
35 |
-
})
|
36 |
-
return customer_login
|
37 |
-
except Exception as e:
|
38 |
-
raise Exception(f"Failed to create record: {str(e)}")
|
39 |
-
|
40 |
-
def get_menu_items(sf):
|
41 |
-
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
42 |
-
result = sf.query(query)
|
43 |
-
return result['records']
|
44 |
|
45 |
-
def create_salesforce_order(sf, cart_items, total_price, customer_id):
|
46 |
-
try:
|
47 |
-
order = sf.Order__c.create({
|
48 |
-
'Total_Price__c': total_price,
|
49 |
-
'Cart_Items__c': json.dumps(cart_items), # Storing cart items as JSON
|
50 |
-
'Customer__c': customer_id # Linking to the customer record
|
51 |
-
})
|
52 |
-
return order
|
53 |
-
except Exception as e:
|
54 |
-
raise Exception(f"Failed to create order: {str(e)}")
|
55 |
-
|
56 |
-
# Voice-related functions
|
57 |
def generate_audio_prompt(text, filename):
|
58 |
try:
|
59 |
tts = gTTS(text)
|
60 |
tts.save(os.path.join("static", filename))
|
61 |
except gtts.tts.gTTSError as e:
|
62 |
print(f"Error: {e}")
|
63 |
-
|
64 |
-
time.sleep(5) # Wait for 5 seconds before retrying
|
65 |
generate_audio_prompt(text, filename)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
print(
|
81 |
-
|
|
|
82 |
|
83 |
-
# Routes and Views
|
84 |
@app.route("/")
|
85 |
def index():
|
86 |
return render_template("index.html")
|
87 |
|
88 |
-
@app.route("/dashboard"
|
89 |
def dashboard():
|
90 |
-
return render_template("dashboard.html")
|
91 |
|
92 |
-
@app.route("/menu"
|
93 |
-
def
|
94 |
-
|
95 |
-
menu_items = get_menu_items(sf) # Fetch menu items from Salesforce
|
96 |
-
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
97 |
-
return render_template("menu_page.html", menu_items=menu_data)
|
98 |
|
99 |
-
@app.route("/cart"
|
100 |
def cart():
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
name =
|
140 |
-
email =
|
141 |
-
phone_number =
|
142 |
|
143 |
if not name or not email or not phone_number:
|
144 |
return jsonify({'error': 'Missing required fields'}), 400
|
145 |
|
146 |
try:
|
147 |
-
customer_login =
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
150 |
except Exception as e:
|
151 |
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
|
152 |
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
if __name__ == "__main__":
|
155 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
1 |
|
2 |
import torch
|
3 |
+
from flask import Flask, render_template, request, jsonify
|
4 |
import json
|
5 |
import os
|
6 |
from transformers import pipeline
|
|
|
13 |
from simple_salesforce import Salesforce
|
14 |
import requests
|
15 |
|
|
|
16 |
app = Flask(__name__)
|
|
|
17 |
|
18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
+
config = AutoConfig.from_pretrained("openai/whisper-small")
|
20 |
+
config.update({"timeout": 60})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def generate_audio_prompt(text, filename):
|
23 |
try:
|
24 |
tts = gTTS(text)
|
25 |
tts.save(os.path.join("static", filename))
|
26 |
except gtts.tts.gTTSError as e:
|
27 |
print(f"Error: {e}")
|
28 |
+
time.sleep(5)
|
|
|
29 |
generate_audio_prompt(text, filename)
|
30 |
|
31 |
+
prompts = {
|
32 |
+
"welcome": "Welcome to Biryani Hub.",
|
33 |
+
"ask_name": "Tell me your name.",
|
34 |
+
"ask_email": "Please provide your email address.",
|
35 |
+
"thank_you": "Thank you for registration."
|
36 |
+
}
|
37 |
+
|
38 |
+
for key, text in prompts.items():
|
39 |
+
generate_audio_prompt(text, f"{key}.mp3")
|
40 |
|
41 |
+
try:
|
42 |
+
print("Attempting to connect to Salesforce...")
|
43 |
+
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
44 |
+
print("Connected to Salesforce successfully!")
|
45 |
+
except Exception as e:
|
46 |
+
print(f"Failed to connect to Salesforce: {str(e)}")
|
47 |
|
|
|
48 |
@app.route("/")
|
49 |
def index():
|
50 |
return render_template("index.html")
|
51 |
|
52 |
+
@app.route("/dashboard")
|
53 |
def dashboard():
|
54 |
+
return render_template("dashboard.html")
|
55 |
|
56 |
+
@app.route("/menu")
|
57 |
+
def menu():
|
58 |
+
return render_template("menu.html")
|
|
|
|
|
|
|
59 |
|
60 |
+
@app.route("/cart")
|
61 |
def cart():
|
62 |
+
return render_template("cart.html")
|
63 |
+
|
64 |
+
@app.route("/order")
|
65 |
+
def order():
|
66 |
+
return render_template("order.html")
|
67 |
+
|
68 |
+
@app.route("/final")
|
69 |
+
def final():
|
70 |
+
return render_template("final.html")
|
71 |
+
|
72 |
+
@app.route("/submit", methods=["POST"])
|
73 |
+
def submit():
|
74 |
+
data = request.json
|
75 |
+
name = data.get('name')
|
76 |
+
email = data.get('email')
|
77 |
+
phone = data.get('phone')
|
78 |
+
|
79 |
+
if not name or not email or not phone:
|
80 |
+
return jsonify({'error': 'Missing data'}), 400
|
81 |
+
|
82 |
+
try:
|
83 |
+
customer_login = sf.Customer_Login__c.create({
|
84 |
+
'Name': name,
|
85 |
+
'Email__c': email,
|
86 |
+
'Phone_Number__c': phone
|
87 |
+
})
|
88 |
+
|
89 |
+
if customer_login.get('id'):
|
90 |
+
return jsonify({'success': True})
|
91 |
+
else:
|
92 |
+
return jsonify({'error': 'Failed to create record'}), 500
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
return jsonify({'error': str(e)}), 500
|
96 |
+
|
97 |
+
@app.route("/login", methods=["POST"])
|
98 |
+
def login():
|
99 |
+
data = request.json
|
100 |
+
name = data.get('name')
|
101 |
+
email = data.get('email')
|
102 |
+
phone_number = data.get('phone_number')
|
103 |
|
104 |
if not name or not email or not phone_number:
|
105 |
return jsonify({'error': 'Missing required fields'}), 400
|
106 |
|
107 |
try:
|
108 |
+
customer_login = sf.Customer_Login__c.create({
|
109 |
+
'Name': name,
|
110 |
+
'Email__c': email,
|
111 |
+
'Phone_Number__c': phone_number
|
112 |
+
})
|
113 |
+
return jsonify({'success': True, 'id': customer_login['id']}), 200
|
114 |
except Exception as e:
|
115 |
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
|
116 |
|
117 |
+
@app.route("/transcribe", methods=["POST"])
|
118 |
+
def transcribe():
|
119 |
+
if "audio" not in request.files:
|
120 |
+
return jsonify({"error": "No audio file provided"}), 400
|
121 |
+
|
122 |
+
audio_file = request.files["audio"]
|
123 |
+
input_audio_path = os.path.join("static", "temp_input.wav")
|
124 |
+
output_audio_path = os.path.join("static", "temp.wav")
|
125 |
+
audio_file.save(input_audio_path)
|
126 |
+
|
127 |
+
try:
|
128 |
+
audio = AudioSegment.from_file(input_audio_path)
|
129 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
130 |
+
audio.export(output_audio_path, format="wav")
|
131 |
+
|
132 |
+
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
|
133 |
+
if len(nonsilent_parts) == 0:
|
134 |
+
return jsonify({"error": "No speech detected. Please try again."}), 400
|
135 |
+
|
136 |
+
result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
|
137 |
+
transcribed_text = result["text"].strip().capitalize()
|
138 |
+
|
139 |
+
return jsonify({"text": transcribed_text})
|
140 |
+
except Exception as e:
|
141 |
+
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
142 |
+
|
143 |
if __name__ == "__main__":
|
144 |
serve(app, host="0.0.0.0", port=7860)
|