codewithdark commited on
Commit
622dd40
·
verified ·
1 Parent(s): 9c16909

Update utility/script_generator.py

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