Spaces:
Sleeping
Sleeping
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"<b>Error {response.status_code}</b>: {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"<div class='narration'>{para}</div>\n" | |
return formatted | |
# === GRADIO UI === | |
custom_css = """ | |
.narration { | |
background: linear-gradient(to right, #434343, #000000); | |
color: #f5f5f5; | |
padding: 20px; | |
margin: 18px 0; | |
border-radius: 12px; | |
font-size: 18px; | |
line-height: 1.8; | |
box-shadow: 0 4px 12px rgba(0,0,0,0.5); | |
font-family: 'Georgia', serif; | |
} | |
""" | |
def generate(topic, language): | |
if not is_safe_topic(topic): | |
return "<b style='color:red;'>⚠️ Restricted content. Please enter a safe, inspirational topic.</b>" | |
raw_script = generate_reflective_script(topic, language) | |
return format_script(raw_script) | |
with gr.Blocks(css=custom_css) as demo: | |
gr.Markdown("## 🌌 Reflective Video Script Generator") | |
gr.Markdown("Enter a thoughtful topic and choose your language to generate a poetic and emotional script for video narration.") | |
topic_input = gr.Textbox(label="Enter a reflective topic (e.g., loneliness, hope, youth...)") | |
lang_input = gr.Dropdown( | |
choices=["English", "Vietnamese", "Spanish", "French", "Japanese"], | |
value="English", | |
label="Select output language" | |
) | |
generate_btn = gr.Button("📝 Generate Script") | |
output_box = gr.HTML() | |
generate_btn.click(fn=generate, inputs=[topic_input, lang_input], outputs=output_box) | |
demo.launch() | |