-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeminifull.py
131 lines (106 loc) · 3.97 KB
/
geminifull.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
import os
import json
import google.generativeai as genai
from google.ai.generativelanguage_v1beta.types import content
from pybars import Compiler
# Configure Gemini API
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Create the model
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
"response_schema": content.Schema(
type=content.Type.OBJECT,
enum=[],
required=["title", "summary", "sections"],
properties={
"title": content.Schema(
type=content.Type.STRING,
),
"summary": content.Schema(
type=content.Type.STRING,
),
"sections": content.Schema(
type=content.Type.ARRAY,
items=content.Schema(
type=content.Type.OBJECT,
enum=[],
required=["timestamp", "header", "content"],
properties={
"timestamp": content.Schema(
type=content.Type.NUMBER,
),
"header": content.Schema(
type=content.Type.STRING,
),
"content": content.Schema(
type=content.Type.STRING,
),
},
),
),
},
),
"response_mime_type": "application/json",
}
model = genai.GenerativeModel(
model_name="gemini-2.0-flash-exp",
generation_config=generation_config,
)
system_instruction = (
"You are tasked with creating a detailed newsletter from podcast data. "
"The newsletter should include a title, summary, and sections summarizing "
"key parts of the content. Each section should have at least one detailed paragraph, "
"explaining the topic discussed in depth. Timestamps should be formatted in HH:MM:SS, and "
"each section should be professional and exclude advertisements. "
"The content is sourced from a JSON object and an uploaded audio file."
)
print("Please upload the podcast audio file.")
def process_uploaded_file(audio_file_path):
# Read episode metadata
with open("episode.json", "r") as f:
episode_data = json.load(f)
# Generate Newsletter using Gemini
print("Generating newsletter with Gemini...")
chat_session = model.start_chat(history=[])
response = chat_session.send_message(
f"System: {system_instruction}\nPodcast episode: {json.dumps(episode_data)}\nAudio file uploaded: {audio_file_path}"
)
json_input = json.loads(response.text)
# Base URL for timestamps
base_url = audio_file_path
def format_timestamp(seconds):
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = int(seconds % 60)
return f"{hours:02}:{minutes:02}:{seconds:02}"
# Preprocess JSON to include formatted timestamps
for section in json_input["sections"]:
section["formatted_timestamp"] = format_timestamp(section["timestamp"])
# Handlebars template
markdown_template = """
# {{title}}
{{summary}}
{{#each sections}}
## {{header}}
{{content}}
[{{formatted_timestamp}}]({{../base_url}}#t={{timestamp}})
{{/each}}
"""
# Compile the template
compiler = Compiler()
template = compiler.compile(markdown_template)
# Add the base_url to the JSON input
json_input["base_url"] = base_url
# Generate Markdown
output = template(json_input)
# Save the markdown
newsletter_file = "newsletter.md"
with open(newsletter_file, "w", encoding="utf-8") as f:
f.write(output)
print(f"Markdown newsletter with detailed segments saved to {newsletter_file}")
# Example call to process the uploaded file (to be replaced by actual file handling logic in a multimodal environment)
# Assuming the uploaded file is saved as "uploaded_audio.mp3"
process_uploaded_file("uploaded_audio.mp3")