Spaces:
Configuration error
Configuration error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,420 +1,87 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 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 |
-
"speaker": 2,
|
| 91 |
-
"line": "And how are researchers even approaching that? What are some of the big ideas out there?"
|
| 92 |
-
},
|
| 93 |
-
{
|
| 94 |
-
"speaker": 1,
|
| 95 |
-
"line": "Well, there are a few different schools of thought. One is this idea of neuromorphic computing where they're literally trying to build computer chips that mimic the structure and function of the human brain."
|
| 96 |
-
},
|
| 97 |
-
{
|
| 98 |
-
"speaker": 2,
|
| 99 |
-
"line": "Wow, so like actually replicating the physical architecture of the brain. That's wild."
|
| 100 |
-
},
|
| 101 |
-
{
|
| 102 |
-
"speaker": 1,
|
| 103 |
-
"line": "It's pretty mind-blowing stuff and then you've got folks working on something called whole brain emulation."
|
| 104 |
-
},
|
| 105 |
-
{
|
| 106 |
-
"speaker": 2,
|
| 107 |
-
"line": "Okay, and what's that all about?"
|
| 108 |
-
},
|
| 109 |
-
{
|
| 110 |
-
"speaker": 1,
|
| 111 |
-
"line": "The basic idea there is to create a complete digital copy of a human brain down to the last neuron and synapse and run it on a sufficiently powerful computer simulation."
|
| 112 |
-
},
|
| 113 |
-
{
|
| 114 |
-
"speaker": 2,
|
| 115 |
-
"line": "Hold on, a digital copy of an entire brain, that sounds like something straight out of science fiction."
|
| 116 |
-
},
|
| 117 |
-
{
|
| 118 |
-
"speaker": 1,
|
| 119 |
-
"line": "It does, doesn't it? But it gives you an idea of the kind of ambition we're talking about here and the truth is we're still a long way off from truly achieving AGI, no matter which approach you look at."
|
| 120 |
-
},
|
| 121 |
-
{
|
| 122 |
-
"speaker": 2,
|
| 123 |
-
"line": "That makes sense but it's still exciting to think about the possibilities, even if they're a ways off."
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
"speaker": 1,
|
| 127 |
-
"line": "Absolutely and those possibilities are what really get people fired up about AGI, right? Yeah."
|
| 128 |
-
},
|
| 129 |
-
{
|
| 130 |
-
"speaker": 2,
|
| 131 |
-
"line": "For sure. In fact, I remember you mentioning something in that podcast about AGI's potential to revolutionize scientific research. Something about supercharging breakthroughs."
|
| 132 |
-
},
|
| 133 |
-
{
|
| 134 |
-
"speaker": 1,
|
| 135 |
-
"line": "Oh, absolutely. Imagine an AI that doesn't just crunch numbers but actually understands scientific data the way a human researcher does. We're talking about potential breakthroughs in everything from medicine and healthcare to material science and climate change."
|
| 136 |
-
},
|
| 137 |
-
{
|
| 138 |
-
"speaker": 2,
|
| 139 |
-
"line": "It's like giving scientists this incredibly powerful new tool to tackle some of the biggest challenges we face."
|
| 140 |
-
},
|
| 141 |
-
{
|
| 142 |
-
"speaker": 1,
|
| 143 |
-
"line": "Exactly, it could be a total game changer."
|
| 144 |
-
},
|
| 145 |
-
{
|
| 146 |
-
"speaker": 2,
|
| 147 |
-
"line": "Okay, but let's be real, every coin has two sides. What about the potential downsides of AGI? Because it can't all be sunshine and roses, right?"
|
| 148 |
-
},
|
| 149 |
-
{
|
| 150 |
-
"speaker": 1,
|
| 151 |
-
"line": "Right, there are definitely valid concerns. Probably the biggest one is the impact on the job market. As AGI gets more sophisticated, there's a real chance it could automate a lot of jobs that are currently done by humans."
|
| 152 |
-
},
|
| 153 |
-
{
|
| 154 |
-
"speaker": 2,
|
| 155 |
-
"line": "So we're not just talking about robots taking over factories but potentially things like, what, legal work, analysis, even creative fields?"
|
| 156 |
-
},
|
| 157 |
-
{
|
| 158 |
-
"speaker": 1,
|
| 159 |
-
"line": "Potentially, yes. And that raises a whole host of questions about what happens to those workers, how we retrain them, how we ensure that the benefits of AGI are shared equitably."
|
| 160 |
-
},
|
| 161 |
-
{
|
| 162 |
-
"speaker": 2,
|
| 163 |
-
"line": "Right, because it's not just about the technology itself, but how we choose to integrate it into society."
|
| 164 |
-
},
|
| 165 |
-
{
|
| 166 |
-
"speaker": 1,
|
| 167 |
-
"line": "Absolutely. We need to be having these conversations now about ethics, about regulation, about how to make sure AGI is developed and deployed responsibly."
|
| 168 |
-
},
|
| 169 |
-
{
|
| 170 |
-
"speaker": 2,
|
| 171 |
-
"line": "So it's less about preventing some kind of sci-fi robot apocalypse and more about making sure we're steering this technology in the right direction from the get-go."
|
| 172 |
-
},
|
| 173 |
-
{
|
| 174 |
-
"speaker": 1,
|
| 175 |
-
"line": "Exactly, AGI has the potential to be incredibly beneficial, but it's not going to magically solve all our problems. It's on us to make sure we're using it for good."
|
| 176 |
-
},
|
| 177 |
-
{
|
| 178 |
-
"speaker": 2,
|
| 179 |
-
"line": "It's like you said earlier, it's about shaping the future of intelligence."
|
| 180 |
-
},
|
| 181 |
-
{
|
| 182 |
-
"speaker": 1,
|
| 183 |
-
"line": "I like that. It really is."
|
| 184 |
-
},
|
| 185 |
-
{
|
| 186 |
-
"speaker": 2,
|
| 187 |
-
"line": "And honestly, that's a responsibility that extends beyond just the researchers and the policymakers."
|
| 188 |
-
},
|
| 189 |
-
{
|
| 190 |
-
"speaker": 1,
|
| 191 |
-
"line": "100%"
|
| 192 |
-
},
|
| 193 |
-
{
|
| 194 |
-
"speaker": 2,
|
| 195 |
-
"line": "So to everyone listening out there I'll leave you with this. As AGI continues to develop, what role do you want to play in shaping its future?"
|
| 196 |
-
},
|
| 197 |
-
{
|
| 198 |
-
"speaker": 1,
|
| 199 |
-
"line": "That's a question worth pondering."
|
| 200 |
-
},
|
| 201 |
-
{
|
| 202 |
-
"speaker": 2,
|
| 203 |
-
"line": "It certainly is and on that note, we'll wrap up this deep dive. Thanks for listening, everyone."
|
| 204 |
-
},
|
| 205 |
-
{
|
| 206 |
-
"speaker": 1,
|
| 207 |
-
"line": "Peace."
|
| 208 |
-
}
|
| 209 |
-
]
|
| 210 |
-
}
|
| 211 |
-
"""
|
| 212 |
-
|
| 213 |
-
if language == "Auto Detect":
|
| 214 |
-
language_instruction = "- The podcast MUST be in the same language as the user input."
|
| 215 |
-
else:
|
| 216 |
-
language_instruction = f"- The podcast MUST be in {language} language"
|
| 217 |
-
|
| 218 |
-
system_prompt = f"""
|
| 219 |
-
You are a professional podcast generator. Your task is to generate a professional podcast script based on the user input.
|
| 220 |
-
{language_instruction}
|
| 221 |
-
- The podcast should have 2 speakers.
|
| 222 |
-
- The podcast should be long.
|
| 223 |
-
- Do not use names for the speakers.
|
| 224 |
-
- The podcast should be interesting, lively, and engaging, and hook the listener from the start.
|
| 225 |
-
- The input text might be disorganized or unformatted, originating from sources like PDFs or text files. Ignore any formatting inconsistencies or irrelevant details; your task is to distill the essential points, identify key definitions, and highlight intriguing facts that would be suitable for discussion in a podcast.
|
| 226 |
-
- The script must be in JSON format.
|
| 227 |
-
Follow this example structure:
|
| 228 |
-
{example}
|
| 229 |
-
"""
|
| 230 |
-
user_prompt = f"Please generate a podcast script based on the following user input:\n{prompt}"
|
| 231 |
-
|
| 232 |
-
messages = [
|
| 233 |
-
{"role": "user", "parts": [user_prompt]}
|
| 234 |
-
]
|
| 235 |
-
|
| 236 |
-
genai.configure(api_key=api_key)
|
| 237 |
-
|
| 238 |
-
generation_config = {
|
| 239 |
-
"temperature": 1,
|
| 240 |
-
"max_output_tokens": 8192,
|
| 241 |
-
"response_mime_type": "application/json",
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
-
model = genai.GenerativeModel(
|
| 245 |
-
model_name="gemini-1.5-flash",
|
| 246 |
-
generation_config=generation_config,
|
| 247 |
-
safety_settings={
|
| 248 |
-
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
| 249 |
-
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
| 250 |
-
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
| 251 |
-
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE
|
| 252 |
-
},
|
| 253 |
-
system_instruction=system_prompt
|
| 254 |
-
)
|
| 255 |
-
|
| 256 |
-
try:
|
| 257 |
-
response = await model.generate_content_async(messages)
|
| 258 |
-
except Exception as e:
|
| 259 |
-
if "API key not valid" in str(e):
|
| 260 |
-
raise gr.Error("Invalid API key. Please provide a valid Gemini API key.")
|
| 261 |
-
elif "rate limit" in str(e).lower():
|
| 262 |
-
raise gr.Error("Rate limit exceeded for the API key. Please try again later or provide your own Gemini API key.")
|
| 263 |
-
else:
|
| 264 |
-
raise gr.Error(f"Failed to generate podcast script: {e}")
|
| 265 |
-
|
| 266 |
-
print(f"Generated podcast script:\n{response.text}")
|
| 267 |
-
|
| 268 |
-
return json.loads(response.text)
|
| 269 |
-
|
| 270 |
-
async def tts_generate(self, text: str, speaker: int, speaker1: str, speaker2: str) -> str:
|
| 271 |
-
voice = speaker1 if speaker == 1 else speaker2
|
| 272 |
-
speech = edge_tts.Communicate(text, voice)
|
| 273 |
-
|
| 274 |
-
temp_filename = f"temp_{uuid.uuid4()}.wav"
|
| 275 |
-
try:
|
| 276 |
-
await speech.save(temp_filename)
|
| 277 |
-
return temp_filename
|
| 278 |
-
except Exception as e:
|
| 279 |
-
if os.path.exists(temp_filename):
|
| 280 |
-
os.remove(temp_filename)
|
| 281 |
-
raise e
|
| 282 |
-
|
| 283 |
-
async def combine_audio_files(self, audio_files: List[str]) -> str:
|
| 284 |
-
combined_audio = AudioSegment.empty()
|
| 285 |
-
for audio_file in audio_files:
|
| 286 |
-
combined_audio += AudioSegment.from_file(audio_file)
|
| 287 |
-
os.remove(audio_file) # Clean up temporary files
|
| 288 |
-
|
| 289 |
-
output_filename = f"output_{uuid.uuid4()}.wav"
|
| 290 |
-
combined_audio.export(output_filename, format="wav")
|
| 291 |
-
return output_filename
|
| 292 |
-
|
| 293 |
-
async def generate_podcast(self, input_text: str, language: str, speaker1: str, speaker2: str, api_key: str) -> str:
|
| 294 |
-
gr.Info("Generating podcast script...")
|
| 295 |
-
start_time = time.time()
|
| 296 |
-
podcast_json = await self.generate_script(input_text, language, api_key)
|
| 297 |
-
end_time = time.time()
|
| 298 |
-
gr.Info(f"Successfully generated podcast script in {(end_time - start_time):.2f} seconds!")
|
| 299 |
-
|
| 300 |
-
gr.Info("Generating podcast audio files...")
|
| 301 |
-
start_time = time.time()
|
| 302 |
-
audio_files = await asyncio.gather(*[self.tts_generate(item['line'], item['speaker'], speaker1, speaker2) for item in podcast_json['podcast']])
|
| 303 |
-
end_time = time.time()
|
| 304 |
-
gr.Info(f"Successfully generated podcast audio files in {(end_time - start_time):.2f} seconds!")
|
| 305 |
-
|
| 306 |
-
combined_audio = await self.combine_audio_files(audio_files)
|
| 307 |
-
return combined_audio
|
| 308 |
-
|
| 309 |
-
class TextExtractor:
|
| 310 |
-
@staticmethod
|
| 311 |
-
async def extract_from_pdf(file_path: str) -> str:
|
| 312 |
-
async with aiofiles.open(file_path, 'rb') as file:
|
| 313 |
-
content = await file.read()
|
| 314 |
-
pdf_reader = pypdf.PdfReader(io.BytesIO(content))
|
| 315 |
-
return "\n\n".join(page.extract_text() for page in pdf_reader.pages if page.extract_text())
|
| 316 |
-
|
| 317 |
-
@staticmethod
|
| 318 |
-
async def extract_from_txt(file_path: str) -> str:
|
| 319 |
-
async with aiofiles.open(file_path, 'r') as file:
|
| 320 |
-
return await file.read()
|
| 321 |
-
|
| 322 |
-
@classmethod
|
| 323 |
-
async def extract_text(cls, file_path: str) -> str:
|
| 324 |
-
_, file_extension = os.path.splitext(file_path)
|
| 325 |
-
if file_extension.lower() == '.pdf':
|
| 326 |
-
return await cls.extract_from_pdf(file_path)
|
| 327 |
-
elif file_extension.lower() == '.txt':
|
| 328 |
-
return await cls.extract_from_txt(file_path)
|
| 329 |
-
else:
|
| 330 |
-
raise gr.Error(f"Unsupported file type: {file_extension}")
|
| 331 |
-
|
| 332 |
-
async def process_input(input_text: str, input_file, language: str, speaker1: str, speaker2: str, api_key: str = "") -> str:
|
| 333 |
-
gr.Info("Starting podcast generation...")
|
| 334 |
-
start_time = time.time()
|
| 335 |
-
|
| 336 |
-
voice_names = {
|
| 337 |
-
"Andrew - English (United States)": "en-US-AndrewMultilingualNeural",
|
| 338 |
-
"Ava - English (United States)": "en-US-AvaMultilingualNeural",
|
| 339 |
-
"Brian - English (United States)": "en-US-BrianMultilingualNeural",
|
| 340 |
-
"Emma - English (United States)": "en-US-EmmaMultilingualNeural",
|
| 341 |
-
"Florian - German (Germany)": "de-DE-FlorianMultilingualNeural",
|
| 342 |
-
"Seraphina - German (Germany)": "de-DE-SeraphinaMultilingualNeural",
|
| 343 |
-
"Remy - French (France)": "fr-FR-RemyMultilingualNeural",
|
| 344 |
-
"Vivienne - French (France)": "fr-FR-VivienneMultilingualNeural"
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
speaker1 = voice_names[speaker1]
|
| 348 |
-
speaker2 = voice_names[speaker2]
|
| 349 |
-
|
| 350 |
-
if input_file:
|
| 351 |
-
input_text = await TextExtractor.extract_text(input_file.name)
|
| 352 |
-
|
| 353 |
-
if not api_key:
|
| 354 |
-
api_key = os.getenv("GENAI_API_KEY")
|
| 355 |
-
|
| 356 |
-
podcast_generator = PodcastGenerator()
|
| 357 |
-
podcast = await podcast_generator.generate_podcast(input_text, language, speaker1, speaker2, api_key)
|
| 358 |
-
|
| 359 |
-
end_time = time.time()
|
| 360 |
-
gr.Info(f"Successfully generated podcast in {(end_time - start_time):.2f} seconds!")
|
| 361 |
-
|
| 362 |
-
return podcast
|
| 363 |
-
|
| 364 |
-
# Define Gradio interface
|
| 365 |
-
iface = gr.Interface(
|
| 366 |
-
fn=process_input,
|
| 367 |
-
inputs=[
|
| 368 |
-
gr.Textbox(label="Input Text"),
|
| 369 |
-
gr.File(label="Or Upload a PDF or TXT file"),
|
| 370 |
-
gr.Dropdown(label="Language", choices=[
|
| 371 |
-
"Auto Detect",
|
| 372 |
-
"Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian", "Azerbaijani",
|
| 373 |
-
"Bahasa Indonesian", "Bangla", "Basque", "Bengali", "Bosnian", "Bulgarian",
|
| 374 |
-
"Burmese", "Catalan", "Chinese Cantonese", "Chinese Mandarin",
|
| 375 |
-
"Chinese Taiwanese", "Croatian", "Czech", "Danish", "Dutch", "English",
|
| 376 |
-
"Estonian", "Filipino", "Finnish", "French", "Galician", "Georgian",
|
| 377 |
-
"German", "Greek", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Irish",
|
| 378 |
-
"Italian", "Japanese", "Javanese", "Kannada", "Kazakh", "Khmer", "Korean",
|
| 379 |
-
"Lao", "Latvian", "Lithuanian", "Macedonian", "Malay", "Malayalam",
|
| 380 |
-
"Maltese", "Mongolian", "Nepali", "Norwegian Bokmål", "Pashto", "Persian",
|
| 381 |
-
"Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Sinhala",
|
| 382 |
-
"Slovak", "Slovene", "Somali", "Spanish", "Sundanese", "Swahili",
|
| 383 |
-
"Swedish", "Tamil", "Telugu", "Thai", "Turkish", "Ukrainian", "Urdu",
|
| 384 |
-
"Uzbek", "Vietnamese", "Welsh", "Zulu"
|
| 385 |
-
],
|
| 386 |
-
value="Auto Detect"),
|
| 387 |
-
gr.Dropdown(label="Speaker 1 Voice", choices=[
|
| 388 |
-
"Andrew - English (United States)",
|
| 389 |
-
"Ava - English (United States)",
|
| 390 |
-
"Brian - English (United States)",
|
| 391 |
-
"Emma - English (United States)",
|
| 392 |
-
"Florian - German (Germany)",
|
| 393 |
-
"Seraphina - German (Germany)",
|
| 394 |
-
"Remy - French (France)",
|
| 395 |
-
"Vivienne - French (France)"
|
| 396 |
-
],
|
| 397 |
-
value="Andrew - English (United States)"),
|
| 398 |
-
gr.Dropdown(label="Speaker 2 Voice", choices=[
|
| 399 |
-
"Andrew - English (United States)",
|
| 400 |
-
"Ava - English (United States)",
|
| 401 |
-
"Brian - English (United States)",
|
| 402 |
-
"Emma - English (United States)",
|
| 403 |
-
"Florian - German (Germany)",
|
| 404 |
-
"Seraphina - German (Germany)",
|
| 405 |
-
"Remy - French (France)",
|
| 406 |
-
"Vivienne - French (France)"
|
| 407 |
-
],
|
| 408 |
-
value="Ava - English (United States)"),
|
| 409 |
-
gr.Textbox(label="Your Gemini API Key (Optional) - In case you are getting rate limited"),
|
| 410 |
-
],
|
| 411 |
-
outputs=[
|
| 412 |
-
gr.Audio(label="Generated Podcast Audio")
|
| 413 |
-
],
|
| 414 |
-
title="PodcastGen 🎙️",
|
| 415 |
-
description="Generate a 2-speaker podcast from text input or documents!",
|
| 416 |
-
allow_flagging="never"
|
| 417 |
-
)
|
| 418 |
-
|
| 419 |
-
if __name__ == "__main__":
|
| 420 |
-
iface.launch()
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# 財政部財政資訊中心 江信宗
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import openai
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
MODEL = "Meta-Llama-3.1-405B-Instruct"
|
| 9 |
+
|
| 10 |
+
def create_client(api_key=None):
|
| 11 |
+
if api_key:
|
| 12 |
+
openai.api_key = api_key
|
| 13 |
+
else:
|
| 14 |
+
openai.api_key = os.getenv("YOUR_API_TOKEN")
|
| 15 |
+
return openai.OpenAI(api_key=openai.api_key, base_url="https://api.sambanova.ai/v1")
|
| 16 |
+
|
| 17 |
+
def generate_response(input_text):
|
| 18 |
+
system_prompt = """你的任務是將提供的輸入文字轉換為一個引人入勝、訊息豐富且專業的Podcast對話。輸入文字可能會比較混亂或結構不完整,因為它可能來自不同來源,如PDF檔案或文字檔等。不要擔心格式問題或任何不相關的訊息;你的目標是提取可以在Podcast中討論的關鍵點、識別重要定義,並突出有趣的事實。
|
| 19 |
+
|
| 20 |
+
以下是你將要處理的輸入文字:
|
| 21 |
+
<input_text>
|
| 22 |
+
{input_text}
|
| 23 |
+
</input_text>
|
| 24 |
+
|
| 25 |
+
首先,仔細閱讀輸入文字,找出主要話題、關鍵點,以及任何有趣的事實或軼事。思考如何將這些訊息以一種有趣且吸引人的方式呈現出來,適合高質量的音訊Podcast。
|
| 26 |
+
|
| 27 |
+
<scratchpad>
|
| 28 |
+
頭腦風暴一些創造性的方法來討論你在輸入文字中識別出的主要話題、關鍵點及任何有趣的事實或軼事。可以考慮使用類比、講故事技巧或假設情境來讓內容對聽眾更加貼近和有趣。
|
| 29 |
+
|
| 30 |
+
請記住,你的Podcast應當易於普通聽眾理解,所以避免使用過多的專業術語或假設聽眾對該話���已有瞭解。如有必要,請思考如何用簡單的術語簡要解釋任何複雜的概念。
|
| 31 |
+
|
| 32 |
+
利用你的想像力填補輸入文字中的任何空白,或者想出一些值得探討與發人深省的問題,以供Podcast討論。目標是創造一個訊息豐富且娛樂性強的對話,因此可以在你的方法上大膽自由發揮創意。
|
| 33 |
+
|
| 34 |
+
將你的頭腦風暴想法和Podcast對話的粗略大綱寫在這裡。確保記錄下你希望在結尾重申的主要見解和要點。
|
| 35 |
+
</scratchpad>
|
| 36 |
+
|
| 37 |
+
現在你已經進行了頭腦風暴並建立了一個粗略的大綱,是時候撰寫實際的Podcast對話了。目標是主持人(speaker1)與嘉賓(speaker2)之間自然、對話式的交流。融入你在頭腦風暴中得出的最佳想法,並確保將任何複雜話題以易於理解的方式解釋清楚。
|
| 38 |
+
- The podcast should have 2 speakers.
|
| 39 |
+
- Use english names for the speakers.
|
| 40 |
+
- The podcast should be long.
|
| 41 |
+
- The podcast should be interesting, lively, and engaging, and hook the listener from the start.
|
| 42 |
+
- The script must be in JSON format.
|
| 43 |
+
Follow this example structure:
|
| 44 |
+
```
|
| 45 |
+
{
|
| 46 |
+
"speaker1": "歡迎收聽財資歐北共Podcast,我是主持人XXX,今天我們邀請到XX專家XXX,百忙之中來上我們節目",
|
| 47 |
+
"speaker2": "大家好,我是XXX,很榮幸能來跟大家一起聊聊",
|
| 48 |
+
"speaker1": "今天我們將探討一個非常有趣的話題……",
|
| 49 |
+
"speaker2": "沒錯,這個話題確實讓人著迷,讓我們先從……開始說起吧……",
|
| 50 |
+
…………
|
| 51 |
+
"speaker1": "謝謝嘉賓的分享,歡迎訂閱來許願Podcast節目喔,我們下次再見"
|
| 52 |
+
}
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
<podcast_dialogue>
|
| 56 |
+
根據你在頭腦風暴階段提出的關鍵點和創造性想法,撰寫你的引人入勝、訊息豐富的Podcast對話。採用對話式的語氣,並包括任何必要的上下文或解釋,使內容對一般聽眾而言容易理解。使用虛構的主持人和嘉賓名字,以營造更吸引人和身臨其境的聆聽體驗。不要包括像[主持人]或[嘉賓]這樣的括號預留位置。設計你的輸出內容以供直接朗讀——它將直接轉換為音訊。
|
| 57 |
+
|
| 58 |
+
確保對話儘可能詳細、完整,同時保持在主題之內並維持吸引人的流暢性。目標是使用你的全部輸出容量,建立儘可能長的Podcast節目,同時以有趣的方式傳遞輸入文字中的關鍵訊息。
|
| 59 |
+
|
| 60 |
+
在對話結束時,讓主持人和嘉賓自然總結他們討論中的主要見解和要點。這應當是對話的隨機部分,以自然隨意而非明顯的總結——目的是在結束前最後一次以自然流暢的方式強化核心思想。最終以感謝詞結束。
|
| 61 |
+
</podcast_dialogue>
|
| 62 |
+
"""
|
| 63 |
+
client = create_client()
|
| 64 |
+
response = client.chat.completions.create(
|
| 65 |
+
model=MODEL,
|
| 66 |
+
messages=[
|
| 67 |
+
{"role": "system", "content": system_prompt},
|
| 68 |
+
{"role": "user", "content": input_text}
|
| 69 |
+
],
|
| 70 |
+
temperature=1
|
| 71 |
+
)
|
| 72 |
+
return response.choices[0].message.content
|
| 73 |
+
|
| 74 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as iface:
|
| 75 |
+
gr.Markdown("# 🎙️ Generated Podcast Audio. Deployed by 江信宗")
|
| 76 |
+
|
| 77 |
+
input_text = gr.Textbox(label="請輸入您的文字")
|
| 78 |
+
output_text = gr.Textbox(label="生成的結果")
|
| 79 |
+
|
| 80 |
+
generate_button = gr.Button("生成")
|
| 81 |
+
generate_button.click(fn=generate_response, inputs=input_text, outputs=output_text)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
if "SPACE_ID" in os.environ:
|
| 85 |
+
iface.launch()
|
| 86 |
+
else:
|
| 87 |
+
iface.launch(share=True, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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