File size: 2,979 Bytes
9abf335
86c4a24
 
 
9abf335
 
c23eee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba034
c23eee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9abf335
86c4a24
 
 
 
 
9abf335
 
 
 
 
 
 
 
c23eee0
9abf335
 
 
 
 
86c4a24
9abf335
86c4a24
 
 
 
 
 
b97eb8c
86c4a24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9abf335
86c4a24
 
9abf335
86c4a24
9abf335
 
362a65a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)