from flask import Flask, render_template, request, jsonify import os from werkzeug.utils import secure_filename from pydub import AudioSegment import speech_recognition as sr import google.generativeai as genai from google.generativeai.types import HarmCategory, HarmBlockThreshold # Configure Gemini genai.configure(api_key=os.environ.get('GEMINI_KEY')) generation_config = { "temperature": 1, "top_p": 0.95, "top_k": 64, "max_output_tokens": 8192, "response_mime_type": "text/plain", } model = genai.GenerativeModel( model_name="gemini-2.5-flash", generation_config=generation_config, safety_settings={ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE, HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE, HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE, HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE, }, system_instruction="Respond like Paris Hilton" ) chat_session = model.start_chat(history=[]) def gemini_response(user_input): try: response = chat_session.send_message(user_input) return response.text.split('-***-')[0].strip() if '-***-' in response.text else response.text.strip() except Exception as e: print("Gemini error:", e) return "Oops! Paris is having a moment. Try again later 💅" app = Flask(__name__) UPLOAD_FOLDER = 'uploads' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) @app.route('/') def index(): return render_template('index.html') @app.route('/send_message', methods=['POST']) def send_message(): user_input = request.form['user_input'] response = gemini_response(user_input) return jsonify({'response': response, 'user_input': user_input}) def echo_response(user_input): return user_input def respond(audio_path): recognizer = sr.Recognizer() audio = AudioSegment.from_file(audio_path) audio.export("temp.wav", format="wav") with sr.AudioFile("temp.wav") as source: audio_data = recognizer.record(source) text = recognizer.recognize_google(audio_data) os.remove("temp.wav") print(text) return text @app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: return jsonify({'error': 'No file part'}) file = request.files['file'] if file.filename == '': return jsonify({'error': 'No selected file'}) filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) # Process the audio file and get the response text response_text = respond(file_path) # Remove the audio file after processing os.remove(file_path) return jsonify({'text': response_text}) if __name__ == '__main__': app.run(host="0.0.0.0", port=7860)