TDN-M commited on
Commit
34e4015
·
verified ·
1 Parent(s): 2f2db51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -30
app.py CHANGED
@@ -1,68 +1,77 @@
1
- from langchain.llms import HuggingFacePipeline
2
  import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, AutoModelForSeq2SeqLM
4
-
5
- from components import caption_chain, tag_chain
6
  from components import pexels, utils
7
  import os, gc
8
  import gradio as gr
9
 
10
-
11
- # model = AutoModelForSeq2SeqLM.from_pretrained("declare-lab/flan-alpaca-gpt4-xl")
12
- # tokenizer = AutoTokenizer.from_pretrained("declare-lab/flan-alpaca-gpt4-xl")
13
-
14
- model = AutoModelForSeq2SeqLM.from_pretrained("declare-lab/flan-alpaca-large")
15
- tokenizer = AutoTokenizer.from_pretrained("declare-lab/flan-alpaca-large")
16
-
17
  pipe = pipeline(
18
  'text2text-generation',
19
  model=model,
20
- tokenizer= tokenizer,
21
- max_length=256
 
22
  )
23
 
24
- local_llm = HuggingFacePipeline(pipeline=pipe)
25
-
26
- llm_chain = caption_chain.chain(llm=local_llm)
27
- sum_llm_chain = tag_chain.chain(llm=local_llm)
 
28
 
29
- pexels_api_key = os.getenv('pexels_api_key')
 
 
 
 
30
 
 
31
  def pred(product_name, orientation):
32
  if orientation == "Shorts/Reels/TikTok (1080 x 1920)":
33
- orientation = "potrait"
34
  height = 1920
35
  width = 1080
36
  elif orientation == "Youtube Videos (1920 x 1080)":
37
  orientation = "landscape"
38
  height = 1080
39
  width = 1920
40
- else :
41
  orientation = "square"
42
  height = 1080
43
  width = 1080
44
- folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width, llm_chain, sum_llm_chain)
 
 
 
 
 
 
 
 
45
  gc.collect()
 
 
46
  utils.combine_videos(folder_name)
47
- return ["\n".join(sentences), os.path.join(folder_name, "Final_Ad_Video.mp4")]
48
- #{'video':os.path.join(folder_name, "Final_Ad_Video.mp4"),
49
- # 'captions':"\n".join(sentences)}
50
-
51
 
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown(
54
  """
55
- ### Note : Thời gian tạo 1 video là khoảng 3-4 phút
56
  """
57
  )
58
  dimension = gr.Dropdown(
59
- ["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"],
60
- label="Video Dimension", info="Choose dimension"
61
- )
62
  product_name = gr.Textbox(label="Tên Sản Phẩm")
63
  captions = gr.Textbox(label="Chú Thích")
64
  video = gr.Video()
65
  btn = gr.Button("Bắt Đầu Tạo Video")
66
- btn.click(pred, inputs=[product_name, dimension], outputs=[captions,video])
67
 
68
  demo.launch()
 
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
 
 
3
  from components import pexels, utils
4
  import os, gc
5
  import gradio as gr
6
 
7
+ # Load model and tokenizer
8
+ model_name = "google/flan-t5-xxl"
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
11
  pipe = pipeline(
12
  'text2text-generation',
13
  model=model,
14
+ tokenizer=tokenizer,
15
+ max_length=256,
16
+ device_map="auto" # Sử dụng GPU nếu có
17
  )
18
 
19
+ # Function to generate captions
20
+ def generate_captions(product_name):
21
+ template = f"Make 5 different advertisement captions about this product: {product_name}"
22
+ result = pipe(template)
23
+ return result[0]['generated_text']
24
 
25
+ # Function to generate tags
26
+ def generate_tags(sentence):
27
+ template = f"What is the most significant actions or places or things, say it in at most 5 words: {sentence}"
28
+ result = pipe(template)
29
+ return result[0]['generated_text']
30
 
31
+ # Main prediction function
32
  def pred(product_name, orientation):
33
  if orientation == "Shorts/Reels/TikTok (1080 x 1920)":
34
+ orientation = "portrait"
35
  height = 1920
36
  width = 1080
37
  elif orientation == "Youtube Videos (1920 x 1080)":
38
  orientation = "landscape"
39
  height = 1080
40
  width = 1920
41
+ else:
42
  orientation = "square"
43
  height = 1080
44
  width = 1080
45
+
46
+ # Generate captions
47
+ sentences = generate_captions(product_name)
48
+
49
+ # Generate tags
50
+ tags = generate_tags(sentences)
51
+
52
+ # Generate videos using Pexels API
53
+ folder_name = pexels.generate_videos(product_name, os.getenv('pexels_api_key'), orientation, height, width, sentences, tags)
54
  gc.collect()
55
+
56
+ # Combine videos
57
  utils.combine_videos(folder_name)
58
+ return [sentences, os.path.join(folder_name, "Final_Ad_Video.mp4")]
 
 
 
59
 
60
+ # Gradio interface
61
  with gr.Blocks() as demo:
62
  gr.Markdown(
63
  """
64
+ ### Note: Thời gian tạo 1 video là khoảng 3-4 phút
65
  """
66
  )
67
  dimension = gr.Dropdown(
68
+ ["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"],
69
+ label="Video Dimension", info="Choose dimension"
70
+ )
71
  product_name = gr.Textbox(label="Tên Sản Phẩm")
72
  captions = gr.Textbox(label="Chú Thích")
73
  video = gr.Video()
74
  btn = gr.Button("Bắt Đầu Tạo Video")
75
+ btn.click(pred, inputs=[product_name, dimension], outputs=[captions, video])
76
 
77
  demo.launch()