File size: 1,727 Bytes
4e60a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from langchain_community.llms import HuggingFaceHub
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_community.tools import DuckDuckGoSearchRun
import os


def generate_script(prompt, video_length, creativity):
    """Generates a YouTube video script using a Hugging Face model (no OpenAI)."""

    api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
    if not api_token:
        raise ValueError("❌ Missing HUGGINGFACEHUB_API_TOKEN — please set it in Hugging Face Space Secrets.")

    # ✅ Lightweight, free-tier-compatible model
    llm = HuggingFaceHub(
        repo_id="google/flan-t5-base",
        model_kwargs={
            "temperature": float(creativity),
            "max_length": 512
        }
    )

    title_template = PromptTemplate(
        input_variables=["subject"],
        template="Write a catchy YouTube video title about {subject}."
    )

    script_template = PromptTemplate(
        input_variables=["title", "DuckDuckGo_Search", "duration"],
        template=(
            "Write a full YouTube video script for the title '{title}'. "
            "The video should last about {duration} minutes. "
            "Include relevant details based on this info: {DuckDuckGo_Search}."
        )
    )

    title_chain = LLMChain(llm=llm, prompt=title_template)
    script_chain = LLMChain(llm=llm, prompt=script_template)

    search = DuckDuckGoSearchRun()

    # Generate title and script
    title = title_chain.run(prompt)
    search_result = search.run(prompt)
    script = script_chain.run(title=title, DuckDuckGo_Search=search_result, duration=video_length)

    return search_result, title, script