Spaces:
Sleeping
Sleeping
Download src/utils.py from Dhaerie/YouTube_Script_writer: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/spaces/Dhaerie/YouTube_Script_writer/resolve/main/src/utils.py
- Command line
-
hf download hf://spaces/Dhaerie/YouTube_Script_writer/src/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Dhaerie/YouTube_Script_writer/resolve/main/src/utils.py
1.73 kB
| 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 |