Spaces:
Runtime error
Runtime error
Update app.py
Browse files78 unmodified lines798081828384201 unmodified lines286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411145 unmodified lines5575585595605615625631 unmodified line5655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945951 unmodified line59759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469523 unmodified lines7197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537544 unmodified lines75976076176276376476576676776814 unmodified lines7837847857867877887897907917924 unmodified lines79779879980080180280380480580680780880981081181281381481581681781881982018 unmodified lines83984084184284384484516 unmodified lines8628638648658668678688698708718728738748759 unmodified lines8858868878888898908918928938948958961 unmodified line89889990090190290390490590690790890991091191291391491591691791891992092110 unmodified lines93293393493593693793893994094194294394494594694794894995095195295395495595695736 unmodified lines9949959969979989991000100110021003100410051006100710081009101044 unmodified lines10551056105710581059106010611062106310641065106610671 unmodified line1069107010711072107310741075107610771078107910805 unmodified lines1086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711185 unmodified lines112411251126112711281129113019 unmodified lines11501151115211531154115511564 unmodified lines1161116211631164116511661167116811691170117152 unmodified lines12241225122612271228122912301231123212331 unmodified line123512361237123812391240124112421243124412451246124712481249125012511252125324 unmodified lines1278127912801281128212831284128512861287128812891290129112926 unmodified lines1299130013011302130313041305130613 unmodified lines1320132113221323132413251326132713281329133013311332133329 unmodified lines13631364136513661367136813695 unmodified lines137513761377137813791380138113825 unmodified lines138813891390139113921393139413956 unmodified lines1402140314041405140614071408140914101411141228 unmodified lines14411442144314441445144614474 unmodified lines145214531454145514561457145814591460146131 unmodified lines149314941495149614971498149924 unmodified lines1524152515261527152815291530153115321533153415351536153715381539154015411542154310 unmodified lines155415551556155715581559156030 unmodified lines159115921593159478 unmodified linesBRAVE_KEY = os.getenv("BSEARCH_API")BRAVE_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"@dataclassclass ConversationConfig:max_words: int = 8000 # 4000에서 6000으로 증가 (1.5배)201 unmodified linesreturn intro + compileddef _build_prompt(self, text: str, language: str = "English", search_context: str = "") -> str:"""Build prompt for conversation generation with enhanced radio talk show style"""# 텍스트 길이 제한max_text_length = 4500 if search_context else 6000if len(text) > max_text_length:text = text[:max_text_length] + "..."if language == "Korean":# 대화 템플릿을 더 많은 턴으로 확장 (15-20회)template = """{"conversation": [{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""}]}"""context_part = ""if search_context:context_part = f"# 최신 관련 정보:\n{search_context}\n"base_prompt = (f"# 원본 콘텐츠:\n{text}\n\n"f"{context_part}"f"위 내용으로 전문적이고 심층적인 라디오 팟캐스트 대담 프로그램 대본을 작성해주세요.\n\n"f"## 필수 요구사항:\n"f"1. **최소 18회 이상의 대화 교환** (준수 9회, 민호 9회 이상)\n"f"2. **대화 스타일**: 전문적이고 깊이 있는 팟캐스트 대담\n"f"3. **화자 역할**:\n"f" - 준수: 진행자 (통찰력 있는 질문, 핵심 포인트 정리, 청취자 관점 대변)\n"f" - 민호: 전문가 (상세하고 전문적인 설명, 구체적 예시, 데이터 기반 분석)\n"f"4. **답변 규칙**:\n"f" - 준수: 1-2문장의 명확한 질문이나 요약\n"f" - 민호: **반드시 2-4문장으로 충실하게 답변** (핵심 개념 설명 + 부연 설명 + 예시/근거)\n"f" - 전문 용어는 쉽게 풀어서 설명\n"f" - 구체적인 수치, 사례, 연구 결과 인용\n"f"5. **내용 구성**:\n"f" - 도입부 (2-3회): 주제의 중요성과 배경 설명\n"f" - 전개부 (12-14회): 핵심 내용을 다각도로 심층 분석\n"f" - 마무리 (2-3회): 핵심 요약과 미래 전망\n"f"6. **전문성**: 학술적 근거와 실무적 통찰을 균형있게 포함\n"f"7. **필수**: 서로 존댓말 사용, 청취자가 전문 지식을 얻을 수 있도록 상세히 설명\n\n"f"반드시 위 JSON 형식으로 18회 이상의 전문적인 대화를 작성하세요:\n{template}")return base_promptelse:# 영어 템플릿도 확장template = """{"conversation": [{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""}]}"""context_part = ""if search_context:context_part = f"# Latest Information:\n{search_context}\n"base_prompt = (f"# Content:\n{text}\n\n"f"{context_part}"f"Create a professional and in-depth podcast conversation.\n\n"f"## Requirements:\n"f"1. **Minimum 18 conversation exchanges** (Alex 9+, Jordan 9+)\n"f"2. **Style**: Professional, insightful podcast discussion\n"f"3. **Roles**:\n"f" - Alex: Host (insightful questions, key point summaries, audience perspective)\n"f" - Jordan: Expert (detailed explanations, concrete examples, data-driven analysis)\n"f"4. **Response Rules**:\n"f" - Alex: 1-2 sentence clear questions or summaries\n"f" - Jordan: **Must answer in 2-4 sentences** (core concept + elaboration + example/evidence)\n"f" - Explain technical terms clearly\n"f" - Include specific data, cases, research findings\n"f"5. **Structure**:\n"f" - Introduction (2-3 exchanges): Topic importance and context\n"f" - Main content (12-14 exchanges): Multi-angle deep analysis\n"f" - Conclusion (2-3 exchanges): Key takeaways and future outlook\n"f"6. **Expertise**: Balance academic rigor with practical insights\n\n"f"Create exactly 18+ professional exchanges in this JSON format:\n{template}")return base_promptclass UnifiedAudioConverter:def __init__(self, config: ConversationConfig):self.config = config145 unmodified lineselse:return MessagesFormatterType.LLAMA_3def _build_prompt(self, text: str, language: str = "English", search_context: str = "") -> str:"""Build prompt for conversation generation with enhanced professional podcast style"""# 텍스트 길이 제한1 unmodified lineif len(text) > max_text_length:text = text[:max_text_length] + "..."if language == "Korean":# 대화 템플릿을 더 많은 턴으로 확장template = """{"conversation": [{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""},{"speaker": "준수", "text": ""},{"speaker": "민호", "text": ""}]}"""context_part = ""if search_context:context_part = f"# 최신 관련 정보:\n{search_context}\n"base_prompt = (f"# 원본 콘텐츠:\n{text}\n\n"f"{context_part}"1 unmodified linef"## 핵심 지침:\n"f"1. **대화 스타일**: 전문적이면서도 이해하기 쉬운 팟캐스트 대담\n"f"2. **화자 역할**:\n"f" - 준수: 진행자/호스트 (핵심을 짚는 질문, 청취자 관점에서 궁금한 점 질문)\n"f" - 민호: 전문가 (깊이 있는 설명, 구체적 사례와 데이터 제시)\n"f"3. **중요한 답변 규칙**:\n"f" - 준수: 1-2문장의 명확한 질문 (\"그렇다면 구체적으로 어떤 의미인가요?\", \"실제 사례를 들어주시겠어요?\")\n"f" - 민호: **반드시 2-4문장으로 충실�� 답변** (개념 설명 + 구체적 설명 + 예시나 함의)\n"f" - 예: \"이것은 ~를 의미합니다. 구체적으로 ~한 측면에서 중요한데요. 실제로 최근 ~한 사례가 있었고, 이는 ~를 보여줍니다.\"\n"f"4. **전문성 요소**:\n"f" - 통계나 연구 결과 인용\n"f" - 실제 사례와 케이스 스터디\n"f" - 전문 용어를 쉽게 풀어서 설명\n"f" - 다양한 관점과 시각 제시\n"f"5. **필수 규칙**: 서로 존댓말 사용, 12-15회 대화 교환\n\n"f"JSON 형식으로만 반환:\n{template}")return base_promptelse:# 영어 템플릿도 확장template = """{"conversation": [{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""},{"speaker": "Alex", "text": ""},{"speaker": "Jordan", "text": ""}]}"""context_part = ""if search_context:context_part = f"# Latest Information:\n{search_context}\n"base_prompt = (f"# Content:\n{text}\n\n"f"{context_part}"f"Create a professional and insightful podcast conversation.\n\n"f"## Key Guidelines:\n"f"1. **Style**: Professional yet accessible podcast discussion\n"f"2. **Roles**:\n"f" - Alex: Host (insightful questions, audience perspective)\n"f" - Jordan: Expert (in-depth explanations, concrete examples and data)\n"f"3. **Critical Response Rules**:\n"f" - Alex: 1-2 sentence clear questions (\"Could you elaborate on that?\", \"What's a real-world example?\")\n"f" - Jordan: **Must answer in 2-4 sentences** (concept + detailed explanation + example/implication)\n"f" - Example: \"This refers to... Specifically, it's important because... For instance, recent studies show... This demonstrates...\"\n"f"4. **Professional Elements**:\n"f" - Cite statistics and research\n"f" - Real cases and case studies\n"f" - Explain technical terms clearly\n"f" - Present multiple perspectives\n"f"5. **Length**: 12-15 exchanges total\n\n"f"Return JSON only:\n{template}")return base_promptdef _build_messages_for_local(self, text: str, language: str = "English", search_context: str = "") -> List[Dict]:"""Build messages for local LLM with enhanced professional podcast style"""if language == "Korean":system_message = ("당신은 한국 최고의 전문 팟캐스트 작가입니다. ""청취자들이 전문 지식을 쉽게 이해할 수 있는 고품질 대담을 만들어냅니다.\n\n""핵심 원칙:\n""1. 진행자(준수)는 핵심을 짚는 통찰력 있는 질문으로 대화를 이끌어갑니다\n""2. 전문가(민호)는 반드시 2-4문��으로 깊이 있게 답변합니다 (개념+설명+예시)\n""3. 구체적인 데이터, 연구 결과, 실제 사례를 포함합니다\n""4. 전문 용어는 쉽게 풀어서 설명하되, 정확성을 유지합니다\n""5. 다양한 관점을 제시하여 균형잡힌 시각을 제공합니다\n""6. 반드시 서로 존댓말을 사용하며, 전문적이면서도 친근한 톤을 유지합니다")else:system_message = ("You are an expert podcast scriptwriter who creates high-quality, ""professional discussions that make complex topics accessible.\n\n""Key principles:\n""1. The host (Alex) asks insightful questions that drive the conversation\n""2. The expert (Jordan) MUST answer in 2-4 sentences (concept+explanation+example)\n""3. Include specific data, research findings, and real cases\n""4. Explain technical terms clearly while maintaining accuracy\n""5. Present multiple perspectives for balanced views\n""6. Maintain a professional yet approachable tone")return [23 unmodified lineschat_template = self._get_messages_formatter_type(self.config.local_model_name)provider = LlamaCppPythonProvider(self.local_llm)# 강화된 전문 팟캐스트 스타일 시스템 메시지if language == "Korean":system_message = ("당신은 한국의 유명 팟캐스트 전문 작가입니다. ""청취자들이 깊이 있는 전문 지식을 얻을 수 있는 고품질 대담을 만듭니다.\n\n""작성 규칙:\n""1. 진행자(준수)는 핵심을 짚는 1-2문장 질문을 합니다\n""2. 전문가(민호)는 반드시 2-4문장으로 충실히 답변합니다:\n"" - 첫 문장: 핵심 개념 설명\n"" - 둘째 문장: 구체적인 설명이나 맥락\n"" - 셋째-넷째 문장: 실제 예시, 데이터, 함의\n""3. 통계, 연구 결과, 실제 사례를 적극 활용하세요\n""4. 전문성을 유지하면서도 이해하기 쉽게 설명하세요\n""5. 12-15회의 대화 교환으로 구성하세요\n""6. JSON 형식으로만 응답하세요")else:system_message = ("You are a professional podcast scriptwriter creating high-quality, ""insightful discussions that provide deep expertise to listeners.\n\n""Writing rules:\n""1. Host (Alex) asks focused 1-2 sentence questions\n""2. Expert (Jordan) MUST answer in 2-4 substantial sentences:\n"" - First sentence: Core concept explanation\n"" - Second sentence: Specific details or context\n"" - Third-fourth sentences: Real examples, data, implications\n""3. Actively use statistics, research findings, real cases\n""4. Maintain expertise while keeping explanations accessible\n""5. Create 12-15 conversation exchanges\n""6. Respond only in JSON format")agent = LlamaCppAgent(4 unmodified lines)settings = provider.get_provider_default_settings()settings.temperature = 0.75 # 약간 낮춰서 더 일관된 전문적 답변settings.top_k = 40settings.top_p = 0.95settings.max_tokens = self.config.max_tokens # 증가된 토큰 수 사용settings.repeat_penalty = 1.1settings.stream = False14 unmodified linesif json_match:conversation_data = json.loads(json_match.group())# 대화 길이 확인 및 조정if len(conversation_data["conversation"]) < self.config.min_conversation_turns:print(f"Conversation too short ({len(conversation_data['conversation'])} turns), regenerating...")# 재시도 로직 추가 가능return conversation_dataelse:raise ValueError("No valid JSON found in local LLM response")4 unmodified lines@spaces.GPU(duration=120)def extract_conversation_legacy_local(self, text: str, language: str = "English", progress=None, search_context: str = "") -> Dict:"""Extract conversation using legacy local model with enhanced professional style"""try:self.initialize_legacy_local_mode()# 강화된 전문 팟캐스트 스타일 시스템 메시지if language == "Korean":system_message = ("당신은 전문 팟캐스트 작가입니다. ""진행자(준수)는 통찰력 있는 질문을, 전문가(민호)는 2-4문장의 상세한 답변을 합니다. ""구체적인 데이터와 사례를 포함하여 전문적이면서도 이해하기 쉽게 설명하세요. ""12-15회 대화 교환으로 구성하세요.")else:system_message = ("You are a professional podcast scriptwriter. ""Create insightful dialogue where the host (Alex) asks focused questions ""and the expert (Jordan) gives detailed 2-4 sentence answers. ""Include specific data and examples. Create 12-15 exchanges.")chat = [18 unmodified linesgenerate_kwargs = dict(model_inputs,streamer=streamer,max_new_tokens=self.config.max_new_tokens, # 증가된 토큰 수 사용do_sample=True,temperature=0.75,eos_token_id=terminators,16 unmodified linesexcept Exception as e:print(f"Legacy local model also failed: {e}")# Return enhanced default templateif language == "Korean":return self._get_default_korean_conversation()else:return self._get_default_english_conversation()def _get_default_korean_conversation(self) -> Dict:"""더 전문적인 기본 한국어 대화 템플릿"""return {"conversation": [{"speaker": "준수", "text": "안녕하세요, 여러분! 오늘은 정말 중요하고 흥미로운 주제를 다뤄보려고 합니다. 민호 박사님, 먼저 이 주제가 왜 지금 이렇게 주목받고 있는지 설명해주시겠어요?"},9 unmodified lines{"speaker": "준수", "text": "실용적인 조언 감사합니다. 마지막으로 이 분야의 미래 전망은 어떻게 보시나요?"},{"speaker": "민호", "text": "향후 10년은 인류 역사상 가장 급격한 기술 발전을 경험하는 시기가 될 것입니다. 가트너의 하이프 사이클 분석에 따르면, 현재 우리는 이 기술의 초기 단계에 불과합니다. 2030년까지는 지금으로서는 상상하기 어려운 수준의 혁신이 일어날 것으로 예상됩니다. 중요한 것은 이런 변화를 두려워하기보다는 기회로 삼아 더 나은 미래를 만들어가는 것이라고 생각합니다."},{"speaker": "준수", "text": "정말 통찰력 있�� 말씀이네요. 오늘 너무나 유익한 시간이었습니다. 청취자 여러분도 오늘 논의된 내용을 바탕으로 미래를 준비하시길 바랍니다. 민호 박사님, 귀중한 시간 내주셔서 감사합니다!"},{"speaker": "민호", "text": "감사합니다. 청취자 여러분들이 이 변화의 시대를 현명하게 헤쳐나가시길 바랍니다. 기술은 도구일 뿐이고, 그것을 어떻게 활용하는지는 우리에게 달려있다는 점을 기억해주세요. 오늘 말씀드린 내용에 대해 더 궁금하신 점이 있으시면 제가 운영하는 블로그나 최근 출간한 책에서 더 자세한 정보를 찾으실 수 있습니다."}]}def _get_default_english_conversation(self) -> Dict:"""Enhanced professional English conversation template"""return {"conversation": [{"speaker": "Alex", "text": "Welcome everyone to our podcast! Today we're diving into a topic that's reshaping our world. Dr. Jordan, could you start by explaining why this subject has become so critical right now?"},1 unmodified line{"speaker": "Alex", "text": "400% acceleration is staggering! What does this mean for everyday people who might not be tech-savvy?"},{"speaker": "Jordan", "text": "The impact will be profound yet accessible. Think about how smartphones revolutionized communication - this will be similar but across every aspect of life. McKinsey's latest report projects that by 2026, these technologies will create $4.4 trillion in annual value globally. For individuals, this translates to personalized healthcare that can predict illnesses years in advance, educational systems that adapt to each student's learning style, and financial tools that democratize wealth-building strategies previously available only to the ultra-wealthy."},{"speaker": "Alex", "text": "Those applications sound transformative. Can you give us a concrete example of how this is already being implemented?"},{"speaker": "Jordan", "text": "Absolutely. Let me share a compelling case from Johns Hopkins Hospital. They've deployed an AI system that analyzes patient data in real-time, reducing diagnostic errors by 85% and cutting average diagnosis time from days to hours. In one documented case, the system identified a rare genetic disorder in a child that had been misdiagnosed for three years. The accuracy comes from analyzing patterns across millions of cases - something impossible for even the most experienced doctors to do manually. This technology is now being rolled out to rural hospitals, bringing world-class diagnostic capabilities to underserved communities."},{"speaker": "Alex", "text": "That's truly life-changing technology. But I imagine there are significant challenges and risks we need to consider?"},{"speaker": "Jordan", "text": "You're absolutely right to raise this. The challenges are as significant as the opportunities. The World Economic Forum identifies three critical risks: First, algorithmic bias could perpetuate or amplify existing inequalities if not carefully managed. Second, cybersecurity threats become exponentially more dangerous when AI systems control critical infrastructure. Third, there's the socioeconomic disruption - PwC estimates that 30% of jobs could be automated by 2030. However, history shows us that technological revolutions create new opportunities even as they displace old ones. The key is proactive adaptation and responsible development."},{"speaker": "Alex", "text": "How should individuals and organizations prepare for these changes?"},{"speaker": "Jordan", "text": "Preparation requires a multi-faceted approach. For individuals, I recommend focusing on skills that complement rather than compete with AI: critical thinking, emotional intelligence, and creative problem-solving. MIT's recent study shows that professionals who combine domain expertise with AI literacy see salary increases of 40% on average. Organizations need to invest in continuous learning programs - Amazon's $700 million worker retraining initiative is a good model. Most importantly, we need to cultivate an adaptive mindset. The half-life of specific technical skills is shrinking, but the ability to learn and unlearn quickly is becoming invaluable."},{"speaker": "Alex", "text": "That's practical advice. What about the ethical considerations? How do we ensure this technology benefits humanity as a whole?"},{"speaker": "Jordan", "text": "Ethics must be at the forefront of development. The EU's AI Act and similar regulations worldwide are establishing important guardrails. We need transparent AI systems where decisions can be explained and audited. Companies like IBM and Google have established AI ethics boards, but we need industry-wide standards. Additionally, we must address the digital divide - UNESCO reports that 37% of the global population still lacks internet access. Without inclusive development, these technologies could exacerbate global inequality rather than reduce it. The solution requires collaboration between technologists, ethicists, policymakers, and communities."},{"speaker": "Alex", "text": "Looking ahead, what's your vision for how this technology will shape the next decade?"},{"speaker": "Jordan", "text": "The next decade will be transformative beyond our current imagination. Ray Kurzweil's prediction of technological singularity by 2045 seems increasingly plausible. By 2035, I expect we'll see autonomous systems managing entire cities, personalized medicine extending human lifespan by 20-30 years, and educational AI that makes world-class education universally accessible. The convergence of AI with quantum computing, biotechnology, and nanotechnology will unlock possibilities we can barely conceive of today. However, the future isn't predetermined - it's shaped by the choices we make now about development priorities, ethical frameworks, and inclusive access."},{"speaker": "Alex", "text": "That's both exciting and sobering. Any final thoughts for our listeners?"},{"speaker": "Jordan", "text": "I'd encourage everyone to view this as humanity's next great adventure. Yes, there are risks and challenges, but we're also on the cusp of solving problems that have plagued us for millennia - disease, poverty, environmental degradation. The key is engaged participation rather than passive observation. Stay informed through reliable sources, experiment with new technologies, and most importantly, contribute to the conversation about what kind of future we want to build. The decisions we make in the next five years will reverberate for generations."},{"speaker": "Alex", "text": "Dr. Jordan, this has been an incredibly enlightening discussion. Thank you for sharing your expertise and insights with us today."},{"speaker": "Jordan", "text": "Thank you, Alex. It's been a pleasure discussing these crucial topics. For listeners wanting to dive deeper, I've compiled additional resources on my website, including links to the studies we discussed today. Remember, the future isn't something that happens to us - it's something we create together. I look forward to seeing how each of you contributes to shaping this exciting new era."}]}def extract_conversation_api(self, text: str, language: str = "English") -> Dict:"""Extract conversation using API with enhanced professional style"""if not self.llm_client:raise RuntimeError("API mode not initialized")10 unmodified linesexcept Exception as e:print(f"Search failed, continuing without context: {e}")# 강화된 전문 팟캐스트 스타일 프롬프트if language == "Korean":system_message = ("당신은 한국의 최고 전문 팟캐스트 작가입니다. ""청취자들이 깊이 있는 인사이트를 얻을 수 있는 고품질 대담을 만드세요.\n""준수(진행자)는 핵심을 짚는 1-2문장 질문을 하고, ""민호(전문가)는 반드시 2-4문장으로 상세히 답변합니다. ""구체적인 데이터, 연구 결과, 실제 사례를 포함하세요. ""전문 용어는 쉽게 설명하고, 반드시 서로 존댓말을 사용하세요. ""12-15회의 깊이 있는 대화 교환으로 구성하세요.")else:system_message = ("You are a top professional podcast scriptwriter. ""Create high-quality discussions that provide deep insights to listeners. ""Alex (host) asks focused 1-2 sentence questions, ""while Jordan (expert) MUST answer in 2-4 detailed sentences. ""Include specific data, research findings, and real cases. ""Explain technical terms clearly. ""Create 12-15 insightful conversation exchanges.")chat_completion = self.llm_client.chat.completions.create(36 unmodified linesfilenames = []try:# 언어별 음성 설정 - 한국어는 모두 남성 음성if language == "Korean":voices = ["ko-KR-HyunsuNeural", # 남성 음성 1 (차분하고 신뢰감 있는)"ko-KR-InJoonNeural" # 남성 음성 2 (활기차고 친근한)]else:voices = ["en-US-AndrewMultilingualNeural", # 남성 음성 1"en-US-BrianMultilingualNeural" # 남성 음성 2]for i, turn in enumerate(conversation_json["conversation"]):filename = output_dir / f"output_{i}.wav"44 unmodified lines# Create different voice characteristics for different speakersif language == "Korean":voice_configs = [{"prompt_text": "안녕하세요, 오늘 팟캐스트 진행을 맡은 준수입니다. 여러분과 함께 흥미로운 이야기를 나눠보겠습니다.", "gender": "male"},{"prompt_text": "안녕하세요, 저는 오늘 이 주제에 대해 설명드릴 민호입니다. 쉽고 재미있게 설명드릴게요.", "gender": "male"}]else:voice_configs = [{"prompt_text": "Hello everyone, I'm Alex, your host for today's podcast. Let's explore this fascinating topic together.", "gender": "male"},{"prompt_text": "Hi, I'm Jordan. I'm excited to share my insights on this subject with you all today.", "gender": "male"}]for i, turn in enumerate(conversation_json["conversation"]):1 unmodified lineif not text.strip():continue# Use different voice config for each speakervoice_config = voice_configs[i % len(voice_configs)]output_file = os.path.join(output_dir, f"spark_output_{i}.wav")# Run Spark TTS CLI inferencecmd = ["python", "-m", "cli.inference","--text", text,5 unmodified lines]try:# Run the commandresult = subprocess.run(cmd,capture_output=True,text=True,timeout=60,cwd="." # Make sure we're in the right directory)if result.returncode == 0:audio_files.append(output_file)else:print(f"Spark TTS error for turn {i}: {result.stderr}")# Create a short silence as fallbacksilence = np.zeros(int(22050 * 1.0)) # 1 second of silencesf.write(output_file, silence, 22050)audio_files.append(output_file)except subprocess.TimeoutExpired:print(f"Spark TTS timeout for turn {i}")# Create silence as fallbacksilence = np.zeros(int(22050 * 1.0))sf.write(output_file, silence, 22050)audio_files.append(output_file)except Exception as e:print(f"Error running Spark TTS for turn {i}: {e}")# Create silence as fallbacksilence = np.zeros(int(22050 * 1.0))sf.write(output_file, silence, 22050)audio_files.append(output_file)5 unmodified lineselse:raise RuntimeError("No audio files generated")# Generate conversation textconversation_text = "\n".join(f"{turn.get('speaker', f'Speaker {i+1}')}: {turn['text']}"for i, turn in enumerate(conversation_json["conversation"])19 unmodified linesspeaker = speakers[i % 2]speaker_id = self.melo_models["EN"].hps.data.spk2id[speaker]# Generate audioself.melo_models["EN"].tts_to_file(text, speaker_id, bio, speed=1.0,pbar=progress.tqdm if progress else None,4 unmodified linesaudio_segment = AudioSegment.from_file(bio, format="wav")combined_audio += audio_segment# Save final audiofinal_audio_path = "melo_podcast.mp3"combined_audio.export(final_audio_path, format="mp3")# Generate conversation textconversation_text = "\n".join(f"{turn.get('speaker', f'Speaker {i+1}')}: {turn['text']}"for i, turn in enumerate(conversation_json["conversation"])52 unmodified lineselse: # Keywordif not article_input or not isinstance(article_input, str):return "Please provide a keyword or topic.", None# 키워드로 검색하여 콘텐츠 생성text = search_and_compile_content(article_input, language)text = f"Keyword-based content:\n{text}" # 마커 추가# Limit text to max wordswords = text.split()if len(words) > converter.config.max_words:1 unmodified line# Extract conversation based on modeif mode == "Local":# 로컬 모드가 기본 (새로운 Local LLM 사용)try:conversation_json = converter.extract_conversation_local(text, language)except Exception as e:print(f"Local mode failed: {e}, trying API fallback")# API 폴백api_key = os.environ.get("TOGETHER_API_KEY")if api_key:converter.initialize_api_mode(api_key)conversation_json = converter.extract_conversation_api(text, language)else:raise RuntimeError("Local mode failed and no API key available for fallback")else: # API mode (now secondary)api_key = os.environ.get("TOGETHER_API_KEY")if not api_key:print("API key not found, falling back to local mode")24 unmodified linesreturn "Please provide conversation text.", Nonetry:# Parse the conversation text back to JSON formatconversation_json = converter.parse_conversation_text(conversation_text)if not conversation_json["conversation"]:return "No valid conversation found in the text.", None# 한국어인 경우 Edge-TTS만 사용 (다른 TTS는 한국어 지원이 제한적)if language == "Korean" and tts_engine != "Edge-TTS":tts_engine = "Edge-TTS" # 자동으로 Edge-TTS로 변경# Generate audio based on TTS engineif tts_engine == "Edge-TTS":6 unmodified lineselse: # MeloTTSif not MELO_AVAILABLE:return "MeloTTS not available. Please install required dependencies.", Noneif language == "Korean":return "MeloTTS does not support Korean. Please use Edge-TTS for Korean.", Noneconverter.initialize_melo_tts()output_file, _ = converter.text_to_speech_melo(conversation_json)13 unmodified linesreturn asyncio.run(regenerate_audio(conversation_text, tts_engine, language))def update_tts_engine_for_korean(language):"""한국어 선택 시 TTS 엔진 옵션 업데이트"""if language == "Korean":return gr.Radio(choices=["Edge-TTS"],value="Edge-TTS",label="TTS Engine",info="한국어는 Edge-TTS만 지원됩니다",interactive=False)else:29 unmodified linesprint(f"Failed to download model at startup: {e}")# Gradio Interface - 개선된 레이아웃with gr.Blocks(theme='soft', title="AI Podcast Generator", css=""".container {max-width: 1200px; margin: auto; padding: 20px;}.header-text {text-align: center; margin-bottom: 30px;}5 unmodified lines# 헤더with gr.Row(elem_classes="header-text"):gr.Markdown("""# 🎙️ AI Podcast Generator - Professional Edition### Convert any article, blog, PDF document, or topic into an engaging professional podcast conversation with in-depth analysis!""")with gr.Row(elem_classes="discord-badge"):5 unmodified lines</p>""")# 상태 표시 섹션with gr.Row():with gr.Column(scale=1):6 unmodified lines""")with gr.Column(scale=1):gr.Markdown("""#### 📻 Podcast Features- **Length**: 12-15 professional exchanges- **Style**: Expert discussions with data & insights- **Languages**: English & Korean (한국어)- **Input**: URL, PDF, or Keywords""")# 메인 입력 섹션28 unmodified lines# 키워드 입력keyword_input = gr.Textbox(label="🔍 Topic/Keyword",placeholder="Enter a topic (e.g., 'AI trends 2024', '인공지능 최신 동향')",value="",visible=False,info="System will search and compile latest information",4 unmodified lineswith gr.Column(scale=1):# 언어 선택language_selector = gr.Radio(choices=["English", "Korean"],value="English",label="🌐 Language / 언어",info="Output language")# 처리 모드31 unmodified lineslines=25,max_lines=50,interactive=True,placeholder="Professional podcast conversation will appear here...\n전문 팟캐스트 대화가 여기에 표시됩니다...",info="Edit the conversation as needed. Format: 'Speaker Name: Text'")24 unmodified linesgr.Markdown("""#### 💡 Quick Tips:- **URL**: Paste any article link- **PDF**: Upload documents directly- **Keyword**: Enter topics for AI research- Edit conversation before audio generation- Korean (한국어) fully supported""")# 예제 섹션with gr.Accordion("📚 Examples", open=False):gr.Examples(examples=[["https://huggingface.co/blog/openfree/cycle-navigator", "URL", "Local", "Edge-TTS", "English"],["quantum computing breakthroughs", "Keyword", "Local", "Edge-TTS", "English"],["https://huggingface.co/papers/2505.14810", "URL", "Local", "Edge-TTS", "Korean"],["인공지능 윤리와 규제", "Keyword", "Local", "Edge-TTS", "Korean"],],inputs=[url_input, input_type_selector, mode_selector, tts_selector, language_selector],outputs=[conversation_output, status_output],10 unmodified lines# 언어 변경 시 TTS 엔진 옵션 업데이트language_selector.change(fn=update_tts_engine_for_korean,inputs=[language_selector],outputs=[tts_selector])30 unmodified linesshare=False,server_name="0.0.0.0",server_port=7860)78 unmodified lines798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196201 unmodified lines398399400401402403145 unmodified lines5495505515525535541 unmodified line5565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136141 unmodified line61661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266323 unmodified lines6876886896906916926936946956966976986997007017027037047054 unmodified lines71071171271371471571671771871914 unmodified lines7347357367377387394 unmodified lines74474574674774874975075175275375475575675775875976076176276318 unmodified lines78278378478578678778816 unmodified lines8058068078088098108118128138148158168178188198208219 unmodified lines8318328338348358368378388398408418421 unmodified line84484584684784884985085185285385485585685785885986086186286386486510 unmodified lines87687787887988088188288388488588688788888989089189289336 unmodified lines93093193293393493593693744 unmodified lines9829839849859869879889899909919929939941 unmodified line996997998999100010011002100310045 unmodified lines101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710385 unmodified lines10441045104610471048104919 unmodified lines1069107010711072107310744 unmodified lines10791080108110821083108410851086108752 unmodified lines1140114111421143114411451146114711481 unmodified line1150115111521153115411551156115711581159116011611162116311641165116624 unmodified lines119111921193119411951196119711981199120012011202120312046 unmodified lines1211121212131214121512161217121813 unmodified lines123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126529 unmodified lines12951296129712981299130013015 unmodified lines130713081309131013111312131313145 unmodified lines1320132113221323132413256 unmodified lines1332133313341335133613371338133913401341134228 unmodified lines13711372137313741375137613774 unmodified lines138213831384138513861387138813891390139113921393139413951396139731 unmodified lines142914301431143214331434143524 unmodified lines14601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148310 unmodified lines149414951496149714981499150030 unmodified lines153115321533153478 unmodified linesBRAVE_KEY = os.getenv("BSEARCH_API")BRAVE_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"# Edge TTS 전용 언어 목록 (English 제외)EDGE_TTS_ONLY_LANGUAGES = ["Korean", "Japanese", "French", "German", "Spanish", "Italian","Portuguese", "Dutch", "Thai", "Vietnamese", "Arabic", "Hebrew","Indonesian", "Hindi", "Russian", "Chinese", "Norwegian", "Swedish","Finnish", "Danish", "Polish", "Turkish", "Greek", "Czech"]# 언어별 Edge TTS 음성 설정EDGE_TTS_VOICES = {"English": ["en-US-AndrewMultilingualNeural", # 남성 음성 1"en-US-BrianMultilingualNeural" # 남성 음성 2],"Korean": ["ko-KR-HyunsuNeural", # 남성 음성 1 (차분하고 신뢰감 있는)"ko-KR-InJoonNeural" # 남성 음성 2 (활기차고 친근한)],"Japanese": ["ja-JP-KeitaNeural", # 남성 음성 1"ja-JP-NanamiNeural" # 여성 음성 (백업용)],"French": ["fr-FR-HenriNeural", # 남성 음성 1"fr-FR-DeniseNeural" # 여성 음성 (백업용)],"German": ["de-DE-ConradNeural", # 남성 음성 1"de-DE-KillianNeural" # 남성 음성 2],"Spanish": ["es-ES-AlvaroNeural", # 남성 음성 1"es-ES-ElviraNeural" # 여성 음성 (백업용)],"Italian": ["it-IT-DiegoNeural", # 남성 음성 1"it-IT-IsabellaNeural" # 여성 음성 (백업용)],"Portuguese": ["pt-BR-AntonioNeural", # 남성 음성 1"pt-BR-FranciscaNeural" # 여성 음성 (백업용)],"Dutch": ["nl-NL-MaartenNeural", # 남성 음성 1"nl-NL-ColetteNeural" # 여성 음성 (백업용)],"Thai": ["th-TH-NiwatNeural", # 남성 음성 1"th-TH-PremwadeeNeural" # 여성 음성 (백업용)],"Vietnamese": ["vi-VN-NamMinhNeural", # 남성 음성 1"vi-VN-HoaiMyNeural" # 여성 음성 (백업용)],"Arabic": ["ar-SA-HamedNeural", # 남성 음성 1"ar-SA-ZariyahNeural" # 여성 음성 (백업용)],"Hebrew": ["he-IL-AvriNeural", # 남성 음성 1"he-IL-HilaNeural" # 여성 음성 (백업용)],"Indonesian": ["id-ID-ArdiNeural", # 남성 음성 1"id-ID-GadisNeural" # 여성 음성 (백업용)],"Hindi": ["hi-IN-MadhurNeural", # 남성 음성 1"hi-IN-SwaraNeural" # 여성 음성 (백업용)],"Russian": ["ru-RU-DmitryNeural", # 남성 음성 1"ru-RU-SvetlanaNeural" # 여성 음성 (백업용)],"Chinese": ["zh-CN-YunxiNeural", # 남성 음성 1"zh-CN-XiaoxiaoNeural" # 여성 음성 (백업용)],"Norwegian": ["nb-NO-FinnNeural", # 남성 음성 1"nb-NO-PernilleNeural" # 여성 음성 (백업용)],"Swedish": ["sv-SE-MattiasNeural", # 남성 음성 1"sv-SE-SofieNeural" # 여성 음성 (백업용)],"Finnish": ["fi-FI-HarriNeural", # 남성 음성 1"fi-FI-NooraNeural" # 여성 음성 (백업용)],"Danish": ["da-DK-JeppeNeural", # 남성 음성 1"da-DK-ChristelNeural" # 여성 음성 (백업용)],"Polish": ["pl-PL-MarekNeural", # 남성 음성 1"pl-PL-ZofiaNeural" # 여성 음성 (백업용)],"Turkish": ["tr-TR-AhmetNeural", # 남성 음성 1"tr-TR-EmelNeural" # 여성 음성 (백업용)],"Greek": ["el-GR-NestorasNeural", # 남성 음성 1"el-GR-AthinaNeural" # 여성 음성 (백업용)],"Czech": ["cs-CZ-AntoninNeural", # 남성 음성 1"cs-CZ-VlastaNeural" # 여성 음성 (백업용)]}@dataclassclass ConversationConfig:max_words: int = 8000 # 4000에서 6000으로 증가 (1.5배)201 unmodified linesreturn intro + compiledclass UnifiedAudioConverter:def __init__(self, config: ConversationConfig):self.config = config145 unmodified lineselse:return MessagesFormatterType.LLAMA_3def _build_prompt(self, text: str, language: str = "English", search_context: str = "") -> str:"""Build prompt for conversation generation with enhanced professional podcast style"""# 텍스트 길이 제한1 unmodified lineif len(text) > max_text_length:text = text[:max_text_length] + "..."# 언어별 화자 이름 설정if language == "Korean":speaker1, speaker2 = "준수", "민호"elif language == "Japanese":speaker1, speaker2 = "Hiroshi", "Takeshi"elif language == "French":speaker1, speaker2 = "Pierre", "Marc"elif language == "German":speaker1, speaker2 = "Klaus", "Stefan"elif language == "Spanish":speaker1, speaker2 = "Carlos", "Miguel"elif language == "Italian":speaker1, speaker2 = "Marco", "Giuseppe"elif language == "Portuguese":speaker1, speaker2 = "João", "Pedro"elif language == "Dutch":speaker1, speaker2 = "Jan", "Pieter"elif language == "Thai":speaker1, speaker2 = "Somchai", "Prasert"elif language == "Vietnamese":speaker1, speaker2 = "Minh", "Duc"elif language == "Arabic":speaker1, speaker2 = "Ahmed", "Mohammed"elif language == "Hebrew":speaker1, speaker2 = "David", "Michael"elif language == "Indonesian":speaker1, speaker2 = "Budi", "Andi"elif language == "Hindi":speaker1, speaker2 = "Raj", "Amit"elif language == "Russian":speaker1, speaker2 = "Alexei", "Dmitri"elif language == "Chinese":speaker1, speaker2 = "Wei", "Jun"else: # English and othersspeaker1, speaker2 = "Alex", "Jordan"# 대화 템플릿 생성template = "{\n \"conversation\": [\n"for i in range(12): # 12번의 교환template += f" {{\"speaker\": \"{speaker1 if i % 2 == 0 else speaker2}\", \"text\": \"\"}}"if i < 11:template += ","template += "\n"template += " ]\n}"context_part = ""if search_context:if language == "Korean":context_part = f"# 최신 관련 정보:\n{search_context}\n"else:context_part = f"# Latest Information:\n{search_context}\n"if language == "Korean":base_prompt = (f"# 원본 콘텐츠:\n{text}\n\n"f"{context_part}"1 unmodified linef"## 핵심 지침:\n"f"1. **대화 스타일**: 전문적이면서도 이해하기 쉬운 팟캐스트 대담\n"f"2. **화자 역할**:\n"f" - {speaker1}: 진행자/호스트 (핵심을 짚는 질문, 청취자 관점에서 궁금한 점 질문)\n"f" - {speaker2}: 전문가 (깊이 있는 설명, 구체적 사례와 데이터 제시)\n"f"3. **중요한 답변 규칙**:\n"f" - {speaker1}: 1-2문장의 명확한 질문\n"f" - {speaker2}: **반드시 2-4문장으로 충실히 답변** (개념 설명 + 구체적 설명 + 예시나 함의)\n"f"4. **전문성 요소**: 통계나 연구 결과 인용, 실제 사례와 케이스 스터디, 전문 용어를 쉽게 풀어서 설명\n"f"5. **필수 규칙**: 서로 존댓말 사용, 12회 대화 교환\n\n"f"JSON 형식으로만 반환:\n{template}")else:base_prompt = (f"# Content:\n{text}\n\n"f"{context_part}"f"Create a professional and insightful podcast conversation in {language}.\n\n"f"## Key Guidelines:\n"f"1. **Style**: Professional yet accessible podcast discussion\n"f"2. **Roles**:\n"f" - {speaker1}: Host (insightful questions, audience perspective)\n"f" - {speaker2}: Expert (in-depth explanations, concrete examples and data)\n"f"3. **Critical Response Rules**:\n"f" - {speaker1}: 1-2 sentence clear questions\n"f" - {speaker2}: **Must answer in 2-4 sentences** (concept + detailed explanation + example/implication)\n"f"4. **Professional Elements**: Cite statistics and research, real cases and case studies, explain technical terms clearly\n"f"5. **Length**: 12 exchanges total\n\n"f"Return JSON only:\n{template}")return base_promptdef _build_messages_for_local(self, text: str, language: str = "English", search_context: str = "") -> List[Dict]:"""Build messages for local LLM with enhanced professional podcast style"""if language == "Korean":system_message = ("당신은 한국 최고의 전문 팟캐스트 작가입니다. ""청취자들이 전문 지식을 쉽게 이해할 수 있는 고품질 대담을 만들어냅니다. ""반드시 서로 존댓말을 사용하며, 전문적이면서도 친근한 톤을 유지합니다.")else:system_message = (f"You are an expert podcast scriptwriter creating high-quality "f"professional discussions in {language}. Make complex topics accessible "f"while maintaining expertise and a professional yet approachable tone.")return [23 unmodified lineschat_template = self._get_messages_formatter_type(self.config.local_model_name)provider = LlamaCppPythonProvider(self.local_llm)# 언어별 시스템 메시지if language == "Korean":system_message = ("당신은 한국의 유명 팟캐스트 전문 작가입니다. ""청취자들이 깊이 있는 전문 지식을 얻을 수 있는 고품질 대담을 만듭니다. ""반드시 서로 존댓말을 사용하며, 12회의 대화 교환으로 구성하세요. ""JSON 형식으로만 응답하세요.")else:system_message = (f"You are a professional podcast scriptwriter creating high-quality, "f"insightful discussions in {language}. Create exactly 12 conversation exchanges "f"with professional expertise. Respond only in JSON format.")agent = LlamaCppAgent(4 unmodified lines)settings = provider.get_provider_default_settings()settings.temperature = 0.75settings.top_k = 40settings.top_p = 0.95settings.max_tokens = self.config.max_tokenssettings.repeat_penalty = 1.1settings.stream = False14 unmodified linesif json_match:conversation_data = json.loads(json_match.group())return conversation_dataelse:raise ValueError("No valid JSON found in local LLM response")4 unmodified lines@spaces.GPU(duration=120)def extract_conversation_legacy_local(self, text: str, language: str = "English", progress=None, search_context: str = "") -> Dict:"""Extract conversation using legacy local model"""try:self.initialize_legacy_local_mode()# 언어별 시스템 메시지if language == "Korean":system_message = ("당신은 전문 팟캐스트 작가입니다. ""12회의 대화 교환으로 구성된 전문적인 대담을 만드세요.")else:system_message = (f"You are a professional podcast scriptwriter. "f"Create a professional dialogue in {language} with 12 exchanges.")chat = [18 unmodified linesgenerate_kwargs = dict(model_inputs,streamer=streamer,max_new_tokens=self.config.max_new_tokens,do_sample=True,temperature=0.75,eos_token_id=terminators,16 unmodified linesexcept Exception as e:print(f"Legacy local model also failed: {e}")return self._get_default_conversation(language)def _get_default_conversation(self, language: str) -> Dict:"""언어별 기본 대화 템플릿"""if language == "Korean":return self._get_default_korean_conversation()else:return self._get_default_english_conversation()def _get_default_korean_conversation(self) -> Dict:"""기본 한국어 대화 템플릿"""return {"conversation": [{"speaker": "준수", "text": "안녕하세요, 여러분! 오늘은 정말 중요하고 흥미로운 주제를 다뤄보려고 합니다. 민호 박사님, 먼저 이 주제가 왜 지금 이렇게 주목받고 있는지 설명해주시겠어요?"},9 unmodified lines{"speaker": "준수", "text": "실용적인 조언 감사합니다. 마지막으로 이 분야의 미래 전망은 어떻게 보시나요?"},{"speaker": "민호", "text": "향후 10년은 인류 역사상 가장 급격한 기술 발전을 경험하는 시기가 될 것입니다. 가트너의 하이프 사이클 분석에 따르면, 현재 우리는 이 기술의 초기 단계에 불과합니다. 2030년까지는 지금으로서는 상상하기 어려운 수준의 혁신이 일어날 것으로 예상됩니다. 중요한 것은 이런 변화를 두려워하기보다는 기회로 삼아 더 나은 미래를 만들어가는 것이라고 생각합니다."},{"speaker": "준수", "text": "정말 통찰력 있�� 말씀이네요. 오늘 너무나 유익한 시간이었습니다. 청취자 여러분도 오늘 논의된 내용을 바탕으로 미래를 준비하시길 바랍니다. 민호 박사님, 귀중한 시간 내주셔서 감사합니다!"},{"speaker": "민호", "text": "감사합니다. 청취자 여러분들이 이 변화의 시대를 현명하게 헤쳐나가시길 바랍니다. 기술은 도구일 뿐이고, 그것을 어떻게 활용하는지는 우리에게 달려있다는 점을 기억해주세요."}]}def _get_default_english_conversation(self) -> Dict:"""기본 영어 대화 템플릿"""return {"conversation": [{"speaker": "Alex", "text": "Welcome everyone to our podcast! Today we're diving into a topic that's reshaping our world. Dr. Jordan, could you start by explaining why this subject has become so critical right now?"},1 unmodified line{"speaker": "Alex", "text": "400% acceleration is staggering! What does this mean for everyday people who might not be tech-savvy?"},{"speaker": "Jordan", "text": "The impact will be profound yet accessible. Think about how smartphones revolutionized communication - this will be similar but across every aspect of life. McKinsey's latest report projects that by 2026, these technologies will create $4.4 trillion in annual value globally. For individuals, this translates to personalized healthcare that can predict illnesses years in advance, educational systems that adapt to each student's learning style, and financial tools that democratize wealth-building strategies previously available only to the ultra-wealthy."},{"speaker": "Alex", "text": "Those applications sound transformative. Can you give us a concrete example of how this is already being implemented?"},{"speaker": "Jordan", "text": "Absolutely. Let me share a compelling case from Johns Hopkins Hospital. They've deployed an AI system that analyzes patient data in real-time, reducing diagnostic errors by 85% and cutting average diagnosis time from days to hours. In one documented case, the system identified a rare genetic disorder in a child that had been misdiagnosed for three years. The accuracy comes from analyzing patterns across millions of cases - something impossible for even the most experienced doctors to do manually."},{"speaker": "Alex", "text": "That's truly life-changing technology. But I imagine there are significant challenges and risks we need to consider?"},{"speaker": "Jordan", "text": "You're absolutely right to raise this. The challenges are as significant as the opportunities. The World Economic Forum identifies three critical risks: algorithmic bias could perpetuate existing inequalities, cybersecurity threats become exponentially more dangerous, and there's the socioeconomic disruption with PwC estimating that 30% of jobs could be automated by 2030. However, history shows us that technological revolutions create new opportunities even as they displace old ones. The key is proactive adaptation and responsible development."},{"speaker": "Alex", "text": "How should individuals and organizations prepare for these changes?"},{"speaker": "Jordan", "text": "Preparation requires a multi-faceted approach. For individuals, I recommend focusing on skills that complement rather than compete with AI: critical thinking, emotional intelligence, and creative problem-solving. MIT's recent study shows that professionals who combine domain expertise with AI literacy see salary increases of 40% on average. Organizations need to invest in continuous learning programs - Amazon's $700 million worker retraining initiative is a good model. Most importantly, we need to cultivate an adaptive mindset."},{"speaker": "Alex", "text": "That's practical advice. What about the ethical considerations? How do we ensure this technology benefits humanity as a whole?"},{"speaker": "Jordan", "text": "Ethics must be at the forefront of development. The EU's AI Act and similar regulations worldwide are establishing important guardrails. We need transparent AI systems where decisions can be explained and audited. Companies like IBM and Google have established AI ethics boards, but we need industry-wide standards. Additionally, we must address the digital divide - UNESCO reports that 37% of the global population still lacks internet access. Without inclusive development, these technologies could exacerbate global inequality."},{"speaker": "Alex", "text": "Looking ahead, what's your vision for how this technology will shape the next decade?"},{"speaker": "Jordan", "text": "The next decade will be transformative beyond our current imagination. By 2035, I expect we'll see autonomous systems managing entire cities, personalized medicine extending human lifespan by 20-30 years, and educational AI that makes world-class education universally accessible. The convergence of AI with quantum computing, biotechnology, and nanotechnology will unlock possibilities we can barely conceive of today. However, the future isn't predetermined - it's shaped by the choices we make now about development priorities and ethical frameworks."},{"speaker": "Alex", "text": "Dr. Jordan, this has been an incredibly enlightening discussion. Thank you for sharing your expertise and insights with us today."},{"speaker": "Jordan", "text": "Thank you, Alex. For listeners wanting to dive deeper, I've compiled additional resources on my website. Remember, the future isn't something that happens to us - it's something we create together. I look forward to seeing how each of you contributes to shaping this exciting new era."}]}def extract_conversation_api(self, text: str, language: str = "English") -> Dict:"""Extract conversation using API"""if not self.llm_client:raise RuntimeError("API mode not initialized")10 unmodified linesexcept Exception as e:print(f"Search failed, continuing without context: {e}")# 언어별 시스템 메시지if language == "Korean":system_message = ("당신은 한국의 최고 전문 팟캐스트 작가입니다. ""12회의 깊이 있는 대화 교환으로 구성된 고품질 대담을 만드세요. ""반드시 서로 존댓말을 사용하세요.")else:system_message = (f"You are a top professional podcast scriptwriter. "f"Create high-quality discussions in {language} with exactly 12 exchanges. "f"Include specific data, research findings, and real cases.")chat_completion = self.llm_client.chat.completions.create(36 unmodified linesfilenames = []try:# 언어별 음성 설정voices = EDGE_TTS_VOICES.get(language, EDGE_TTS_VOICES["English"])for i, turn in enumerate(conversation_json["conversation"]):filename = output_dir / f"output_{i}.wav"44 unmodified lines# Create different voice characteristics for different speakersif language == "Korean":voice_configs = [{"prompt_text": "안녕하세요, 오늘 팟캐스트 진행을 맡은 준수입니다.", "gender": "male"},{"prompt_text": "안녕하세요, 저는 오늘 이 주제에 대해 설명드릴 민호입니다.", "gender": "male"}]else:voice_configs = [{"prompt_text": "Hello everyone, I'm Alex, your host for today's podcast.", "gender": "male"},{"prompt_text": "Hi, I'm Jordan. I'm excited to share my insights with you.", "gender": "male"}]for i, turn in enumerate(conversation_json["conversation"]):1 unmodified lineif not text.strip():continuevoice_config = voice_configs[i % len(voice_configs)]output_file = os.path.join(output_dir, f"spark_output_{i}.wav")cmd = ["python", "-m", "cli.inference","--text", text,5 unmodified lines]try:result = subprocess.run(cmd,capture_output=True,text=True,timeout=60,cwd=".")if result.returncode == 0:audio_files.append(output_file)else:print(f"Spark TTS error for turn {i}: {result.stderr}")silence = np.zeros(int(22050 * 1.0))sf.write(output_file, silence, 22050)audio_files.append(output_file)except subprocess.TimeoutExpired:print(f"Spark TTS timeout for turn {i}")silence = np.zeros(int(22050 * 1.0))sf.write(output_file, silence, 22050)audio_files.append(output_file)except Exception as e:print(f"Error running Spark TTS for turn {i}: {e}")silence = np.zeros(int(22050 * 1.0))sf.write(output_file, silence, 22050)audio_files.append(output_file)5 unmodified lineselse:raise RuntimeError("No audio files generated")conversation_text = "\n".join(f"{turn.get('speaker', f'Speaker {i+1}')}: {turn['text']}"for i, turn in enumerate(conversation_json["conversation"])19 unmodified linesspeaker = speakers[i % 2]speaker_id = self.melo_models["EN"].hps.data.spk2id[speaker]self.melo_models["EN"].tts_to_file(text, speaker_id, bio, speed=1.0,pbar=progress.tqdm if progress else None,4 unmodified linesaudio_segment = AudioSegment.from_file(bio, format="wav")combined_audio += audio_segmentfinal_audio_path = "melo_podcast.mp3"combined_audio.export(final_audio_path, format="mp3")conversation_text = "\n".join(f"{turn.get('speaker', f'Speaker {i+1}')}: {turn['text']}"for i, turn in enumerate(conversation_json["conversation"])52 unmodified lineselse: # Keywordif not article_input or not isinstance(article_input, str):return "Please provide a keyword or topic.", Nonetext = search_and_compile_content(article_input, language)text = f"Keyword-based content:\n{text}"# Limit text to max wordswords = text.split()if len(words) > converter.config.max_words:1 unmodified line# Extract conversation based on modeif mode == "Local":try:conversation_json = converter.extract_conversation_local(text, language)except Exception as e:print(f"Local mode failed: {e}, trying API fallback")api_key = os.environ.get("TOGETHER_API_KEY")if api_key:converter.initialize_api_mode(api_key)conversation_json = converter.extract_conversation_api(text, language)else:raise RuntimeError("Local mode failed and no API key available for fallback")else: # API modeapi_key = os.environ.get("TOGETHER_API_KEY")if not api_key:print("API key not found, falling back to local mode")24 unmodified linesreturn "Please provide conversation text.", Nonetry:conversation_json = converter.parse_conversation_text(conversation_text)if not conversation_json["conversation"]:return "No valid conversation found in the text.", None# Edge TTS 전용 언어는 자동으로 Edge-TTS 사용if language in EDGE_TTS_ONLY_LANGUAGES and tts_engine != "Edge-TTS":tts_engine = "Edge-TTS"# Generate audio based on TTS engineif tts_engine == "Edge-TTS":6 unmodified lineselse: # MeloTTSif not MELO_AVAILABLE:return "MeloTTS not available. Please install required dependencies.", Noneif language in EDGE_TTS_ONLY_LANGUAGES:return f"MeloTTS does not support {language}. Please use Edge-TTS for this language.", Noneconverter.initialize_melo_tts()output_file, _ = converter.text_to_speech_melo(conversation_json)13 unmodified linesreturn asyncio.run(regenerate_audio(conversation_text, tts_engine, language))def update_tts_engine_for_language(language):"""언어별 TTS 엔진 옵션 업데이트"""if language in EDGE_TTS_ONLY_LANGUAGES:language_info = {"Korean": "한국어는 Edge-TTS만 지원됩니다","Japanese": "日本語はEdge-TTSのみサポートされています","French": "Le français n'est pris en charge que par Edge-TTS","German": "Deutsch wird nur von Edge-TTS unterstützt","Spanish": "El español solo es compatible con Edge-TTS","Italian": "L'italiano è supportato solo da Edge-TTS","Portuguese": "O português é suportado apenas pelo Edge-TTS","Dutch": "Nederlands wordt alleen ondersteund door Edge-TTS","Thai": "ภาษาไทยรองรับเฉพาะ Edge-TTS เท่านั้น","Vietnamese": "Tiếng Việt chỉ được hỗ trợ bởi Edge-TTS","Arabic": "العربية مدعومة فقط من Edge-TTS","Hebrew": "עברית נתמכת רק על ידי Edge-TTS","Indonesian": "Bahasa Indonesia hanya didukung oleh Edge-TTS","Hindi": "हिंदी केवल Edge-TTS द्वारा समर्थित है","Russian": "Русский поддерживается только Edge-TTS","Chinese": "中文仅支持Edge-TTS"}info_text = language_info.get(language, f"{language} is only supported by Edge-TTS")return gr.Radio(choices=["Edge-TTS"],value="Edge-TTS",label="TTS Engine",info=info_text,interactive=False)else:29 unmodified linesprint(f"Failed to download model at startup: {e}")# Gradio Interface - 개선된 다국어 레이아웃with gr.Blocks(theme='soft', title="AI Podcast Generator", css=""".container {max-width: 1200px; margin: auto; padding: 20px;}.header-text {text-align: center; margin-bottom: 30px;}5 unmodified lines# 헤더with gr.Row(elem_classes="header-text"):gr.Markdown("""# 🎙️ AI Podcast Generator - Professional Multi-Language Edition### Convert any article, blog, PDF document, or topic into an engaging professional podcast conversation in 24+ languages!""")with gr.Row(elem_classes="discord-badge"):5 unmodified lines</p>""")# 상태 표시 섹션with gr.Row():with gr.Column(scale=1):6 unmodified lines""")with gr.Column(scale=1):gr.Markdown("""#### 🌍 Multi-Language Support- **24+ Languages**: Korean, Japanese, French, German, Spanish, Italian, etc.- **Native Voices**: Optimized for each language- **Professional Style**: Expert discussions with data & insights- **Auto-TTS Selection**: Best engine per language""")# 메인 입력 섹션28 unmodified lines# 키워드 입력keyword_input = gr.Textbox(label="🔍 Topic/Keyword",placeholder="Enter a topic (e.g., 'AI trends 2024', '인공지능', 'IA tendances', 'KI Trends')",value="",visible=False,info="System will search and compile latest information",4 unmodified lineswith gr.Column(scale=1):# 언어 선택language_selector = gr.Radio(choices=["English", "Korean", "Japanese", "French", "German","Spanish", "Italian", "Portuguese", "Dutch", "Thai","Vietnamese", "Arabic", "Hebrew", "Indonesian", "Hindi","Russian", "Chinese", "Norwegian", "Swedish", "Finnish","Danish", "Polish", "Turkish", "Greek", "Czech"],value="English",label="🌐 Language / 언어 / 语言",info="Select podcast language")# 처리 모드31 unmodified lineslines=25,max_lines=50,interactive=True,placeholder="Professional podcast conversation will appear here...\n전문 팟캐스트 대화가 여기에 표시됩니다...\nLa conversation professionnelle du podcast apparaîtra ici...",info="Edit the conversation as needed. Format: 'Speaker Name: Text'")24 unmodified linesgr.Markdown("""#### 💡 Quick Tips:- **URL**: Paste any article link- **PDF**: Upload documents directly- **Keyword**: Enter topics for AI research- **24+ Languages** fully supported- Edit conversation before audio generation- Auto TTS engine selection per language""")# 예제 섹션with gr.Accordion("📚 Multi-Language Examples", open=False):gr.Examples(examples=[["https://huggingface.co/blog/openfreeai/cycle-navigator", "URL", "Local", "Edge-TTS", "English"],["quantum computing breakthroughs", "Keyword", "Local", "Edge-TTS", "English"],["인공지능 윤리와 규제", "Keyword", "Local", "Edge-TTS", "Korean"],["https://huggingface.co/papers/2505.14810", "URL", "Local", "Edge-TTS", "Japanese"],["intelligence artificielle tendances", "Keyword", "Local", "Edge-TTS", "French"],["künstliche intelligenz entwicklung", "Keyword", "Local", "Edge-TTS", "German"],["inteligencia artificial avances", "Keyword", "Local", "Edge-TTS", "Spanish"],],inputs=[url_input, input_type_selector, mode_selector, tts_selector, language_selector],outputs=[conversation_output, status_output],10 unmodified lines# 언어 변경 시 TTS 엔진 옵션 업데이트language_selector.change(fn=update_tts_engine_for_language,inputs=[language_selector],outputs=[tts_selector])30 unmodified linesshare=False,server_name="0.0.0.0",server_port=7860)
app.py+291-351