seawolf2357 commited on
Commit
2242171
โ€ข
1 Parent(s): c55bf8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -33
app.py CHANGED
@@ -1,72 +1,69 @@
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=120
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
- # Ads Generator
56
- Create video ads based on your product name using AI
57
- ### Note : the video generation takes about 2-4 minutes
58
- """
59
- )
60
- dimension = gr.Dropdown(
61
- ["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"],
62
- label="Video Dimension", info="Choose dimension"
63
- )
64
- product_name = gr.Textbox(label="product name")
65
- captions = gr.Textbox(label="captions")
66
- video = gr.Video()
67
- btn = gr.Button("Submit")
68
- btn.click(pred, inputs=[product_name, dimension], outputs=[captions,video])
69
 
70
-
71
-
72
- demo.launch()
 
1
+ # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
2
+ from langchain_community.llms import HuggingFacePipeline # ์ˆ˜์ •๋œ ์ž„ํฌํŠธ
3
  import torch
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
5
 
6
+ # ๋‚˜๋จธ์ง€ ์ปดํฌ๋„ŒํŠธ ์ž„ํฌํŠธ (์ฝ”๋“œ์— ๋”ฐ๋ผ ๋‹ค๋ฆ„)
7
  from components import caption_chain, tag_chain
8
  from components import pexels, utils
9
  import os, gc
10
  import gradio as gr
11
 
12
+ # ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
 
 
 
13
  model = AutoModelForSeq2SeqLM.from_pretrained("declare-lab/flan-alpaca-large")
14
  tokenizer = AutoTokenizer.from_pretrained("declare-lab/flan-alpaca-large")
15
 
16
+ # ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
17
  pipe = pipeline(
18
  'text2text-generation',
19
  model=model,
20
+ tokenizer=tokenizer,
21
  max_length=120
22
  )
23
 
24
+ # HuggingFacePipeline์„ ์‚ฌ์šฉํ•˜์—ฌ LLM ์ดˆ๊ธฐํ™”
25
  local_llm = HuggingFacePipeline(pipeline=pipe)
26
 
27
+ # ์ฒด์ธ ๊ตฌ์„ฑ
28
  llm_chain = caption_chain.chain(llm=local_llm)
29
  sum_llm_chain = tag_chain.chain(llm=local_llm)
30
 
31
+ # Pexels API ํ‚ค
32
  pexels_api_key = os.getenv('pexels_api_key')
33
 
34
+ # ์˜ˆ์ธก ํ•จ์ˆ˜
35
  def pred(product_name, orientation):
36
+ # ๋น„๋””์˜ค ๋ฐฉํ–ฅ๊ณผ ํ•ด์ƒ๋„ ์„ค์ •
37
  if orientation == "Shorts/Reels/TikTok (1080 x 1920)":
38
+ orientation = "portrait" # ์˜คํƒ€ ์ˆ˜์ •
39
  height = 1920
40
  width = 1080
41
  elif orientation == "Youtube Videos (1920 x 1080)":
42
  orientation = "landscape"
43
  height = 1080
44
  width = 1920
45
+ else:
46
  orientation = "square"
47
  height = 1080
48
  width = 1080
49
+
50
+ # ๋น„๋””์˜ค ์ƒ์„ฑ ๋ฐ ๋ฌธ์žฅ ์ถ”์ถœ
51
  folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width, llm_chain, sum_llm_chain)
52
  gc.collect()
 
 
 
 
 
53
 
54
+ # ๋น„๋””์˜ค ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
55
+ if not sentences:
56
+ return ["No videos generated. Please try again.", ""]
57
+
58
+ # ๋น„๋””์˜ค ํŒŒ์ผ ๊ฒฐํ•ฉ
59
+ video_path = utils.combine_videos(folder_name)
60
+ if not os.path.exists(video_path):
61
+ return ["Failed to combine videos.", ""]
62
+
63
+ return ["\n".join(sentences), video_path]
64
+
65
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ๋ฐ ๋Ÿฐ์นญ
66
  with gr.Blocks() as demo:
67
+ # ์ƒ๋žต๋œ Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ demo.launch()