Update app.py
Browse files
app.py
CHANGED
@@ -104,13 +104,18 @@ def generate_ai_response(user_input):
|
|
104 |
if not user_input or len(user_input.strip()) < 2:
|
105 |
return "I'm listening. Please say more."
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
@app.route("/talk", methods=["POST"])
|
116 |
def talk():
|
@@ -119,38 +124,73 @@ def talk():
|
|
119 |
|
120 |
# Save audio
|
121 |
audio_file = request.files["audio"]
|
122 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
123 |
-
audio_path = tmp.name
|
124 |
-
audio_file.save(audio_path)
|
125 |
|
126 |
try:
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
print(f"Transcription: {transcription}") # Debugging
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
# Use the centralized function to generate a response
|
138 |
-
final_response = generate_ai_response(transcription)
|
139 |
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
-
#
|
143 |
tts_audio_path = audio_path.replace(".wav", "_reply.wav")
|
144 |
-
tts.tts_to_file(text=final_response, file_path=tts_audio_path)
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
except Exception as e:
|
152 |
print(f"Error in talk endpoint: {str(e)}")
|
153 |
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
@app.route("/chat", methods=["POST"])
|
156 |
def chat():
|
|
|
104 |
if not user_input or len(user_input.strip()) < 2:
|
105 |
return "I'm listening. Please say more."
|
106 |
|
107 |
+
try:
|
108 |
+
# Generate response
|
109 |
+
raw_response = llm(user_input)[0]["generated_text"]
|
110 |
+
|
111 |
+
# Process to get clean, short response
|
112 |
+
final_response = process_response(user_input, raw_response)
|
113 |
+
|
114 |
+
return final_response
|
115 |
+
except Exception as e:
|
116 |
+
print(f"Error generating AI response: {str(e)}")
|
117 |
+
# Return a default response if anything goes wrong
|
118 |
+
return "I heard you, but I'm having trouble forming a response right now."
|
119 |
|
120 |
@app.route("/talk", methods=["POST"])
|
121 |
def talk():
|
|
|
124 |
|
125 |
# Save audio
|
126 |
audio_file = request.files["audio"]
|
|
|
|
|
|
|
127 |
|
128 |
try:
|
129 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
130 |
+
audio_path = tmp.name
|
131 |
+
audio_file.save(audio_path)
|
|
|
|
|
132 |
|
133 |
+
# Transcribe
|
134 |
+
try:
|
135 |
+
segments, _ = whisper_model.transcribe(audio_path)
|
136 |
+
transcription = "".join([seg.text for seg in segments])
|
|
|
|
|
137 |
|
138 |
+
print(f"Transcription: {transcription}") # Debugging
|
139 |
+
|
140 |
+
if not transcription.strip():
|
141 |
+
# Handle empty transcription
|
142 |
+
final_response = "I didn't catch that. Could you please speak again?"
|
143 |
+
else:
|
144 |
+
# Use the centralized function to generate a response
|
145 |
+
final_response = generate_ai_response(transcription)
|
146 |
+
|
147 |
+
print(f"Voice response: {final_response}") # Debugging
|
148 |
+
except Exception as e:
|
149 |
+
print(f"Transcription error: {str(e)}")
|
150 |
+
final_response = "I had trouble understanding that. Could you try again?"
|
151 |
|
152 |
+
# Prepare TTS output path
|
153 |
tts_audio_path = audio_path.replace(".wav", "_reply.wav")
|
|
|
154 |
|
155 |
+
try:
|
156 |
+
# Synthesize speech
|
157 |
+
tts.tts_to_file(text=final_response, file_path=tts_audio_path)
|
158 |
+
|
159 |
+
if not os.path.exists(tts_audio_path) or os.path.getsize(tts_audio_path) == 0:
|
160 |
+
raise Exception("TTS failed to generate audio file")
|
161 |
+
|
162 |
+
except Exception as e:
|
163 |
+
print(f"TTS error: {str(e)}")
|
164 |
+
# If TTS fails, generate a simple audio file with a message
|
165 |
+
# In a production app, you might want to have a pre-recorded fallback audio
|
166 |
+
tts_audio_path = audio_path # Just reuse the input path for now
|
167 |
+
final_response = "Sorry, I couldn't generate audio right now."
|
168 |
|
169 |
+
# Return both the audio file and the text response
|
170 |
+
try:
|
171 |
+
response = send_file(tts_audio_path, mimetype="audio/wav")
|
172 |
+
response.headers["X-Response-Text"] = final_response
|
173 |
+
response.headers["Access-Control-Expose-Headers"] = "X-Response-Text"
|
174 |
+
return response
|
175 |
+
except Exception as e:
|
176 |
+
print(f"Error sending file: {str(e)}")
|
177 |
+
return jsonify({
|
178 |
+
"error": "Could not send audio response",
|
179 |
+
"text_response": final_response
|
180 |
+
}), 500
|
181 |
+
|
182 |
except Exception as e:
|
183 |
print(f"Error in talk endpoint: {str(e)}")
|
184 |
return jsonify({"error": str(e)}), 500
|
185 |
+
finally:
|
186 |
+
# Clean up temporary files
|
187 |
+
try:
|
188 |
+
if 'audio_path' in locals() and os.path.exists(audio_path):
|
189 |
+
os.unlink(audio_path)
|
190 |
+
if 'tts_audio_path' in locals() and os.path.exists(tts_audio_path) and tts_audio_path != audio_path:
|
191 |
+
os.unlink(tts_audio_path)
|
192 |
+
except Exception as cleanup_error:
|
193 |
+
print(f"Error cleaning up files: {str(cleanup_error)}")
|
194 |
|
195 |
@app.route("/chat", methods=["POST"])
|
196 |
def chat():
|