mirror of
https://github.com/Paillat-dev/viralfactory.git
synced 2026-01-02 17:24:54 +00:00
♻️ ✨ Refactor assets handling, add new AI images engines, add new long form videos pipeline, remove import of shorts pipeline awaiting upgrade to use new code.
This commit is contained in:
26
src/engines/StockImageEngine/BaseStockImageEngine.py
Normal file
26
src/engines/StockImageEngine/BaseStockImageEngine.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import moviepy as mp
|
||||
|
||||
from abc import abstractmethod
|
||||
|
||||
from ..BaseEngine import BaseEngine
|
||||
|
||||
|
||||
class BaseStockImageEngine(BaseEngine):
|
||||
"""
|
||||
The base class for all Stock Image engines.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get(self, query: str, start: float, end: float) -> mp.ImageClip:
|
||||
"""
|
||||
Get a stock image based on a query.
|
||||
|
||||
Args:
|
||||
query (str): The query to search for.
|
||||
start (float): The starting time of the video clip.
|
||||
end (float): The ending time of the video clip.
|
||||
|
||||
Returns:
|
||||
str: The path to the saved image.
|
||||
"""
|
||||
...
|
||||
95
src/engines/StockImageEngine/GoogleStockImageEngine.py
Normal file
95
src/engines/StockImageEngine/GoogleStockImageEngine.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import os
|
||||
import shutil
|
||||
from typing import TypedDict
|
||||
|
||||
import gradio as gr
|
||||
import moviepy as mp
|
||||
import moviepy.video.fx as vfx
|
||||
|
||||
from google_images_search import GoogleImagesSearch
|
||||
from .BaseStockImageEngine import BaseStockImageEngine
|
||||
|
||||
|
||||
class Spec(TypedDict):
|
||||
query: str
|
||||
start: float
|
||||
end: float
|
||||
|
||||
|
||||
class GoogleStockImageEngine(BaseStockImageEngine):
|
||||
name = "Google"
|
||||
description = "Search for images using the Google Images API."
|
||||
|
||||
num_options = 0
|
||||
|
||||
def __init__(self, options: dict):
|
||||
api_key = self.get_setting(type="google_api_key")["api_key"]
|
||||
project_cx = self.get_setting(type="google_project_cx")["project_cx"]
|
||||
self.google = GoogleImagesSearch(api_key, project_cx)
|
||||
super().__init__()
|
||||
|
||||
def get(self, query: str, start: float, end: float) -> mp.ImageClip:
|
||||
max_width = int(self.ctx.width / 3 * 2)
|
||||
_search_params = {
|
||||
"q": query,
|
||||
"num": 1,
|
||||
}
|
||||
os.makedirs("temp", exist_ok=True)
|
||||
try:
|
||||
self.google.search(
|
||||
search_params=_search_params,
|
||||
path_to_dir="./temp/",
|
||||
custom_image_name="temp",
|
||||
)
|
||||
# we find the file called temp. extension
|
||||
filename = [f for f in os.listdir("./temp/") if f.startswith("temp.")][0]
|
||||
img = mp.ImageClip(f"./temp/{filename}")
|
||||
# delete the temp folder
|
||||
except Exception as e:
|
||||
gr.Warning(f"Failed to get image: {e}")
|
||||
return (
|
||||
mp.ColorClip((self.ctx.width, self.ctx.height), color=(0, 0, 0))
|
||||
.with_duration(end - start)
|
||||
.with_start(start)
|
||||
)
|
||||
finally:
|
||||
shutil.rmtree("temp")
|
||||
|
||||
img = (
|
||||
img.with_duration(end - start)
|
||||
.with_start(start)
|
||||
.with_effects([vfx.Resize(width=max_width)])
|
||||
.with_position(("center", "top"))
|
||||
)
|
||||
return img
|
||||
|
||||
@classmethod
|
||||
def get_options(cls):
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def get_settings(cls):
|
||||
current_api_key = cls.get_setting(type="google_api_key")
|
||||
current_api_key = current_api_key["api_key"] if current_api_key else ""
|
||||
api_key_box = gr.Textbox(
|
||||
label="Google API Key",
|
||||
type="password",
|
||||
value=current_api_key,
|
||||
)
|
||||
current_project_cx = cls.get_setting(type="google_project_cx")
|
||||
current_project_cx = (
|
||||
current_project_cx["project_cx"] if current_project_cx else ""
|
||||
)
|
||||
project_cx_box = gr.Textbox(
|
||||
label="Google Project CX",
|
||||
type="password",
|
||||
value=current_project_cx,
|
||||
)
|
||||
submit_button = gr.Button("Save")
|
||||
|
||||
def save_settings(api_key, project_cx):
|
||||
cls.store_setting(type="google_api_key", data={"api_key": api_key})
|
||||
cls.store_setting(type="google_project_cx", data={"project_cx": project_cx})
|
||||
gr.Info("Settings saved successfully")
|
||||
|
||||
submit_button.click(save_settings, inputs=[api_key_box, project_cx_box])
|
||||
2
src/engines/StockImageEngine/__init__.py
Normal file
2
src/engines/StockImageEngine/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .GoogleStockImageEngine import GoogleStockImageEngine
|
||||
from .BaseStockImageEngine import BaseStockImageEngine
|
||||
Reference in New Issue
Block a user