import os import requests import gradio as gr import re # === API SETUP === GROQ_API_KEY = "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" # Nên dùng biến môi trường trong thực tế GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" HEADERS = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } # === BLOCKED TOPICS === 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) # === REFLECTIVE SCRIPT GENERATION === def generate_reflective_script(topic: str, language: str, model: str = "llama3-70b-8192") -> str: prompt = f""" You are a reflective and thoughtful writer. Write a short, emotional, and thought-provoking script in **{language}** about the topic: "{topic}". The tone should be reflective, poetic, and emotional — suitable for a short narration video. Avoid giving facts or academic details. Instead, focus on emotions, metaphors, inner voice, and human feelings. Use beautiful language, natural rhythm, and pauses. The output should be between 200 to 400 words, formatted as a poetic monologue or a subtle inner conversation. Do NOT include sound effects, scene descriptions, or storytelling instructions. Just output the reflective script. """ data = { "model": model, "messages": [ {"role": "system", "content": "You are a poetic, emotional writer that creates powerful scripts for videos."}, {"role": "user", "content": prompt.strip()} ], "temperature": 0.8, "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}" # === SCRIPT FORMATTING === def format_script(script: str) -> str: paragraphs = script.strip().split("\n") formatted = "" for para in paragraphs: para = para.strip() if para: formatted += f"