Spaces:
Runtime error
Runtime error
| import os | |
| from openai import OpenAI | |
| import json | |
| from groq import Groq | |
| model = "mixtral-8x7b-32768" | |
| client = Groq( | |
| api_key=os.environ.get("GROQ_API_KEY"), | |
| ) | |
| def generate_script(topic): | |
| prompt = ( | |
| """You are a seasoned content writer for a YouTube Shorts channel, specializing in facts videos. | |
| Your facts shorts are concise, each lasting less than 50 seconds (approximately 140 words). | |
| They are incredibly engaging and original. When a user requests a specific type of facts short, you will create it. | |
| For instance, if the user asks for: | |
| Weird facts | |
| You would produce content like this: | |
| Weird facts you don't know: | |
| - Bananas are berries, but strawberries aren't. | |
| - A single cloud can weigh over a million pounds. | |
| - There's a species of jellyfish that is biologically immortal. | |
| - Honey never spoils; archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible. | |
| - The shortest war in history was between Britain and Zanzibar on August 27, 1896. Zanzibar surrendered after 38 minutes. | |
| - Octopuses have three hearts and blue blood. | |
| You are now tasked with creating the best short script based on the user's requested type of 'facts'. | |
| Keep it brief, highly interesting, and unique. | |
| Stictly output the script in a JSON format like below, and only provide a parsable JSON object with the key 'script'. | |
| # Output | |
| {"script": "Here is the script ..."} | |
| """ | |
| ) | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=[ | |
| {"role": "system", "content": prompt}, | |
| {"role": "user", "content": topic} | |
| ] | |
| ) | |
| content = response.choices[0].message.content | |
| try: | |
| # Basic cleanup of common JSON formatting issues | |
| content = content.strip() | |
| # Parse JSON directly | |
| response_dict = json.loads(content) | |
| script = response_dict["script"] | |
| except Exception as e: | |
| print(f"Error parsing script: {e}") | |
| print("Raw content:", content) | |
| script = "Failed to generate script. Please try again." | |
| return script | |