summryai / falsk_app.py
Ramakrushna's picture
commit new files
2c566d7 verified
raw
history blame
1.72 kB
import groq_backend
from flask import Flask, request, jsonify, g
from typing import Dict, Union
import logging
import re
app = Flask(__name__)
logger = logging.getLogger(__name__)
# Configuration settings directly defined
app.config['DEFAULT_CHUNK_SIZE'] = 25*60*1000
app.config['TEMP_DIR'] = "temp"
YOUTUBE_URL_PATTERN = r'^(https?://)?(www\.)?(youtube\.com|youtu\.be)/.+$'
def validate_youtube_url(url: str) -> bool:
return re.match(YOUTUBE_URL_PATTERN, url) is not None
@app.route('/transcribe', methods=['POST'])
def transcribe() -> Union[Dict, str]:
data = request.json
yt_url = data.get('yt_url')
chunk_size = data.get('chunk_size', app.config['DEFAULT_CHUNK_SIZE'])
temp_dir = data.get('temp_dir', app.config['TEMP_DIR'])
if not yt_url:
error = "YouTube URL is required."
logger.error(error)
return jsonify({'error': error}), 400
if not validate_youtube_url(yt_url):
error = f"Invalid YouTube URL: {yt_url}"
logger.error(error)
return jsonify({'error': error}), 400
try:
print(f"Transcribing {yt_url} with chunk size {chunk_size} and temp dir {temp_dir}")
transcript = groq_backend.generate_youtube_transcript_with_groq(
yt_url, chunk_size, temp_dir
)
return jsonify({'transcript': transcript}), 200
except ValueError as e:
error = f"Failed to transcribe video: {str(e)}"
logger.exception(error)
return jsonify({'error': error}), 400
except Exception as e:
error = "Unexpected error occurred during transcription"
logger.exception(error)
return jsonify({'error': error}), 500
if __name__ == '__main__':
app.run()