-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
135 lines (111 loc) · 4.57 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import json
import logging
import requests
from flask import Flask, render_template, request, send_file, jsonify, redirect, url_for
from deepgram import DeepgramClient, PrerecordedOptions
from banner_generator import create_banner_from_json, update_json_with_gpt
from werkzeug.utils import secure_filename
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
app.config['UPLOAD_FOLDER_BACKGROUNDS'] = 'static/backgrounds'
app.config['UPLOAD_FOLDER_LOGOS'] = 'static/logos'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'webp'}
# Initialize Deepgram client
deepgram_api_key = os.getenv('DG_API_KEY')
deepgram = DeepgramClient(deepgram_api_key)
# OpenAI API key from environment variable
openai_api_key = os.getenv('OPENAI_API_KEY')
# Initial JSON structure
json_data = {
"background_image": "background1.jpg",
"logos": [
{"logo_name": "metro-logo.jpg", "position": 1},
{"logo_name": "mochi-logo.webp", "position": 2}
],
"include_and_more": True,
"primary_copy": "Make Suave Strides",
"sub_copy": "UNDER INR 3999",
"strike_through": "3999"
}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
backgrounds = os.listdir(app.config['UPLOAD_FOLDER_BACKGROUNDS'])
logos = os.listdir(app.config['UPLOAD_FOLDER_LOGOS'])
# Generate the initial banner
banner_path = create_banner_from_json(json_data)
return render_template('index.html',
backgrounds=backgrounds,
logos=logos,
json_data=json.dumps(json_data, indent=2),
banner_path=banner_path)
@app.route('/upload_background', methods=['POST'])
def upload_background():
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER_BACKGROUNDS'], filename))
return redirect(url_for('index'))
@app.route('/upload_logo', methods=['POST'])
def upload_logo():
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER_LOGOS'], filename))
return redirect(url_for('index'))
@app.route('/delete_background/<filename>')
def delete_background(filename):
os.remove(os.path.join(app.config['UPLOAD_FOLDER_BACKGROUNDS'], filename))
return redirect(url_for('index'))
@app.route('/delete_logo/<filename>')
def delete_logo(filename):
os.remove(os.path.join(app.config['UPLOAD_FOLDER_LOGOS'], filename))
return redirect(url_for('index'))
@app.route('/generate', methods=['POST'])
def generate():
global json_data
command = request.form['command']
json_data = update_json_with_gpt(command, json_data)
banner_path = create_banner_from_json(json_data)
return redirect(url_for('index'))
@app.route('/download_banner')
def download_banner():
banner_path = "static/final_banner.jpg"
return send_file(banner_path, as_attachment=True)
@app.route('/voice_command', methods=['POST'])
def voice_command():
try:
audio_file = request.files['audio']
audio_file_path = os.path.join("static", "uploads", secure_filename(audio_file.filename))
audio_file.save(audio_file_path)
logging.debug(f"Audio file saved to {audio_file_path}")
# OpenAI API URL and headers
url = "https://api.openai.com/v1/audio/transcriptions"
headers = {
"Authorization": f"Bearer {openai_api_key}",
}
# Send the request to OpenAI's Whisper API
with open(audio_file_path, "rb") as f:
response = requests.post(url, headers=headers, files={
'file': f,
'model': (None, 'whisper-1')
})
logging.debug(f"OpenAI API response: {response.json()}")
# Get the transcription text
response_json = response.json()
transcription_text = response_json.get("text", "")
logging.debug(f"Transcription: {transcription_text}")
return jsonify({"transcription": transcription_text})
except Exception as e:
logging.error(f"Exception during transcription: {e}")
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)