Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app2.py +120 -0
- requirements.txt +4 -0
app2.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
output_dir = "./outputs"
|
| 8 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
groq_client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
| 11 |
+
PEXELS_API_KEY = os.getenv('PEXELS_API_KEY')
|
| 12 |
+
|
| 13 |
+
def translate_prompt(prompt, language):
|
| 14 |
+
if language == "Indonesian":
|
| 15 |
+
translation_prompt = (
|
| 16 |
+
f"Terjemahkan teks berikut ke dalam bahasa Inggris secara singkat dan akurat, cocok untuk pencarian video: {prompt}"
|
| 17 |
+
)
|
| 18 |
+
completion = groq_client.chat.completions.create(
|
| 19 |
+
model="llama-3.3-70b-versatile",
|
| 20 |
+
messages=[{"role": "user", "content": translation_prompt}],
|
| 21 |
+
temperature=0.5,
|
| 22 |
+
max_tokens=8192,
|
| 23 |
+
stream=False
|
| 24 |
+
)
|
| 25 |
+
return completion.choices[0].message.content.strip()
|
| 26 |
+
return prompt
|
| 27 |
+
|
| 28 |
+
def fetch_pexels_video(query):
|
| 29 |
+
headers = {"Authorization": PEXELS_API_KEY}
|
| 30 |
+
url = f"https://api.pexels.com/videos/search?query={query}&per_page=1"
|
| 31 |
+
response = requests.get(url, headers=headers)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
videos = response.json().get("videos", [])
|
| 34 |
+
if videos and videos[0]["video_files"]:
|
| 35 |
+
return videos[0]["video_files"][0]["link"]
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
def save_video(video_url, output_path):
|
| 39 |
+
if not video_url:
|
| 40 |
+
return None
|
| 41 |
+
response = requests.get(video_url)
|
| 42 |
+
if response.status_code == 200:
|
| 43 |
+
with open(output_path, "wb") as f:
|
| 44 |
+
f.write(response.content)
|
| 45 |
+
return output_path
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
def generate_script(topic, language):
|
| 49 |
+
if language == "Indonesian":
|
| 50 |
+
enhanced_prompt = (
|
| 51 |
+
f"Tulis artikel edukasi singkat dalam bahasa Indonesia tentang topik: {topic}. "
|
| 52 |
+
"Artikel harus informatif, terstruktur, dan mudah dipahami dengan: "
|
| 53 |
+
"1) pendahuluan yang memperkenalkan topik, "
|
| 54 |
+
"2) fakta utama atau konsep penting tentang topik, "
|
| 55 |
+
"3) contoh atau konteks nyata untuk memperjelas, "
|
| 56 |
+
"4) penutup yang mendorong pembelajaran lebih lanjut. "
|
| 57 |
+
"Gunakan bahasa yang jelas, formal, dan menarik untuk artikel edukasi."
|
| 58 |
+
)
|
| 59 |
+
else:
|
| 60 |
+
enhanced_prompt = (
|
| 61 |
+
f"Write a short educational article in English about the topic: {topic}. "
|
| 62 |
+
"The article should be informative, structured, and easy to understand with: "
|
| 63 |
+
"1) an introduction introducing the topic, "
|
| 64 |
+
"2) key facts or core concepts about the topic, "
|
| 65 |
+
"3) a real-world example or context to clarify, "
|
| 66 |
+
"4) a conclusion encouraging further learning. "
|
| 67 |
+
"Use clear, formal, and engaging language suitable for an educational article."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
completion = groq_client.chat.completions.create(
|
| 71 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 72 |
+
messages=[{"role": "user", "content": enhanced_prompt}],
|
| 73 |
+
temperature=0.7,
|
| 74 |
+
max_tokens=8192,
|
| 75 |
+
stream=False
|
| 76 |
+
)
|
| 77 |
+
return completion.choices[0].message.content.strip()
|
| 78 |
+
|
| 79 |
+
def save_outputs(script, topic, language):
|
| 80 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 81 |
+
script_filename = os.path.join(output_dir, f"script_{language.lower()}_{timestamp}.txt")
|
| 82 |
+
video_filename = os.path.join(output_dir, f"video_{language.lower()}_{timestamp}.mp4")
|
| 83 |
+
|
| 84 |
+
with open(script_filename, "w", encoding="utf-8") as f:
|
| 85 |
+
f.write(f"Topik: {topic}\nBahasa: {language}\n\n{script}")
|
| 86 |
+
|
| 87 |
+
video_query = translate_prompt(topic, language)
|
| 88 |
+
video_url = fetch_pexels_video(video_query)
|
| 89 |
+
video_path = save_video(video_url, video_filename) if video_url else None
|
| 90 |
+
video_status = f"Video disimpan sebagai {video_filename}" if video_path else "Tidak ada video yang ditemukan untuk topik ini."
|
| 91 |
+
|
| 92 |
+
return script_filename, video_status, video_path
|
| 93 |
+
|
| 94 |
+
def generate_content(topic, language):
|
| 95 |
+
if not topic:
|
| 96 |
+
return "Masukkan topik edukasi.", None, None, None
|
| 97 |
+
script = generate_script(topic, language)
|
| 98 |
+
script_filename, video_status, video_path = save_outputs(script, topic, language)
|
| 99 |
+
return script, script_filename, video_status, video_path
|
| 100 |
+
|
| 101 |
+
interface = gr.Interface(
|
| 102 |
+
fn=generate_content,
|
| 103 |
+
inputs=[
|
| 104 |
+
gr.Textbox(lines=2, placeholder="Enter the story promt here..."),
|
| 105 |
+
gr.Dropdown(choices=["Indonesian", "English"], label="Select Language", value="English")
|
| 106 |
+
],
|
| 107 |
+
outputs=[
|
| 108 |
+
gr.Textbox(label="Generated Article"),
|
| 109 |
+
gr.Textbox(label="The Article has been saved"),
|
| 110 |
+
gr.Textbox(label="The video has been saved"),
|
| 111 |
+
gr.Video(label="Generated Video")
|
| 112 |
+
],
|
| 113 |
+
examples=[
|
| 114 |
+
["Sejarah candi Borobudur", "Indonesian"],
|
| 115 |
+
["The basics of photosynthesis", "English"]
|
| 116 |
+
]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Launch interface
|
| 120 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
groq
|
| 3 |
+
huggingface_hub
|
| 4 |
+
pillow
|