Repository navigation
Expand file tree
/
Copy pathweb_api.py
More file actions
608 lines (496 loc) · 22.5 KB
/
Copy pathweb_api.py
File metadata and controls
608 lines (496 loc) · 22.5 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import os
import tempfile
import logging
import argparse
import time
import uuid
import inflect
from flask import Flask, request, jsonify, send_file, Response, send_from_directory
from flask_cors import CORS
from src.price_fetcher import get_crypto_price, get_crypto_price_with_change, get_multiple_crypto_prices, get_crypto_historical_data
from src.narrator import generate_narration_file
from src.app_config import app_settings
# Create an instance of the inflect engine for natural language formatting
p = inflect.engine()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
# Create Flask app
app = Flask(__name__, static_folder='static')
CORS(app) # Enable CORS for all routes
# Dictionary to store temporary audio files
# Format: {file_id: (filepath, expiration_timestamp)}
temp_files = {}
# Directory for shared temporary files (works across multiple workers)
SHARED_TEMP_DIR = "/tmp/quantlink-audio"
def _ensure_temp_dir():
"""Ensure the shared temporary directory exists."""
os.makedirs(SHARED_TEMP_DIR, exist_ok=True)
def _get_file_info_path(file_id: str) -> str:
"""Get the path for a file's metadata info."""
return os.path.join(SHARED_TEMP_DIR, f"{file_id}.info")
def _store_file_info(file_id: str, filepath: str, expiration: float):
"""Store file information in filesystem for multi-worker access."""
_ensure_temp_dir()
info_path = _get_file_info_path(file_id)
with open(info_path, 'w') as f:
f.write(f"{filepath}\n{expiration}")
def _get_file_info(file_id: str) -> tuple:
"""Get file information from filesystem."""
info_path = _get_file_info_path(file_id)
if not os.path.exists(info_path):
return None, None
try:
with open(info_path, 'r') as f:
lines = f.read().strip().split('\n')
if len(lines) >= 2:
return lines[0], float(lines[1])
except (ValueError, IndexError):
pass
return None, None
def _cleanup_file_info(file_id: str):
"""Remove file info when cleaning up."""
info_path = _get_file_info_path(file_id)
if os.path.exists(info_path):
try:
os.remove(info_path)
except Exception:
pass
def _format_price_in_words(price: float, currency: str) -> str:
"""
Converts a price and currency into a natural language string.
Args:
price (float): The price to convert.
currency (str): The currency code (e.g., "USD").
Returns:
str: A string like "one hundred dollars and fifty cents".
"""
dollars = int(price)
cents = int(round((price - dollars) * 100))
# Basic currency name mapping
currency_map = {
"USD": "dollar",
"EUR": "euro",
"GBP": "pound",
"JPY": "yen"
}
currency_name = currency_map.get(currency.upper(), currency.upper())
# Convert dollar amount to words
price_words = p.number_to_words(dollars)
currency_words = p.plural_noun(currency_name, dollars)
result = f"{price_words} {currency_words}"
# Add cents if they exist
if cents > 0:
cent_words = p.number_to_words(cents)
cent_currency_name = p.plural_noun("cent", cents)
result += f" and {cent_words} {cent_currency_name}"
return result
def _format_narration_text(base_text: str) -> str:
"""
Applies SSML formatting for better speech synthesis.
Args:
base_text (str): The base text to format.
Returns:
str: The formatted text with SSML tags.
"""
speech_rate = app_settings.elevenlabs_speech_rate if app_settings else "medium"
return f'<prosody rate="{speech_rate}">{base_text}</prosody>'
@app.route('/api/health', methods=['GET'])
def health_check():
"""Simple health check endpoint."""
return jsonify({"status": "healthy", "version": "1.0.0"})
@app.route('/api/crypto/price', methods=['GET'])
def get_price():
"""
Get cryptocurrency price data without narration.
Query parameters:
crypto: Cryptocurrency ID (e.g., bitcoin)
currency: Currency code (e.g., usd)
with_24h_change: Whether to include 24h price change (true/false)
with_7d_change: Whether to include 7d price change (true/false)
with_30d_change: Whether to include 30d price change (true/false)
"""
try:
crypto_id = request.args.get('crypto', app_settings.default_crypto_id).lower()
currency = request.args.get('currency', app_settings.default_vs_currency).lower()
# Check if we should include price changes
include_24h = request.args.get('with_24h_change', 'false').lower() == 'true'
include_7d = request.args.get('with_7d_change', 'false').lower() == 'true'
include_30d = request.args.get('with_30d_change', 'false').lower() == 'true'
if include_24h or include_7d or include_30d:
# Get price with changes
result = get_crypto_price_with_change(
crypto_id,
currency,
include_24h=include_24h,
include_7d=include_7d,
include_30d=include_30d
)
return jsonify(result)
else:
# Get basic price without changes
name, price = get_crypto_price(crypto_id, currency)
if name and price is not None:
return jsonify({
"name": name,
"current_price": price,
"currency": currency.upper(),
"success": True
})
else:
return jsonify({
"name": crypto_id.capitalize(),
"current_price": None,
"currency": currency.upper(),
"success": False,
"error": "Failed to fetch price"
}), 404
except Exception as e:
logger.error(f"Error in get_price endpoint: {e}", exc_info=True)
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/crypto/prices', methods=['GET'])
def get_prices():
"""
Get prices for multiple cryptocurrencies without narration.
Query parameters:
cryptos: Comma-separated list of cryptocurrency IDs
currency: Currency code (e.g., usd)
with_24h_change: Whether to include 24h price change (true/false)
"""
try:
crypto_ids_param = request.args.get('cryptos')
if not crypto_ids_param:
return jsonify({"success": False, "error": "No cryptocurrencies specified"}), 400
crypto_ids = [crypto.strip().lower() for crypto in crypto_ids_param.split(',')]
currency = request.args.get('currency', app_settings.default_vs_currency).lower()
include_change = request.args.get('with_24h_change', 'false').lower() == 'true'
results = get_multiple_crypto_prices(crypto_ids, currency, include_change=include_change)
# Convert dictionary of results to a list for the API response
response_data = {
"prices": results,
"success": any(item["success"] for item in results.values()),
"count": len(results)
}
return jsonify(response_data)
except Exception as e:
logger.error(f"Error in get_prices endpoint: {e}", exc_info=True)
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/narrator/text', methods=['POST'])
def narrate_custom_text():
"""
Generate audio narration for custom text.
Request JSON body:
text: The text to narrate
lang: Language code (default: en)
slow: Whether to use slow narration speed (default: false)
return_audio: Whether to return the audio file (default: false)
Returns:
If return_audio is false: JSON with success status and file ID
If return_audio is true: The audio file as attachment
"""
try:
data = request.json
if not data or 'text' not in data:
return jsonify({"success": False, "error": "No text provided"}), 400
text = data.get('text')
lang = data.get('lang', 'en')
slow = data.get('slow', False)
return_audio = data.get('return_audio', False)
# Create temporary file for the audio
_ensure_temp_dir()
temp_filepath = os.path.join(SHARED_TEMP_DIR, f"{uuid.uuid4()}.mp3")
# Generate audio without playing it
try:
# Apply SSML formatting for better speech synthesis
formatted_text = _format_narration_text(text)
success = generate_narration_file(
text_to_narrate=formatted_text,
output_filepath=temp_filepath,
lang=lang,
slow=slow,
force_new=True # Force new generation for API requests
)
if not success:
raise Exception("Narration generation failed")
# Generate a unique ID for the file
file_id = str(uuid.uuid4())
# Store the file path with an expiration time (30 minutes from now)
expiration = time.time() + (30 * 60) # 30 minutes
_store_file_info(file_id, temp_filepath, expiration)
# Clean up expired files
_cleanup_expired_files()
if return_audio:
return send_file(
temp_filepath,
mimetype="audio/mpeg",
as_attachment=True,
download_name=f"narration_{file_id}.mp3"
)
else:
return jsonify({
"success": True,
"file_id": file_id,
"expires_in_seconds": 30 * 60
})
except Exception as e:
# Clean up the temporary file if narration failed
if os.path.exists(temp_filepath):
os.remove(temp_filepath)
raise e
except Exception as e:
logger.error(f"Error in narrate_custom_text endpoint: {e}", exc_info=True)
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/narrator/audio/<file_id>', methods=['GET'])
def get_audio_file(file_id):
"""
Get a previously generated audio file by its ID.
Path parameters:
file_id: The ID of the audio file
"""
try:
# Check if the file exists and is not expired
filepath, expiration = _get_file_info(file_id)
if filepath is None:
return jsonify({"success": False, "error": "Invalid file ID"}), 404
if time.time() > expiration:
# File has expired
_cleanup_file_info(file_id)
if os.path.exists(filepath):
os.remove(filepath)
return jsonify({"success": False, "error": "Audio file has expired"}), 404
if os.path.exists(filepath):
return send_file(
filepath,
mimetype="audio/mpeg",
as_attachment=True,
download_name=f"narration_{file_id}.mp3"
)
else:
# File was removed from disk
_cleanup_file_info(file_id)
return jsonify({"success": False, "error": "Audio file not found"}), 404
except Exception as e:
logger.error(f"Error in get_audio_file endpoint: {e}", exc_info=True)
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/narrator/crypto', methods=['POST'])
def narrate_crypto_price():
"""
Generate audio narration for a cryptocurrency price.
Request JSON body:
crypto: Cryptocurrency ID
currency: Currency code
with_24h_change: Whether to include 24h price change
with_7d_change: Whether to include 7d price change
with_30d_change: Whether to include 30d price change
lang: Language code
slow: Whether to use slow narration speed
return_audio: Whether to return the audio file
Returns:
If return_audio is false: JSON with success status, price data, and file ID
If return_audio is true: The audio file as attachment
"""
try:
data = request.json
if not data:
return jsonify({"success": False, "error": "No data provided"}), 400
crypto_id = data.get('crypto', app_settings.default_crypto_id).lower()
currency = data.get('currency', app_settings.default_vs_currency).lower()
# Check if we should include price changes
include_24h = data.get('with_24h_change', False)
include_7d = data.get('with_7d_change', False)
include_30d = data.get('with_30d_change', False)
lang = data.get('lang', 'en')
slow = data.get('slow', False)
return_audio = data.get('return_audio', False)
# Fetch price data with changes if requested
if include_24h or include_7d or include_30d:
price_data = get_crypto_price_with_change(
crypto_id,
currency,
include_24h=include_24h,
include_7d=include_7d,
include_30d=include_30d
)
if not price_data["success"]:
return jsonify({
"success": False,
"error": "Failed to fetch price data",
"price_data": price_data
}), 404
# Build narration text with natural language formatting
crypto_name = price_data.get("name", "Unknown")
price = price_data.get("current_price", 0.0)
currency_code = price_data.get("currency", "USD")
# Format the price into natural language
price_in_words = _format_price_in_words(price, currency_code)
narration_text = f"The current price for {crypto_name} is <break time=\"0.3s\" /> {price_in_words}."
# Add 24-hour change if available
if include_24h and "price_change_24h" in price_data and price_data["price_change_24h"] is not None:
change_24h = price_data["price_change_24h"]
direction = "up" if change_24h >= 0 else "down"
narration_text += f" It has gone {direction} {abs(change_24h):.2f} percent in the last 24 hours."
# Add 7-day change if available
if include_7d and "price_change_7d" in price_data and price_data["price_change_7d"] is not None:
change_7d = price_data["price_change_7d"]
direction = "up" if change_7d >= 0 else "down"
narration_text += f" Over the past 7 days, it has gone {direction} {abs(change_7d):.2f} percent."
# Add 30-day change if available
if include_30d and "price_change_30d" in price_data and price_data["price_change_30d"] is not None:
change_30d = price_data["price_change_30d"]
direction = "up" if change_30d >= 0 else "down"
narration_text += f" In the last 30 days, it has gone {direction} {abs(change_30d):.2f} percent."
else:
# Fetch basic price without changes
name, price = get_crypto_price(crypto_id, currency)
if not (name and price is not None):
return jsonify({
"success": False,
"error": "Failed to fetch price data"
}), 404
# Build price data object
price_data = {
"name": name,
"current_price": price,
"currency": currency.upper(),
"success": True
}
# Build narration text with natural language formatting
price_in_words = _format_price_in_words(price, currency.upper())
narration_text = f"The current price for {name} is <break time=\"0.3s\" /> {price_in_words}."
# Create temporary file for the audio
_ensure_temp_dir()
temp_filepath = os.path.join(SHARED_TEMP_DIR, f"{uuid.uuid4()}.mp3")
# Generate audio without playing it
try:
# Apply SSML formatting for better speech synthesis
formatted_narration_text = _format_narration_text(narration_text)
success = generate_narration_file(
text_to_narrate=formatted_narration_text,
output_filepath=temp_filepath,
lang=lang,
slow=slow,
force_new=True # Force new generation for API requests
)
if not success:
raise Exception("Narration generation failed")
# Generate a unique ID for the file
file_id = str(uuid.uuid4())
# Store the file path with an expiration time (30 minutes from now)
expiration = time.time() + (30 * 60) # 30 minutes
_store_file_info(file_id, temp_filepath, expiration)
# Clean up expired files
_cleanup_expired_files()
if return_audio:
return send_file(
temp_filepath,
mimetype="audio/mpeg",
as_attachment=True,
download_name=f"narration_{file_id}.mp3"
)
else:
return jsonify({
"success": True,
"price_data": price_data,
"narration_text": narration_text,
"file_id": file_id,
"expires_in_seconds": 30 * 60
})
except Exception as e:
# Clean up the temporary file if narration failed
if os.path.exists(temp_filepath):
os.remove(temp_filepath)
raise e
except Exception as e:
logger.error(f"Error in narrate_crypto_price endpoint: {e}", exc_info=True)
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/crypto/historical', methods=['GET'])
def get_historical_data():
"""
Get historical price data for a cryptocurrency.
Query parameters:
crypto: Cryptocurrency ID (e.g., bitcoin)
currency: Currency code (e.g., usd)
days: Number of days (1, 7, 14, 30, 90, 180, 365) - default: 7
"""
try:
crypto_id = request.args.get('crypto', app_settings.default_crypto_id).lower()
currency = request.args.get('currency', app_settings.default_vs_currency).lower()
days = int(request.args.get('days', 7))
# Validate days parameter
allowed_days = [1, 7, 14, 30, 90, 180, 365]
if days not in allowed_days:
return jsonify({
"success": False,
"error": f"Invalid days parameter. Allowed values: {allowed_days}"
}), 400
result = get_crypto_historical_data(crypto_id, currency, days)
return jsonify(result)
except ValueError:
return jsonify({"success": False, "error": "Invalid days parameter. Must be an integer."}), 400
except Exception as e:
logger.error(f"Error in get_historical_data endpoint: {e}", exc_info=True)
return jsonify({"success": False, "error": str(e)}), 500
def _cleanup_expired_files():
"""Clean up expired temporary files."""
current_time = time.time()
_ensure_temp_dir()
# Look for .info files in the shared temp directory
try:
for filename in os.listdir(SHARED_TEMP_DIR):
if filename.endswith('.info'):
file_id = filename[:-5] # Remove .info extension
filepath, expiration = _get_file_info(file_id)
if filepath and current_time > expiration:
# Remove expired audio file
if os.path.exists(filepath):
try:
os.remove(filepath)
logger.debug(f"Removed expired audio file: {filepath}")
except Exception as e:
logger.error(f"Error removing expired file {filepath}: {e}")
# Remove info file
_cleanup_file_info(file_id)
except Exception as e:
logger.error(f"Error during cleanup: {e}")
@app.route('/', methods=['GET'])
def index():
"""Serve the main HTML interface."""
return send_from_directory(app.static_folder, 'index.html')
@app.route('/logo.svg')
def logo():
"""Serve the logo file."""
return send_from_directory(app.static_folder, 'logo.svg')
@app.route('/<path:filename>')
def static_files(filename):
"""Serve static files for React app with SPA routing support."""
try:
# Try to serve the requested file
return send_from_directory(app.static_folder, filename)
except Exception:
# If file doesn't exist, serve index.html for client-side routing
return send_from_directory(app.static_folder, 'index.html')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="QuantLink FREN Narrator Web API")
parser.add_argument('--host', type=str, default='127.0.0.1', help='Host to run the server on')
parser.add_argument('--port', type=int, default=5000, help='Port to run the server on')
parser.add_argument('--debug', action='store_true', help='Run in debug mode')
args = parser.parse_args()
if args.debug:
app.logger.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
# Inform user about the web server
print(f"Starting QuantLink FREN Narrator Web API on http://{args.host}:{args.port}")
print("Available endpoints:")
print(" GET /api/health - Health check")
print(" GET /api/crypto/price - Get cryptocurrency price")
print(" GET /api/crypto/prices - Get multiple cryptocurrency prices")
print(" POST /api/narrator/text - Narrate custom text")
print(" GET /api/narrator/audio/<file_id> - Get audio file by ID")
print(" POST /api/narrator/crypto - Narrate cryptocurrency price")
print(" GET /api/crypto/historical - Get historical price data")
# Run the Flask app
app.run(host=args.host, port=args.port, debug=args.debug)