This commit is contained in:
faycal 2025-03-22 00:54:56 +01:00
parent 91a14b231f
commit e0bcef191e
7 changed files with 169 additions and 2 deletions

View File

@ -1,4 +1,4 @@
name: Build and Deploy to Cloud Run
name: Build and Deploy the API to Cloud Run
on:
push:

44
.github/workflows/deploy-function.yml vendored Normal file
View File

@ -0,0 +1,44 @@
name: Deploy Google Cloud Function
on:
push:
branches:
- develop
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r function/requirements.txt
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Deploy Google Cloud Function
run: |
gcloud functions deploy moneyprinterturbo \
--runtime python310 \
--trigger-http \
--entry-point main \
--source function \
--region ${{ secrets.GCP_REGION }} \
--set-env-vars API_URL=${{ secrets.API_URL }},VIDEO_SUBJECT=${{ vars.VIDEO_SUBJECT }},VIDEO_DESCRIPTION=${{ vars.VIDEO_DESCRIPTION }},ACCOUNT_NAME=${{ vars.ACCOUNT_NAME }}, TIME_BEFORE_DOWNLOAD=${{ vars.TIME_BEFORE_DOWNLOAD }}
- name: Upload music.mp3 to Google Cloud Storage
run: |
gsutil cp function/music.mp3 gs://${{ secrets.GCP_BUCKET_NAME }}/music.mp3

View File

@ -0,0 +1,30 @@
on:
push:
branches:
- develop
- main
jobs:
recreate-json:
runs-on: ubuntu-latest
steps:
- name: Checkout du repo
uses: actions/checkout@v3
- name: Recreate JSON file
env:
SECRET_JSON: ${{ secrets.TK_COOKIES_JSON }}
JSON_FILENAME: ${{ vars.JSON_FILENAME }}
run: |
echo "TK_COOKIES_JSON" > function/$JSON_FILENAME
recreate-function:
runs-on: ubuntu-latest
steps:
- name: Checkout du repo
uses: actions/checkout@v3
- name: Recreate function with variables
env:
API_URL: ${{ vars.API_URL }}
run: |
echo "API_URL = '$API_URL'" > function/function.py

3
.gitignore vendored
View File

@ -22,4 +22,5 @@ node_modules
/sites/docs/.vuepress/dist
# 模型目录
/models/
./models/*
./models/*
/function/.venv/

92
function/function.py Normal file
View File

@ -0,0 +1,92 @@
import os
import requests
import json
import time
from tiktokautouploader import upload_tiktok
api_base_url = os.getenv("API_URL")
headers = {"Content-Type": "application/json"}
video_subject = os.getenv("VIDEO_SUBJECT")
video_language = "en-US"
subtitle_font = "STHeitiMedium.ttc"
subtitle_position = "bottom"
subtitle_font_color = "#FFFFFF"
subtitle_outline_color = "#000000"
subtitle_font_size = 60
subtitle_outline_width = 1.5
video_source = "pexels"
video_concat_mode = "random"
video_transition_mode = "None"
video_aspect_ratio = "9:16"
video_clip_duration = 3
video_count = 1
speech_voice = "en-US-GuyNeural-Male"
speech_volume = 1.0
speech_rate = 1.0
song_file_path = "./music.mp3"
bgm_file_path = "../resource/songs/music.mp3"
bgm_volume = 0.2
upload_music_url = f"{api_base_url}/musics"
files = {'file': open(song_file_path, 'rb')}
response = requests.post(upload_music_url, files=files)
bgm_file = response.json()['data']['file']
generate_script_url = f"{api_base_url}/scripts"
script_payload = {
"video_subject": video_subject,
"video_language": video_language,
"paragraph_number": 1
}
response = requests.post(generate_script_url, headers=headers, data=json.dumps(script_payload))
video_script = response.json()['data']['video_script']
generate_keywords_url = f"{api_base_url}/terms"
keywords_payload = {
"video_subject": video_subject,
"video_script": video_script,
"amount": 5
}
response = requests.post(generate_keywords_url, headers=headers, data=json.dumps(keywords_payload))
video_terms = response.json()['data']['video_terms']
generate_video_url = f"{api_base_url}/videos"
video_payload = {
"video_subject": video_subject,
"video_script": video_script,
"video_terms": video_terms,
"video_aspect": video_aspect_ratio,
"video_concat_mode": video_concat_mode,
"video_transition_mode": video_transition_mode,
"video_clip_duration": video_clip_duration,
"video_count": video_count,
"video_source": video_source,
"video_language": video_language,
"voice_name": speech_voice,
"voice_volume": speech_volume,
"voice_rate": speech_rate,
"bgm_type": "custom",
"bgm_file": bgm_file,
"bgm_volume": bgm_volume,
"subtitle_enabled": False,
}
response = requests.post(generate_video_url, headers=headers, data=json.dumps(video_payload))
print(response.json())
task_id = response.json()['data']['task_id']
time.sleep(float(os.getenv("TIME_BEFORE_DOWNLOAD")))
download_url = f"{api_base_url}/download/{task_id}/final-1.mp4"
response = requests.get(download_url, headers=headers)
with open('output_video.mp4', 'wb') as f:
f.write(response.content)
video_path = "output_video.mp4"
description = os.getenv("VIDEO_DESCRIPTION")
account_name = os.getenv("ACCOUNT_NAME")
hashtags = ['fyp']
upload_tiktok(video=video_path, description=description, accountname=account_name)

BIN
function/music.mp3 Normal file

Binary file not shown.

BIN
function/requirements.txt Normal file

Binary file not shown.