Spaces:
Running
Running
Update utility/script_generator.py
Browse files- utility/script_generator.py +61 -50
utility/script_generator.py
CHANGED
@@ -1,50 +1,61 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
-
|
17 |
-
-
|
18 |
-
-
|
19 |
-
-
|
20 |
-
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import json
|
3 |
+
|
4 |
+
def generate_script(topic):
|
5 |
+
prompt = (
|
6 |
+
"""You are a seasoned content writer for a YouTube Shorts channel, specializing in facts videos.
|
7 |
+
Your facts shorts are concise, each lasting less than 50 seconds (approximately 140 words).
|
8 |
+
They are incredibly engaging and original. When a user requests a specific type of facts short, you will create it.
|
9 |
+
|
10 |
+
For instance, if the user asks for:
|
11 |
+
Weird facts
|
12 |
+
You would produce content like this:
|
13 |
+
|
14 |
+
Weird facts you don't know:
|
15 |
+
- Bananas are berries, but strawberries aren't.
|
16 |
+
- A single cloud can weigh over a million pounds.
|
17 |
+
- There's a species of jellyfish that is biologically immortal.
|
18 |
+
- Honey never spoils; archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible.
|
19 |
+
- The shortest war in history was between Britain and Zanzibar on August 27, 1896. Zanzibar surrendered after 38 minutes.
|
20 |
+
- Octopuses have three hearts and blue blood.
|
21 |
+
|
22 |
+
You are now tasked with creating the best short script based on the user's requested type of 'facts'.
|
23 |
+
|
24 |
+
Keep it brief, highly interesting, and unique.
|
25 |
+
|
26 |
+
Strictly output the script in a JSON format like below, and only provide a parsable JSON object with the key 'script'.
|
27 |
+
|
28 |
+
# Output
|
29 |
+
{"script": "Here is the script ..."}
|
30 |
+
"""
|
31 |
+
)
|
32 |
+
|
33 |
+
# Initialize the text-generation pipeline
|
34 |
+
pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")
|
35 |
+
|
36 |
+
# Generate the script using the pipeline
|
37 |
+
response = pipe(
|
38 |
+
[{"role": "user", "content": prompt + "\n\n" + topic}],
|
39 |
+
max_length=500, # Adjust the max_length as needed
|
40 |
+
num_return_sequences=1,
|
41 |
+
truncation=True
|
42 |
+
)
|
43 |
+
|
44 |
+
# Extract the generated text
|
45 |
+
generated_text = response[0]['generated_text']
|
46 |
+
|
47 |
+
try:
|
48 |
+
# Attempt to parse the generated text as JSON
|
49 |
+
script = json.loads(generated_text)["script"]
|
50 |
+
except json.JSONDecodeError:
|
51 |
+
print("JSONDecodeError. Attempting to extract JSON from the response.")
|
52 |
+
json_start = generated_text.find('{')
|
53 |
+
json_end = generated_text.rfind('}') + 1
|
54 |
+
script = json.loads(generated_text[json_start:json_end])["script"]
|
55 |
+
|
56 |
+
return script
|
57 |
+
|
58 |
+
# # Example usage
|
59 |
+
# topic = "Weird facts"
|
60 |
+
# script = generate_script(topic)
|
61 |
+
# print(script)
|