Spaces:
Runtime error
Runtime error
Create utility/script /script_generator.py
Browse files
utility/script /script_generator.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
if len(os.environ.get("GROQ_API_KEY")) > 30:
|
| 6 |
+
from groq import Groq
|
| 7 |
+
model = "mixtral-8x7b-32768"
|
| 8 |
+
client = Groq(
|
| 9 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
| 10 |
+
)
|
| 11 |
+
else:
|
| 12 |
+
OPENAI_API_KEY = os.getenv('OPENAI_KEY')
|
| 13 |
+
model = "gpt-4o"
|
| 14 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 15 |
+
|
| 16 |
+
def generate_script(topic):
|
| 17 |
+
prompt = (
|
| 18 |
+
"""You are a seasoned content writer for a YouTube Shorts channel, specializing in facts videos.
|
| 19 |
+
Your facts shorts are concise, each lasting less than 50 seconds (approximately 140 words).
|
| 20 |
+
They are incredibly engaging and original. When a user requests a specific type of facts short, you will create it.
|
| 21 |
+
|
| 22 |
+
For instance, if the user asks for:
|
| 23 |
+
Weird facts
|
| 24 |
+
You would produce content like this:
|
| 25 |
+
|
| 26 |
+
Weird facts you don't know:
|
| 27 |
+
- Bananas are berries, but strawberries aren't.
|
| 28 |
+
- A single cloud can weigh over a million pounds.
|
| 29 |
+
- There's a species of jellyfish that is biologically immortal.
|
| 30 |
+
- Honey never spoils; archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible.
|
| 31 |
+
- The shortest war in history was between Britain and Zanzibar on August 27, 1896. Zanzibar surrendered after 38 minutes.
|
| 32 |
+
- Octopuses have three hearts and blue blood.
|
| 33 |
+
|
| 34 |
+
You are now tasked with creating the best short script based on the user's requested type of 'facts'.
|
| 35 |
+
|
| 36 |
+
Keep it brief, highly interesting, and unique.
|
| 37 |
+
|
| 38 |
+
Stictly output the script in a JSON format like below, and only provide a parsable JSON object with the key 'script'.
|
| 39 |
+
|
| 40 |
+
# Output
|
| 41 |
+
{"script": "Here is the script ..."}
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
response = client.chat.completions.create(
|
| 46 |
+
model=model,
|
| 47 |
+
messages=[
|
| 48 |
+
{"role": "system", "content": prompt},
|
| 49 |
+
{"role": "user", "content": topic}
|
| 50 |
+
]
|
| 51 |
+
)
|
| 52 |
+
content = response.choices[0].message.content
|
| 53 |
+
try:
|
| 54 |
+
script = json.loads(content)["script"]
|
| 55 |
+
except Exception as e:
|
| 56 |
+
json_start_index = content.find('{')
|
| 57 |
+
json_end_index = content.rfind('}')
|
| 58 |
+
print(content)
|
| 59 |
+
content = content[json_start_index:json_end_index+1]
|
| 60 |
+
script = json.loads(content)["script"]
|
| 61 |
+
return script
|