malvin noel commited on
Commit
b73d328
·
1 Parent(s): d5d7c32

space.gpu corrected

Browse files
scripts/generate_scripts.py CHANGED
@@ -6,10 +6,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
6
  import gradio as gr
7
  from dotenv import load_dotenv
8
  import spaces
9
-
10
-
11
- # Chargement du modèle et du tokenizer
12
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
  import torch
14
  from transformers import AutoModelForCausalLM, AutoTokenizer
15
 
@@ -18,8 +14,10 @@ model_id = "Qwen/Qwen2.5-0.5B"
18
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
19
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, trust_remote_code=True).to(device)
20
 
 
 
21
  def generate_local(prompt: str, max_new_tokens: int = 350, temperature: float = 0.7) -> str:
22
- device = model.device # get the device the model is on
23
  inputs = tokenizer(prompt, return_tensors="pt").to(device)
24
 
25
  output_ids = model.generate(
@@ -32,7 +30,7 @@ def generate_local(prompt: str, max_new_tokens: int = 350, temperature: float =
32
  return tokenizer.decode(output_ids[0], skip_special_tokens=True)
33
 
34
 
35
- @spaces.GPU()
36
  def generate_script(prompt: str, word_count: int = 60) -> str:
37
  system_prompt = (
38
  "You are a professional video scriptwriter. "
@@ -41,7 +39,7 @@ def generate_script(prompt: str, word_count: int = 60) -> str:
41
  )
42
  return generate_local(system_prompt)
43
 
44
- @spaces.GPU()
45
  def one_word(query: str) -> str:
46
  prompt_final = (
47
  "Extract only the unique central theme of the following text in English in JSON format like this: "
@@ -56,7 +54,7 @@ def one_word(query: str) -> str:
56
  keyword = matches[0] if matches else ""
57
  return keyword.lower()
58
 
59
- @spaces.GPU()
60
  def generate_title(text: str) -> str:
61
  prompt_final = (
62
  "Generate a unique title for a YouTube Short video that is engaging and informative, "
@@ -64,7 +62,6 @@ def generate_title(text: str) -> str:
64
  )
65
  return generate_local(prompt_final, max_new_tokens=50, temperature=0.9).strip()
66
 
67
- @spaces.GPU()
68
  def generate_description(text: str) -> str:
69
  prompt_final = (
70
  "Write only the YouTube video description in English:\n"
@@ -75,7 +72,6 @@ def generate_description(text: str) -> str:
75
  )
76
  return generate_local(prompt_final, max_new_tokens=300, temperature=0.7).strip()
77
 
78
- @spaces.GPU()
79
  def generate_tags(text: str) -> list:
80
  prompt_final = (
81
  "List only the important keywords for this YouTube video, separated by commas, "
 
6
  import gradio as gr
7
  from dotenv import load_dotenv
8
  import spaces
 
 
 
 
9
  import torch
10
  from transformers import AutoModelForCausalLM, AutoTokenizer
11
 
 
14
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
15
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, trust_remote_code=True).to(device)
16
 
17
+
18
+ @spaces.GPU()
19
  def generate_local(prompt: str, max_new_tokens: int = 350, temperature: float = 0.7) -> str:
20
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # get the device the model is on
21
  inputs = tokenizer(prompt, return_tensors="pt").to(device)
22
 
23
  output_ids = model.generate(
 
30
  return tokenizer.decode(output_ids[0], skip_special_tokens=True)
31
 
32
 
33
+
34
  def generate_script(prompt: str, word_count: int = 60) -> str:
35
  system_prompt = (
36
  "You are a professional video scriptwriter. "
 
39
  )
40
  return generate_local(system_prompt)
41
 
42
+
43
  def one_word(query: str) -> str:
44
  prompt_final = (
45
  "Extract only the unique central theme of the following text in English in JSON format like this: "
 
54
  keyword = matches[0] if matches else ""
55
  return keyword.lower()
56
 
57
+
58
  def generate_title(text: str) -> str:
59
  prompt_final = (
60
  "Generate a unique title for a YouTube Short video that is engaging and informative, "
 
62
  )
63
  return generate_local(prompt_final, max_new_tokens=50, temperature=0.9).strip()
64
 
 
65
  def generate_description(text: str) -> str:
66
  prompt_final = (
67
  "Write only the YouTube video description in English:\n"
 
72
  )
73
  return generate_local(prompt_final, max_new_tokens=300, temperature=0.7).strip()
74
 
 
75
  def generate_tags(text: str) -> list:
76
  prompt_final = (
77
  "List only the important keywords for this YouTube video, separated by commas, "
scripts/generate_subtitles.py CHANGED
@@ -90,7 +90,7 @@ def transcribe_audio_to_subs(audio_path):
90
  des segments start/end/text, et sauvegarde en .srt.
91
  """
92
  print("🎙️ Transcription avec Whisper...")
93
- model = whisper.load_model("medium", device="cuda" if torch.cuda.is_available() else "cpu")
94
  result = model.transcribe(audio_path)
95
 
96
  subtitles = [{
 
90
  des segments start/end/text, et sauvegarde en .srt.
91
  """
92
  print("🎙️ Transcription avec Whisper...")
93
+ model = whisper.load_model("medium")
94
  result = model.transcribe(audio_path)
95
 
96
  subtitles = [{