seawolf2357's picture
Update app.py
96b10cf verified
raw
history blame
2.85 kB
# ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
from langchain_community.llms import HuggingFacePipeline # ์ˆ˜์ •๋œ ์ž„ํฌํŠธ
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
# ๋‚˜๋จธ์ง€ ์ปดํฌ๋„ŒํŠธ ์ž„ํฌํŠธ (์ฝ”๋“œ์— ๋”ฐ๋ผ ๋‹ค๋ฆ„)
from components import caption_chain, tag_chain
from components import pexels, utils
import os, gc
import gradio as gr
# ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
model = AutoModelForSeq2SeqLM.from_pretrained("declare-lab/flan-alpaca-large")
tokenizer = AutoTokenizer.from_pretrained("declare-lab/flan-alpaca-large")
# ํŒŒ์ดํ”„๋ผ์ธ ์„ค์ •
pipe = pipeline(
'text2text-generation',
model=model,
tokenizer=tokenizer,
max_length=120
)
# HuggingFacePipeline์„ ์‚ฌ์šฉํ•˜์—ฌ LLM ์ดˆ๊ธฐํ™”
local_llm = HuggingFacePipeline(pipeline=pipe)
# ์ฒด์ธ ๊ตฌ์„ฑ
llm_chain = caption_chain.chain(llm=local_llm)
sum_llm_chain = tag_chain.chain(llm=local_llm)
# Pexels API ํ‚ค
pexels_api_key = os.getenv('pexels_api_key')
# ์˜ˆ์ธก ํ•จ์ˆ˜
def pred(product_name, orientation):
# ๋น„๋””์˜ค ๋ฐฉํ–ฅ๊ณผ ํ•ด์ƒ๋„ ์„ค์ •
if orientation == "Shorts/Reels/TikTok (1080 x 1920)":
orientation = "portrait" # ์˜คํƒ€ ์ˆ˜์ •
height = 1920
width = 1080
elif orientation == "Youtube Videos (1920 x 1080)":
orientation = "landscape"
height = 1080
width = 1920
else:
orientation = "square"
height = 1080
width = 1080
# ๋น„๋””์˜ค ์ƒ์„ฑ ๋ฐ ๋ฌธ์žฅ ์ถ”์ถœ
folder_name, sentences = pexels.generate_videos(product_name, pexels_api_key, orientation, height, width, llm_chain, sum_llm_chain)
gc.collect()
# ๋น„๋””์˜ค ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
if not sentences:
return ["No videos generated. Please try again.", ""]
# ๋น„๋””์˜ค ํŒŒ์ผ ๊ฒฐํ•ฉ
video_path = utils.combine_videos(folder_name)
if not os.path.exists(video_path):
return ["Failed to combine videos.", ""]
return ["\n".join(sentences), video_path]
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ๋ฐ ๋Ÿฐ์นญ
with gr.Blocks() as demo:
gr.Markdown(
"""
# Ads Generator
Create video ads based on your product name using AI
### Note: the video generation takes about 2-4 minutes
"""
)
dimension = gr.Dropdown(
["Shorts/Reels/TikTok (1080 x 1920)", "Facebook/Youtube Videos (1920 x 1080)", "Square (1080 x 1080)"],
label="Video Dimension", info="Choose dimension"
)
product_name = gr.Textbox(label="Product name")
captions = gr.Label(label="Captions")
video = gr.Video()
btn = gr.Button("Submit")
btn.click(pred, inputs=[product_name, dimension], outputs=[captions, video])
# ์—ฌ๊ธฐ์— ์ถ”๊ฐ€์ ์ธ Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
# Gradio ์•ฑ ์‹คํ–‰
demo.launch()