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