Spaces:
Sleeping
Sleeping
Download app.py from Becio/scriptwriter_Atlasium: direct link, hf CLI and curl.
- Browser
- Download file 6.75 kB
-
https://huggingface.co/spaces/Becio/scriptwriter_Atlasium/resolve/main/app.py
- Command line
-
hf download hf://spaces/Becio/scriptwriter_Atlasium/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Becio/scriptwriter_Atlasium/resolve/main/app.py
6.75 kB
| import gradio as gr | |
| from openai import OpenAI | |
| from docx import Document | |
| import os | |
| from serpapi import GoogleSearch | |
| # π Get factual product/manufacturing info via SerpAPI | |
| serpapi_key = "19c7f6d0692ca1fad81b694231567d9acf6b9e5e4ea8133b83828f198120aa12" | |
| def fetch_recent_info(topic): | |
| params = { | |
| "engine": "google", | |
| "q": f"latest news and analysis on {topic} tourism and political impacts", | |
| "api_key": serpapi_key, | |
| "num": 10, | |
| } | |
| search = GoogleSearch(params) | |
| results = search.get_dict() | |
| snippets = [result.get("snippet", "") for result in results.get("organic_results", [])] | |
| return "\n".join(snippets) | |
| # π Load reference style from docx | |
| def load_style_from_docx(filepath): | |
| try: | |
| doc = Document(filepath) | |
| return "\n".join([para.text for para in doc.paragraphs]) | |
| except Exception: | |
| return "" | |
| # π§ Generate full script in channel style | |
| def generate_script(api_key, title, style_file): | |
| if not api_key or not title: | |
| return "β Vul alle verplichte velden in." | |
| try: | |
| client = OpenAI(api_key=api_key) | |
| context = fetch_recent_info(title) | |
| if not context or len(context.strip()) < 100: | |
| return "β οΈ Geen context gevonden. Geef een concreter onderwerp of controleer SerpAPI key." | |
| style_text = load_style_from_docx(style_file.name) if style_file else "Educational and engaging YouTube style focused on industrial processes, machinery, and ingredient transformation." | |
| prompt = f""" | |
| You are a professional YouTube video text writer. | |
| Below is an example YouTube video text that demonstrates the style, tone, pacing, and voice to follow: | |
| --- STYLE INSPIRATION START --- | |
| {style_text} | |
| --- STYLE INSPIRATION END --- | |
| Using this style as inspiration, write a full, complete, well-developed YouTube video text about tourism, travel trends, and political impacts on tourism. The script must be dramatic, mysterious, and engaging, while staying 100% true to the provided factual context. | |
| Expand on every key point, include vivid examples, and avoid summarizing too early. | |
| Divide the text (except intro and ending) into 4β6 chapters. | |
| The last chapter should not have a title like "Outro" or "Conclusion"βjust close the script naturally. | |
| Do not use brackets like [] in the chapter titles. | |
| End the first chapter with a rhetorical question to intrigue the audience. | |
| Keep the introduction (first chapter) under 350 words. | |
| Do not show "Chapter one" or similar; show only the chapter title. | |
| You are not allowed to generate any content that is not explicitly present in the provided context. | |
| Do not infer, assume, or imagine any details. | |
| You must reference the facts from the context directly, and nothing beyond that. | |
| Do not include notes or comments for the editor. Do not reply with anything besides the text. | |
| Write a script for the following title: | |
| "{title}" | |
| Use the following current information for context: | |
| --- START OF FACTUAL CONTEXT --- | |
| {context} | |
| --- END OF FACTUAL CONTEXT --- | |
| Maintain the same storytelling quality, sentence rhythm, and tone as the style sample. | |
| Make the tone of the YouTube video dramatic and mysterious, using rhetorical questions. | |
| Ensure the final script is between 2900 and 3200 words. Less than 2900 is NOT acceptable. | |
| """ | |
| response = client.chat.completions.create( | |
| model="gpt-4.1", | |
| messages=[ | |
| {"role": "system", "content": "You are a professional YouTube scriptwriter."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| raw_script = response.choices[0].message.content | |
| max_retries = 3 | |
| for attempt in range(max_retries): | |
| word_count = len(raw_script.split()) | |
| if word_count >= 2900: | |
| break | |
| extra_prompt = f""" | |
| The following script is {word_count} words long. Expand it to at least 4200 words naturally. | |
| Focus on extending chapters with more detailed steps, transitions, and ingredient breakdowns. | |
| I want you to check all again if it's more than 4200 words. If it's not, extend every chapter until the total gets above 4200 words. | |
| --- SCRIPT TO EXPAND --- | |
| {raw_script} | |
| """ | |
| extra_response = client.chat.completions.create( | |
| model="gpt-4.1", | |
| messages=[ | |
| {"role": "system", "content": "You are a professional scriptwriter."}, | |
| {"role": "user", "content": extra_prompt} | |
| ] | |
| ) | |
| raw_script = extra_response.choices[0].message.content | |
| final_script = factual_review(raw_script, context, client) | |
| return final_script.encode('utf-8').decode('utf-8') | |
| except Exception as e: | |
| return f"β Er ging iets fout: {str(e)}" | |
| # π Fact-checker | |
| def factual_review(script, context, client): | |
| prompt = f""" | |
| Below is a script and the factual context it was based on. | |
| --- CONTEXT --- | |
| {context} | |
| --- SCRIPT --- | |
| {script} | |
| Please review the script for factual consistency: | |
| - Minor elaborations are allowed if logically connected to the context. | |
| - Remove clearly speculative or unverifiable claims. | |
| - Keep dramatic tone and rhetorical questions if they don't create fake facts. | |
| - Ensure the final script is at least 2900 and maximum 3200 words and keeps natural storytelling flow. | |
| - Important: Do not add video editing instructions, title cards, camera directions, or editorial comments. Only return the final spoken script text. | |
| - If the script is less than 2900 words, extend most chapters to reach 2900 words and do the factual review again. | |
| Only rewrite or reword parts that really need it. | |
| """ | |
| response = client.chat.completions.create( | |
| model="gpt-4.1", | |
| messages=[ | |
| {"role": "system", "content": "You are a precise fact-checker and editor."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return response.choices[0].message.content | |
| # ποΈ Gradio app | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π YouTube Factory Scriptwriter β with Word Count & Fact-Check") | |
| with gr.Row(): | |
| api_key = gr.Textbox(label="π OpenAI API Key", type="password") | |
| title = gr.Textbox(label="π― Video Title") | |
| style_file = gr.File(label="π Upload Style Sample (.docx)", file_types=[".docx"], type="filepath") | |
| output = gr.Textbox(label="β Final Script (Fact-Checked)", lines=30, interactive=False) | |
| with gr.Row(): | |
| clear_btn = gr.Button("πͺ Clear") | |
| submit_btn = gr.Button("π Generate Script") | |
| submit_btn.click(fn=generate_script, inputs=[api_key, title, style_file], outputs=output) | |
| clear_btn.click(fn=lambda: "", inputs=None, outputs=output) | |
| if __name__ == "__main__": | |
| demo.launch() |