-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.py
More file actions
210 lines (163 loc) · 7.21 KB
/
components.py
File metadata and controls
210 lines (163 loc) · 7.21 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
import base64
import json
import os
import requests
# Get API key from environment
KEY = ""
# 1. Generate lecture script and video timeline using Gemini Pro
def generate_lecture_content(summary, modular_details, keywords):
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"
prompt = f"""
Based on the following information,generate:
1. A complete, engaging lecture script for a video (approximately 3-5 minutes when narrated)
2. A JSON video timeline with timestamps and image prompts for each keyword
OVERALL SUMMARY:
{summary}
MODULAR DETAILS:
{modular_details}
KEYWORDS:
{keywords}
Please return your response in this exact JSON format:
{{
"lecture_script": "Your complete lecture script here...",
"video_timeline": [
{{
"start_time": 0,
"end_time": 15,
"keyword": "Music Arijit Singh",
"image_prompt": "Young Arijit Singh learning music in Murshidabad",
"transition": "fade"
}},
...
]
}}
Make sure the timeline covers the entire lecture duration with appropriate transitions.
"""
headers = {"x-goog-api-key": KEY, "Content-Type": "application/json"}
payload = {"contents": [{"parts": [{"text": prompt}]}]}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
result = response.json()
# Extract the text from the response
text_response = result["candidates"][0]["content"]["parts"][0]["text"]
# Parse JSON from the response (remove markdown code blocks if present)
text_response = text_response.strip()
if text_response.startswith("```"):
text_response = text_response[7:]
if text_response.startswith("```"):
text_response = text_response[3:]
if text_response.endswith("```"):
text_response = text_response[:-3]
return json.loads(text_response.strip())
else:
print(f"Error: {response.status_code}")
print(response.text)
return None
# 2. Generate images for each keyword using Gemini Image API
def generate_image_for_keyword(keyword, context, image_prompt, output_path):
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp-image-generation:generateContent"
headers = {"x-goog-api-key": KEY, "Content-Type": "application/json"}
# Create detailed prompt
full_prompt = f"{image_prompt}. Context: {context}. Style: Professional documentary photograph"
payload = {
"contents": [{"parts": [{"text": full_prompt}]}],
"generationConfig": {"responseModalities": ["Text", "image"]},
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
result = response.json()
# Extract base64 image data
try:
# candidates is a LIST, so use [0]
parts = result["candidates"][0]["content"]["parts"]
# Find the part with inlineData (image is usually in second part)
image_data = None
for part in parts:
if "inlineData" in part:
image_data = part["inlineData"]["data"]
break
if image_data:
# Decode and save image
image_bytes = base64.b64decode(image_data)
with open(output_path, "wb") as f:
f.write(image_bytes)
print(f"✅ Image saved: {output_path}")
return output_path
else:
print(f"❌ No image data found in response")
print("Response structure:", json.dumps(result, indent=2))
return None
except (KeyError, IndexError) as e:
print(f"❌ Error extracting image data: {e}")
print("Full response:", json.dumps(result, indent=2))
return None
else:
print(f"❌ Error generating image: {response.status_code}")
print(response.text)
return None
def generate_lecture_audio(lecture_script):
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-tts:generateContent"
headers = {"x-goog-api-key": KEY, "Content-Type": "application/json"}
payload = {
"contents": [{"parts": [{"text": lecture_script}]}],
"generationConfig": {
"responseModalities": ["AUDIO"],
"speechConfig": {
"voiceConfig": {"prebuiltVoiceConfig": {"voiceName": "Autonoe"}}
},
},
"model": "gemini-2.5-flash-preview-tts",
}
response = requests.post(url, headers=headers, json=payload)
data = response.json()
audio_b64 = data["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
# Write .pcm file
with open("out.pcm", "wb") as wb:
wb.write(base64.b64decode(audio_b64))
# Convert to wav using ffmpeg
os.system("ffmpeg -f s16le -ar 24000 -ac 1 -i out.pcm out.wav")
# Main execution
def main(ovs, md, kw, kws_dict):
print("Generating lecture script and timeline...")
content = generate_lecture_content(ovs, md, kw)
if content:
lecture_script = content["lecture_script"]
video_timeline = content["video_timeline"]
# Save lecture script
with open("lecture_script.txt", "w", encoding="utf-8") as f:
f.write(lecture_script)
print("\nLecture script saved to lecture_script.txt\n")
# Save timeline
with open("video_timeline.json", "w", encoding="utf-8") as f:
json.dump(video_timeline, f, indent=2)
print("Video timeline saved to video_timeline.json")
# Generate images for timeline
print("\nGenerating images for video timeline...")
os.makedirs("images", exist_ok=True)
for idx, segment in enumerate(video_timeline):
keyword = segment.get("keyword", f"segment_{idx}")
image_prompt = segment["image_prompt"]
# Find context from kws_dict
context = ""
for kw_key, details in kws_dict.items():
if kw_key.lower() in keyword.lower():
context = details["reference"]
break
output_path = f"images/image_{idx:03d}_{keyword.replace(' ', '_')[:30]}.png"
generate_image_for_keyword(keyword, context, image_prompt, output_path)
# Add image path to timeline
video_timeline[idx]["image_path"] = output_path
generate_lecture_audio(lecture_script)
print("\nLecture audio generated")
# Save updated timeline with image paths
with open("video_timeline_with_images.json", "w", encoding="utf-8") as f:
json.dump(video_timeline, f, indent=2)
print("\nUpdated timeline saved to video_timeline_with_images.json")
print("\n✅ All content generated successfully!")
print(f" - Lecture script: lecture_script.txt")
print(f" - Timeline: video_timeline_with_images.json")
print(f" - Images: images/ folder")
else:
print("Failed to generate content")
if __name__ == "__main__":
main()