DSatishchandra commited on
Commit
6d40dd0
·
verified ·
1 Parent(s): 837f789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -56
app.py CHANGED
@@ -1,27 +1,43 @@
 
 
 
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
 
17
- # Use whisper-small for faster processing and better speed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
 
20
- # Create config object to set timeout and other parameters
21
  config = AutoConfig.from_pretrained("openai/whisper-small")
22
  config.update({"timeout": 60})
23
 
24
- # Generate required voice prompts
25
  prompts = {
26
  "welcome": "Welcome to Biryani Hub.",
27
  "ask_name": "Tell me your name.",
@@ -35,11 +51,12 @@ def generate_audio_prompt(text, filename):
35
  tts = gTTS(text)
36
  tts.save(os.path.join("static", filename))
37
  except gtts.tts.gTTSError as e:
38
- print(f"Error: {e}")
39
- print("Retrying after 5 seconds...")
40
  time.sleep(5)
41
  generate_audio_prompt(text, filename)
42
 
 
43
  for key, text in prompts.items():
44
  generate_audio_prompt(text, f"{key}.mp3")
45
 
@@ -65,7 +82,7 @@ def convert_to_wav(input_path, output_path):
65
  except Exception as e:
66
  raise Exception(f"Audio conversion failed: {str(e)}")
67
 
68
- # Function to check if audio contains actual speech
69
  def is_silent_audio(audio_path):
70
  audio = AudioSegment.from_wav(audio_path)
71
  nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
@@ -73,22 +90,20 @@ def is_silent_audio(audio_path):
73
 
74
  # Salesforce connection details
75
  try:
76
- print("Attempting to connect to Salesforce...")
77
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
78
- print("Connected to Salesforce successfully!")
79
  except Exception as e:
80
- print(f"Failed to connect to Salesforce: {str(e)}")
81
 
82
- # Function to handle login & registration in Salesforce
83
  @app.route("/validate_login", methods=["POST"])
84
  def validate_login():
85
  try:
86
- # Get the email and mobile number from the request
87
  data = request.json
88
  email = data.get("email")
89
  mobile = data.get("mobile")
90
 
91
- # Salesforce query to check if the email and mobile exist
92
  query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{mobile}'"
93
  result = sf.query(query)
94
 
@@ -97,64 +112,33 @@ def validate_login():
97
  else:
98
  return jsonify({'success': False, 'error': 'Invalid email or mobile number.'}), 400
99
  except Exception as e:
100
- logging.error(f"Error: {str(e)}")
101
  return jsonify({'error': 'Something went wrong. Please try again later.'}), 500
102
 
103
- if __name__ == "__main__":
104
- app.run(host="0.0.0.0", port=7860, debug=True)
105
-
106
-
107
-
108
- # Initialize Flask app
109
- app = Flask(__name__)
110
-
111
- # Set the secret key to handle sessions securely
112
- app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q") # Replace with a secure key
113
-
114
- # Configure the session type
115
- app.config["SESSION_TYPE"] = "filesystem" # Use filesystem for session storage
116
- app.config["SESSION_COOKIE_NAME"] = "my_session" # Optional: Change session cookie name
117
- app.config["SESSION_COOKIE_SECURE"] = True # Ensure cookies are sent over HTTPS
118
- app.config["SESSION_COOKIE_SAMESITE"] = "None" # Allow cross-site cookies
119
-
120
- # Initialize the session
121
- Session(app)
122
-
123
- # Set up logging
124
- logging.basicConfig(level=logging.INFO)
125
-
126
  @app.route("/")
127
  def index():
128
- # Serve the HTML page for the voice-based login
129
  return render_template("index.html")
130
 
 
131
  @app.route("/capture_email_and_mobile", methods=["POST"])
132
  def capture_email_and_mobile():
133
  try:
134
- # Get the voice captured email and mobile number from the request
135
  data = request.json
136
  email = data.get("email")
137
  mobile = data.get("mobile")
138
 
139
- # Validate the captured email and mobile number
140
  if not email or not mobile:
141
  return jsonify({"error": "Email or mobile number is missing."}), 400
142
 
143
- # Log the captured data for now (you can replace it with actual processing logic)
144
  logging.info(f"Captured Email: {email}, Mobile: {mobile}")
145
-
146
- # For simplicity, we'll assume the capture was successful.
147
  return jsonify({"success": True, "message": "Email and mobile captured successfully."}), 200
148
 
149
  except Exception as e:
150
- logging.error(f"Error: {str(e)}")
151
  return jsonify({"error": "Something went wrong while processing."}), 500
152
 
153
-
154
- @app.route("/")
155
- def index():
156
- return render_template("index.html")
157
-
158
  @app.route("/transcribe", methods=["POST"])
159
  def transcribe():
160
  if "audio" not in request.files:
@@ -182,6 +166,6 @@ def transcribe():
182
  except Exception as e:
183
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
184
 
185
- # Start Production Server
186
  if __name__ == "__main__":
187
- serve(app, host="0.0.0.0", port=7860)
 
1
+ import os
2
+ import logging
3
+ import time
4
  import torch
5
  from flask import Flask, render_template, request, jsonify
6
+ from transformers import pipeline, AutoConfig
 
 
7
  from gtts import gTTS
8
  from pydub import AudioSegment
9
  from pydub.silence import detect_nonsilent
 
 
 
10
  from simple_salesforce import Salesforce
11
+ from waitress import serve
12
  import requests
13
 
14
+ # Initialize Flask app
15
  app = Flask(__name__)
16
 
17
+ # Set the secret key to handle sessions securely
18
+ app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q")
19
+
20
+ # Configure the session type
21
+ app.config["SESSION_TYPE"] = "filesystem"
22
+ app.config["SESSION_COOKIE_NAME"] = "my_session"
23
+ app.config["SESSION_COOKIE_SECURE"] = True
24
+ app.config["SESSION_COOKIE_SAMESITE"] = "None"
25
+
26
+ # Initialize the session (Make sure to import Session at the top of your file)
27
+ from flask_session import Session
28
+ Session(app)
29
+
30
+ # Set up logging
31
+ logging.basicConfig(level=logging.INFO)
32
+
33
+ # Set device for torch
34
  device = "cuda" if torch.cuda.is_available() else "cpu"
35
 
36
+ # Create config object for Whisper model
37
  config = AutoConfig.from_pretrained("openai/whisper-small")
38
  config.update({"timeout": 60})
39
 
40
+ # Create voice prompts
41
  prompts = {
42
  "welcome": "Welcome to Biryani Hub.",
43
  "ask_name": "Tell me your name.",
 
51
  tts = gTTS(text)
52
  tts.save(os.path.join("static", filename))
53
  except gtts.tts.gTTSError as e:
54
+ logging.error(f"Error generating audio: {e}")
55
+ logging.info("Retrying after 5 seconds...")
56
  time.sleep(5)
57
  generate_audio_prompt(text, filename)
58
 
59
+ # Generate all prompts
60
  for key, text in prompts.items():
61
  generate_audio_prompt(text, f"{key}.mp3")
62
 
 
82
  except Exception as e:
83
  raise Exception(f"Audio conversion failed: {str(e)}")
84
 
85
+ # Function to check if audio contains speech
86
  def is_silent_audio(audio_path):
87
  audio = AudioSegment.from_wav(audio_path)
88
  nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
 
90
 
91
  # Salesforce connection details
92
  try:
93
+ logging.info("Attempting to connect to Salesforce...")
94
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
95
+ logging.info("Connected to Salesforce successfully!")
96
  except Exception as e:
97
+ logging.error(f"Failed to connect to Salesforce: {str(e)}")
98
 
99
+ # Route to validate user login
100
  @app.route("/validate_login", methods=["POST"])
101
  def validate_login():
102
  try:
 
103
  data = request.json
104
  email = data.get("email")
105
  mobile = data.get("mobile")
106
 
 
107
  query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{mobile}'"
108
  result = sf.query(query)
109
 
 
112
  else:
113
  return jsonify({'success': False, 'error': 'Invalid email or mobile number.'}), 400
114
  except Exception as e:
115
+ logging.error(f"Error during login validation: {str(e)}")
116
  return jsonify({'error': 'Something went wrong. Please try again later.'}), 500
117
 
118
+ # Route to serve the home page for voice-based login
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  @app.route("/")
120
  def index():
 
121
  return render_template("index.html")
122
 
123
+ # Route to capture email and mobile from voice
124
  @app.route("/capture_email_and_mobile", methods=["POST"])
125
  def capture_email_and_mobile():
126
  try:
 
127
  data = request.json
128
  email = data.get("email")
129
  mobile = data.get("mobile")
130
 
 
131
  if not email or not mobile:
132
  return jsonify({"error": "Email or mobile number is missing."}), 400
133
 
 
134
  logging.info(f"Captured Email: {email}, Mobile: {mobile}")
 
 
135
  return jsonify({"success": True, "message": "Email and mobile captured successfully."}), 200
136
 
137
  except Exception as e:
138
+ logging.error(f"Error in capturing email and mobile: {str(e)}")
139
  return jsonify({"error": "Something went wrong while processing."}), 500
140
 
141
+ # Route to handle audio transcription
 
 
 
 
142
  @app.route("/transcribe", methods=["POST"])
143
  def transcribe():
144
  if "audio" not in request.files:
 
166
  except Exception as e:
167
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
168
 
169
+ # Start the production server
170
  if __name__ == "__main__":
171
+ serve(app, host="0.0.0.0", port=7860)