public media gpt
This commit is contained in:
@@ -308,7 +308,11 @@ PLUGIN_HOT_RELOAD = False
|
||||
|
||||
# 自定义按钮的最大数量限制
|
||||
NUM_CUSTOM_BASIC_BTN = 4
|
||||
DAAS_SERVER_URL = "http://localhost:48000/stream"
|
||||
|
||||
|
||||
# 媒体智能体的服务地址(这是一个huggingface空间,请前往huggingface复制该空间,然后把自己新的空间地址填在这里)
|
||||
DAAS_SERVER_URL = "https://hamercity-bbdown.hf.space/stream"
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
@@ -30,31 +30,19 @@ class VideoResource(BaseModel):
|
||||
|
||||
|
||||
def get_video_resource(search_keyword):
|
||||
from experimental_mods.get_search_kw_api_stop import search_videos
|
||||
|
||||
# Default parameters for video search
|
||||
csrf_token = '40a227fcf12c380d7d3c81af2cd8c5e8' # Using default from main()
|
||||
search_type = 'default'
|
||||
max_pages = 1
|
||||
output_path = 'search_results'
|
||||
config_path = 'experimental_mods/config.json'
|
||||
from crazy_functions.media_fns.get_media import search_videos
|
||||
|
||||
# Search for videos and return the first result
|
||||
videos = search_videos(
|
||||
keyword=search_keyword,
|
||||
csrf_token=csrf_token,
|
||||
search_type=search_type,
|
||||
max_pages=max_pages,
|
||||
output_path=output_path,
|
||||
config_path=config_path,
|
||||
early_stop=True
|
||||
search_keyword
|
||||
)
|
||||
|
||||
# Return the first video if results exist, otherwise return None
|
||||
return videos
|
||||
|
||||
def download_video(bvid, user_name, chatbot, history):
|
||||
from experimental_mods.get_bilibili_resource import download_bilibili
|
||||
# from experimental_mods.get_bilibili_resource import download_bilibili
|
||||
from crazy_functions.media_fns.get_media import download_video
|
||||
# pause a while
|
||||
tic_time = 8
|
||||
for i in range(tic_time):
|
||||
@@ -64,7 +52,7 @@ def download_video(bvid, user_name, chatbot, history):
|
||||
|
||||
# download audio
|
||||
chatbot.append((None, "下载音频, 请稍等...")); yield from update_ui(chatbot=chatbot, history=history)
|
||||
downloaded_files = yield from download_bilibili(bvid, only_audio=True, user_name=user_name, chatbot=chatbot, history=history)
|
||||
downloaded_files = yield from download_video(bvid, only_audio=True, user_name=user_name, chatbot=chatbot, history=history)
|
||||
|
||||
# preview
|
||||
preview_list = [promote_file_to_downloadzone(fp) for fp in downloaded_files]
|
||||
@@ -81,7 +69,7 @@ def download_video(bvid, user_name, chatbot, history):
|
||||
|
||||
# download video
|
||||
chatbot.append((None, "下载视频, 请稍等...")); yield from update_ui(chatbot=chatbot, history=history)
|
||||
downloaded_files_part2 = yield from download_bilibili(bvid, only_audio=False, user_name=user_name, chatbot=chatbot, history=history)
|
||||
downloaded_files_part2 = yield from download_video(bvid, only_audio=False, user_name=user_name, chatbot=chatbot, history=history)
|
||||
|
||||
# preview
|
||||
preview_list = [promote_file_to_downloadzone(fp) for fp in downloaded_files_part2]
|
||||
|
||||
39
crazy_functions/media_fns/get_media.py
Normal file
39
crazy_functions/media_fns/get_media.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from toolbox import update_ui, get_conf, promote_file_to_downloadzone, update_ui_lastest_msg, generate_file_link
|
||||
from shared_utils.docker_as_service_api import stream_daas
|
||||
from shared_utils.docker_as_service_api import DockerServiceApiComModel
|
||||
|
||||
def download_video(video_id, only_audio, user_name, chatbot, history):
|
||||
from toolbox import get_log_folder
|
||||
chatbot.append([None, "Processing..."])
|
||||
yield from update_ui(chatbot, history)
|
||||
client_command = f'{video_id} --audio-only' if only_audio else video_id
|
||||
server_url = get_conf('DAAS_SERVER_URL')
|
||||
docker_service_api_com_model = DockerServiceApiComModel(client_command=client_command)
|
||||
save_file_dir = get_log_folder(user_name, plugin_name='media_downloader')
|
||||
for output_manifest in stream_daas(docker_service_api_com_model, server_url, save_file_dir):
|
||||
status_buf = ""
|
||||
status_buf += "DaaS message: \n\n"
|
||||
status_buf += output_manifest['server_message'].replace('\n', '<br/>')
|
||||
status_buf += "\n\n"
|
||||
status_buf += "DaaS standard error: \n\n"
|
||||
status_buf += output_manifest['server_std_err'].replace('\n', '<br/>')
|
||||
status_buf += "\n\n"
|
||||
status_buf += "DaaS standard output: \n\n"
|
||||
status_buf += output_manifest['server_std_out'].replace('\n', '<br/>')
|
||||
status_buf += "\n\n"
|
||||
status_buf += "DaaS file attach: \n\n"
|
||||
status_buf += str(output_manifest['server_file_attach'])
|
||||
yield from update_ui_lastest_msg(status_buf, chatbot, history)
|
||||
|
||||
return output_manifest['server_file_attach']
|
||||
|
||||
|
||||
def search_videos(keywords):
|
||||
from toolbox import get_log_folder
|
||||
client_command = keywords
|
||||
server_url = get_conf('DAAS_SERVER_URL').replace('stream', 'search')
|
||||
docker_service_api_com_model = DockerServiceApiComModel(client_command=client_command)
|
||||
save_file_dir = get_log_folder("default_user", plugin_name='media_downloader')
|
||||
for output_manifest in stream_daas(docker_service_api_com_model, server_url, save_file_dir):
|
||||
return output_manifest['server_message']
|
||||
|
||||
@@ -3,13 +3,13 @@ import pickle
|
||||
import io
|
||||
import os
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, Dict
|
||||
from typing import Optional, Dict, Any
|
||||
from loguru import logger
|
||||
|
||||
class DockerServiceApiComModel(BaseModel):
|
||||
client_command: Optional[str] = Field(default=None, title="Client command", description="The command to be executed on the client side")
|
||||
client_file_attach: Optional[dict] = Field(default=None, title="Client file attach", description="The file to be attached to the client side")
|
||||
server_message: Optional[str] = Field(default=None, title="Server standard error", description="The standard error from the server side")
|
||||
server_message: Optional[Any] = Field(default=None, title="Server standard error", description="The standard error from the server side")
|
||||
server_std_err: Optional[str] = Field(default=None, title="Server standard error", description="The standard error from the server side")
|
||||
server_std_out: Optional[str] = Field(default=None, title="Server standard output", description="The standard output from the server side")
|
||||
server_file_attach: Optional[dict] = Field(default=None, title="Server file attach", description="The file to be attached to the server side")
|
||||
@@ -17,7 +17,10 @@ class DockerServiceApiComModel(BaseModel):
|
||||
def process_received(received: DockerServiceApiComModel, save_file_dir="./daas_output", output_manifest=None):
|
||||
# Process the received data
|
||||
if received.server_message:
|
||||
output_manifest['server_message'] += received.server_message
|
||||
try:
|
||||
output_manifest['server_message'] += received.server_message
|
||||
except:
|
||||
output_manifest['server_message'] = received.server_message
|
||||
if received.server_std_err:
|
||||
output_manifest['server_std_err'] += received.server_std_err
|
||||
if received.server_std_out:
|
||||
@@ -59,11 +62,21 @@ def stream_daas(docker_service_api_com_model, server_url, save_file_dir):
|
||||
# Check if the request was successful
|
||||
if response.status_code == 200:
|
||||
# Process the streaming response
|
||||
chunk_buf = None
|
||||
for chunk in response.iter_content(max_full_package_size):
|
||||
if chunk:
|
||||
received = pickle.loads(chunk)
|
||||
received_output_manifest = process_received(received, save_file_dir, output_manifest = received_output_manifest)
|
||||
yield received_output_manifest
|
||||
if chunk_buf is None: chunk_buf = chunk
|
||||
else: chunk_buf += chunk
|
||||
|
||||
try:
|
||||
received = pickle.loads(chunk_buf)
|
||||
chunk_buf = None
|
||||
received_output_manifest = process_received(received, save_file_dir, output_manifest = received_output_manifest)
|
||||
yield received_output_manifest
|
||||
except Exception as e:
|
||||
# logger.error(f"pickle data was truncated, but don't worry, we will continue to receive the rest of the data.")
|
||||
continue
|
||||
|
||||
else:
|
||||
logger.error(f"Error: Received status code {response.status_code}, response.text: {response.text}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user