import os import requests import gradio as gr import re # === API SETUP === GROQ_API_KEY = "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" GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" HEADERS = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } # === BLOCK INAPPROPRIATE CONTENT === BLOCKED_KEYWORDS = [ "sex", "drugs", "violence", "suicide", "death", "kill", "murder", "gun", "abuse", "politics", "terror", "crime", "war", "religion", "racism", "nude", "adult", "nsfw" ] def is_safe_topic(topic: str) -> bool: topic_lower = topic.lower() return not any(bad_word in topic_lower for bad_word in BLOCKED_KEYWORDS) # === PODCAST GENERATION === def generate_educational_podcast(topic: str, model: str = "llama3-70b-8192") -> str: prompt = f""" You are a scriptwriter for an educational podcast aimed at school students in grades 8 to 12. Create a short and engaging podcast script on the topic: "{topic}". The two hosts are named Ali and Talha. Ali starts the conversation. They should have a back-and-forth conversation about the topic. The tone should be friendly, easy to understand, and informative. Avoid music or sound effects. Keep the total length under 800 words. Only write their dialogue and a few narration lines if needed. Do NOT use Host 1/2 or any generic names. """ data = { "model": model, "messages": [ {"role": "system", "content": "You are a creative and age-appropriate educational podcast writer."}, {"role": "user", "content": prompt.strip()} ], "temperature": 0.75, "max_tokens": 1024 } response = requests.post(GROQ_API_URL, headers=HEADERS, json=data) if response.status_code == 200: return response.json()["choices"][0]["message"]["content"].strip() else: return f"Error {response.status_code}: {response.text}" # === FORMAT SCRIPT CLEANLY FOR DISPLAY === def format_script(script: str) -> str: # Remove markdown bolding like **text** script = re.sub(r'\*\*(.*?)\*\*', r'\1', script) # Remove music and direction lines script = re.sub(r'(?i)^.*(music|sound effect).*$','', script, flags=re.MULTILINE) # Normalize host names to Ali and Talha script = script.replace("Host 1:", "Ali:") script = script.replace("Host 2:", "Talha:") script = re.sub(r'\b[Aa]lex:', 'Ali:', script) script = re.sub(r'\b[Mm]aya:', 'Talha:', script) lines = script.strip().split("\n") formatted = "" for line in lines: line = line.strip() if not line: continue # Only keep lines that are spoken by Ali or Talha if line.startswith("Ali:"): content = line.replace("Ali:", "").strip() formatted += f"