-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
124 lines (104 loc) · 4.34 KB
/
Copy pathapp.py
File metadata and controls
124 lines (104 loc) · 4.34 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
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from youtube_transcript_api import YouTubeTranscriptApi
from agent import run_research_agent
import logging
import os
app = Flask(__name__, static_folder='.', static_url_path='')
CORS(app)
logging.basicConfig(level=logging.INFO)
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('.', path)
@app.route('/api/research', methods=['POST'])
def research_topic():
data = request.get_json()
if not data or 'topic' not in data:
return jsonify({"error": "Missing 'topic' in request body"}), 400
topic = data['topic']
try:
result = run_research_agent(topic)
return jsonify(result)
except Exception as e:
logging.error(f"Error in research agent: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/config', methods=['GET'])
def get_config():
env_keys_str = os.environ.get("GEMINI_API_KEYS", os.environ.get("GEMINI_API_KEY", ""))
env_keys_str = env_keys_str.replace('"', '').replace("'", "")
keys = [k.strip() for k in env_keys_str.split(",") if k.strip()]
return jsonify({"gemini_keys": keys}), 200
@app.route('/transcript/<video_id>', methods=['GET'])
def get_transcript(video_id):
try:
logging.info(f"Fetching transcript for video: {video_id}")
ytt_api = YouTubeTranscriptApi()
# Get the list of available transcripts
try:
transcript_list = ytt_api.list(video_id)
except Exception as list_error:
# If listing fails, try direct fetch (default behavior)
logging.warning(f"Failed to list transcripts, trying direct fetch: {list_error}")
fetched_transcript = ytt_api.fetch(video_id)
full_text = " ".join([snippet.text for snippet in fetched_transcript])
return jsonify({
"video_id": video_id,
"transcript": full_text
})
transcript = None
# 1. Try to find a manually created English transcript
try:
transcript = transcript_list.find_manually_created_transcript(['en'])
except:
pass
# 2. If no manual English, try generated English
if not transcript:
try:
transcript = transcript_list.find_generated_transcript(['en'])
except:
pass
# 3. If no English at all, try to find ANY transcript and translate it to English
if not transcript:
# Iterate through available transcripts to find a translatable one
for t in transcript_list:
if t.is_translatable:
try:
transcript = t.translate('en')
break
except Exception as e:
logging.warning(f"Translation failed for {t.language_code}: {e}")
continue
# If still no transcript (and couldn't translate), try to just get the first available one as fallback
if not transcript:
for t in transcript_list:
transcript = t
break
if not transcript:
return jsonify({"error": "No suitable transcript found"}), 404
fetched_transcript = transcript.fetch()
# Extract text
full_text = " ".join([snippet.text for snippet in fetched_transcript])
return jsonify({
"video_id": video_id,
"transcript": full_text,
"language_code": transcript.language_code,
"is_generated": transcript.is_generated
})
except Exception as e:
logging.error(f"Error: {str(e)}")
# Try one last absolute fallback if the fancy logic fails
try:
ytt_api = YouTubeTranscriptApi()
fetched_transcript = ytt_api.fetch(video_id)
full_text = " ".join([snippet.text for snippet in fetched_transcript])
return jsonify({
"video_id": video_id,
"transcript": full_text
})
except:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)