-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
92 lines (70 loc) · 2.74 KB
/
app.py
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
import pyttsx3
from flask import Flask, render_template, request, jsonify, send_file, Response
from io import BytesIO
from training_data import convo
from model_utils import safety_settings_default
import google.generativeai as genai
import os
enc_api_key = "AIzaSyABntLwQVD7Ql7GxSHJN1ZPyMpz2yyyFRg" # Replace with Your Own API Key
genai.configure(api_key=enc_api_key)
app = Flask(__name__)
app.config['API_KEY'] = enc_api_key
app.config['SAFETY_SETTINGS'] = safety_settings_default
def print_current_api_key():
print(f"Current API key: {app.config['API_KEY']}")
print_current_api_key()
@app.route('/')
def home():
return render_template('index.html')
@app.route('/talk', methods=['GET', 'POST'])
def talk():
if request.method == 'GET':
return render_template('talk.html')
elif request.method == 'POST':
user_input = request.form.get('user_input')
response_text, audio_content = generate_response(user_input)
return Response(audio_content, mimetype="audio/wav")
@app.route('/privacy')
def privacy():
return render_template('Privacy.html')
@app.route('/settings')
def settings():
return render_template('settings.html', default_api_key=app.config['API_KEY'])
@app.route('/save_api_key', methods=['POST'])
def save_api_key():
api_key = request.form.get('api_key')
if api_key != app.config['API_KEY']:
app.config['API_KEY'] = api_key
app.config['SAFETY_SETTINGS'] = safety_settings_default
print_current_api_key()
return jsonify({'message': 'API Key saved successfully'})
else:
return jsonify({'message': 'Please enter a different API Key'})
@app.route('/send_message', methods=['POST'])
def send_message():
user_input = request.form.get('user_input')
response = generate_response(user_input)
return jsonify({'response': response})
@app.route('/download')
def download_page():
return render_template('downloads.html')
def generate_response(user_input):
response = convo.send_message(user_input).text
# Initialize the TTS engine
engine = pyttsx3.init()
# Set properties (optional)
engine.setProperty('rate', 170) # Speed of speech
# Save the response as a temporary audio file
audio_path = 'temp_audio.wav'
engine.save_to_file(response, audio_path)
# Wait for the speech to finish
engine.runAndWait()
# Read the saved audio file
with open(audio_path, 'rb') as audio_file:
audio_content = audio_file.read()
return response, audio_content
# Delete the temporary audio file
os.remove(audio_path)
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True, port=5001)
print_current_api_key()