geethareddy commited on
Commit
e07e62c
·
verified ·
1 Parent(s): 63d823b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for, jsonify
2
+ from simple_salesforce import Salesforce
3
+ import os
4
+ from gtts import gTTS
5
+ import time
6
+ from transformers import AutoConfig, pipeline
7
+ from werkzeug.exceptions import BadRequest
8
+ from pydub import AudioSegment
9
+ from pydub.silence import detect_nonsilent
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Salesforce connection setup
14
+ sf = Salesforce(
15
+ username='diggavalli98@gmail.com',
16
+ password='Sati@1020',
17
+ security_token='sSSjyhInIsUohKpG8sHzty2q',
18
+ consumer_key='3MVG9WVXk15qiz1JbtW1tT9a7Wnkos2RuGamw6p1lC5uPescT5NB2nPygpo6rQ87K1T.zBEn.wR.A6JdgHnIU',
19
+ consumer_secret='A75C6B7801D5D20BED0E46631CF58C4F7FF28E4DAF442FE667553D29C35C0451'
20
+ )
21
+
22
+ # Global variables
23
+ cart = [] # To store items, quantities, and prices
24
+ MENU = {}
25
+
26
+ # Fetching menu items dynamically from Salesforce
27
+ def get_menu_items():
28
+ menu_items = {}
29
+ query = "SELECT Name, Category__c, Price__c, Ingredients__c FROM Menu_Item__c"
30
+ results = sf.query_all(query)
31
+ for record in results['records']:
32
+ category = record['Category__c']
33
+ item_name = record['Name']
34
+ price = record['Price__c']
35
+ if category not in menu_items:
36
+ menu_items[category] = {}
37
+ menu_items[category][item_name] = price
38
+ return menu_items
39
+
40
+ # Fetch menu on app load
41
+ MENU = get_menu_items()
42
+
43
+ # Index route for login/signup page
44
+ @app.route("/", methods=["GET", "POST"])
45
+ def index():
46
+ if request.method == "POST":
47
+ name = request.form["name"]
48
+ email = request.form["email"]
49
+ phone = request.form["phone"]
50
+
51
+ if not name or not email or not phone:
52
+ return jsonify({"error": "All fields are required"}), 400
53
+
54
+ # Create Salesforce record for new customer
55
+ try:
56
+ customer_login = sf.Customer_Login__c.create({
57
+ 'Name': name,
58
+ 'Email__c': email,
59
+ 'Phone_Number__c': phone
60
+ })
61
+ return redirect(url_for('menu')) # Redirect to the menu page after successful login
62
+ except Exception as e:
63
+ return jsonify({"error": f"Failed to create record: {str(e)}"}), 500
64
+ return render_template("login.html") # Render login page
65
+
66
+ # Menu route to show menu page
67
+ @app.route("/menu", methods=["GET"])
68
+ def menu():
69
+ return render_template("menu.html", menu=MENU)
70
+
71
+ # Function to extract quantity from a speech command
72
+ def extract_quantity(command):
73
+ number_words = {
74
+ "one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
75
+ "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10,
76
+ "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10
77
+ }
78
+
79
+ command_words = command.split()
80
+ for word in command_words:
81
+ if word in number_words:
82
+ return number_words[word]
83
+ return None
84
+
85
+ # Audio processing function to convert and check audio
86
+ def convert_to_wav(input_path, output_path):
87
+ try:
88
+ audio = AudioSegment.from_file(input_path)
89
+ audio = audio.set_frame_rate(16000).set_channels(1)
90
+ audio.export(output_path, format="wav")
91
+ except Exception as e:
92
+ print(f"Error: {str(e)}")
93
+ raise Exception(f"Audio conversion failed: {str(e)}")
94
+
95
+ # Audio transcription function
96
+ @app.route("/transcribe", methods=["POST"])
97
+ def transcribe():
98
+ if "audio" not in request.files:
99
+ return jsonify({"error": "No audio file provided"}), 400
100
+
101
+ audio_file = request.files["audio"]
102
+ input_audio_path = os.path.join("static", "temp_input.wav")
103
+ output_audio_path = os.path.join("static", "temp.wav")
104
+ audio_file.save(input_audio_path)
105
+
106
+ try:
107
+ convert_to_wav(input_audio_path, output_audio_path)
108
+ audio = AudioSegment.from_wav(output_audio_path)
109
+ nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
110
+ if len(nonsilent_parts) == 0:
111
+ return jsonify({"error": "No speech detected. Please try again."}), 400
112
+
113
+ result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1)
114
+ transcribed_text = result["text"].strip().capitalize()
115
+
116
+ parts = transcribed_text.split()
117
+ name = parts[0] if len(parts) > 0 else "Unknown Name"
118
+ email = parts[1] if '@' in parts[1] else "unknown@domain.com"
119
+ phone_number = parts[2] if len(parts) > 2 else "0000000000"
120
+
121
+ confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
122
+ return jsonify({"text": confirmation})
123
+
124
+ except Exception as e:
125
+ return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
126
+
127
+ if __name__ == "__main__":
128
+ app.run(host="0.0.0.0", port=7860)