From ca35f56f9bc9e17ca8cb3001538d2fadaae048aa Mon Sep 17 00:00:00 2001 From: binary-husky Date: Mon, 11 Nov 2024 14:48:29 +0000 Subject: [PATCH] fix: media gpt upgrade --- crazy_functions/VideoResource_GPT.py | 54 +++++++++++++++++++++++++--- main.py | 2 +- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/crazy_functions/VideoResource_GPT.py b/crazy_functions/VideoResource_GPT.py index 720715f1..3272b6d3 100644 --- a/crazy_functions/VideoResource_GPT.py +++ b/crazy_functions/VideoResource_GPT.py @@ -14,6 +14,7 @@ from request_llms.bridge_all import predict_no_ui_long_connection from crazy_functions.prompts.internet import SearchOptimizerPrompt, SearchAcademicOptimizerPrompt from crazy_functions.json_fns.pydantic_io import GptJsonIO, JsonStringError from textwrap import dedent +from loguru import logger from pydantic import BaseModel, Field class Query(BaseModel): @@ -22,6 +23,7 @@ class Query(BaseModel): # {'title': '【麦笛奈Martine】天文馆的猫 | Diffsinger Lynxnet版本中文试听【Cover 泠鸢·折纸信笺】', 'author': '灰条纹的灰猫君', 'author_id': 2083633, 'bvid': 'BV1LSSHYXEtv', '播放量': 75, '弹幕': 0, '评论': 7, '点赞': 5, '发布时间': '2024-10-31 22:10:08', '视频时长': '4:40', 'tag': '虚拟歌姬,鸟蛋,冷鸟,女声翻唱,泠鸢yousa,虚拟主播,虚拟UP主,VTuber,Diffsinger,虚拟歌手分享官,虚拟之声创作计划·2024第三期', 'description': '翻唱:麦笛奈Martine\n调混:灰条纹的灰猫君\nQ版曲绘:西柚Kanno\n原曲:Av847726911\n原唱:泠鸢\n\n滑铲——月更!\n万圣节快乐!今天是和喵星发电报的可爱猫猫~'} class VideoResource(BaseModel): + thought: str = Field(description="analysis of the search results based on the user's query") title: str = Field(description="title of the video") author: str = Field(description="author/uploader of the video") bvid: str = Field(description="unique ID of the video") @@ -53,6 +55,12 @@ def get_video_resource(search_keyword): def download_video(bvid, user_name, chatbot, history): from experimental_mods.get_bilibili_resource import download_bilibili + # pause a while + tic_time = 8 + for i in range(tic_time): + yield from update_ui_lastest_msg( + lastmsg=f"即将下载音频。等待{tic_time-i}秒后自动继续, 点击“停止”键取消此操作。", + chatbot=chatbot, history=[], delay=1) # download audio chatbot.append((None, "下载音频, 请稍等...")); yield from update_ui(chatbot=chatbot, history=history) @@ -62,6 +70,14 @@ def download_video(bvid, user_name, chatbot, history): preview_list = [promote_file_to_downloadzone(fp) for fp in downloaded_files] file_links = generate_file_link(preview_list) yield from update_ui_lastest_msg(f"已完成的文件:
" + file_links, chatbot=chatbot, history=history, delay=0) + chatbot.append((None, f"即将下载视频。")) + + # pause a while + tic_time = 16 + for i in range(tic_time): + yield from update_ui_lastest_msg( + lastmsg=f"即将下载视频。等待{tic_time-i}秒后自动继续, 点击“停止”键取消此操作。", + chatbot=chatbot, history=[], delay=1) # download video chatbot.append((None, "下载视频, 请稍等...")); yield from update_ui(chatbot=chatbot, history=history) @@ -86,31 +102,59 @@ def 多媒体任务(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_pro rf_req = dedent(f""" The user wish to get the following resource: {user_wish} - Generate reseach keywords accordingly. + Generate reseach keywords (less than 5 keywords) accordingly. """) gpt_json_io = GptJsonIO(Query) inputs = rf_req + gpt_json_io.format_instructions run_gpt_fn = lambda inputs, sys_prompt: predict_no_ui_long_connection(inputs=inputs, llm_kwargs=llm_kwargs, history=[], sys_prompt=sys_prompt, observe_window=[]) analyze_res = run_gpt_fn(inputs, "") + logger.info(analyze_res) query: Query = gpt_json_io.generate_output_auto_repair(analyze_res, run_gpt_fn) + # 关键词展示 + chatbot.append((None, f"检索关键词已确认: {query.search_keyword}。筛选中, 请稍等...")) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + + # 获取候选资源 candadate_dictionary: dict = get_video_resource(query.search_keyword) candadate_dictionary_as_str = json.dumps(candadate_dictionary, ensure_ascii=False, indent=4) - - # 筛选 - chatbot.append((None, f"检索关键词已确认: {query.search_keyword}。筛选中, 请稍等...")) + # 展示候选资源 + candadate_display = "\n".join([f"{i+1}. {it['title']}" for i, it in enumerate(candadate_dictionary)]) + chatbot.append((None, f"候选:\n\n{candadate_display}")) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + # 结构化生成 rf_req_2 = dedent(f""" + The user wish to get the following resource: + {user_wish} + Select the most relevant and suitable video resource from the following search results: {candadate_dictionary_as_str} + + Note: + 1. The first several search video results are more likely to satisfy the user's wish. + 2. The time duration of the video should be less than 10 minutes. + 3. You should analyze the search results first, before giving your answer. + 4. Use Chinese if possible. """) gpt_json_io = GptJsonIO(VideoResource) inputs = rf_req_2 + gpt_json_io.format_instructions run_gpt_fn = lambda inputs, sys_prompt: predict_no_ui_long_connection(inputs=inputs, llm_kwargs=llm_kwargs, history=[], sys_prompt=sys_prompt, observe_window=[]) analyze_res = run_gpt_fn(inputs, "") + logger.info(analyze_res) video_resource: VideoResource = gpt_json_io.generate_output_auto_repair(analyze_res, run_gpt_fn) + # Display + chatbot.append( + (None, + f"分析:{video_resource.thought}" "
" + f"选择: `{video_resource.title}`。" "
" + f"作者:{video_resource.author}" + ) + ) + chatbot.append((None, f"下载中, 请稍等...")) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + if video_resource and video_resource.bvid: - print(video_resource) + logger.info(video_resource) yield from download_video(video_resource.bvid, chatbot.get_user(), chatbot, history) \ No newline at end of file diff --git a/main.py b/main.py index 021681c4..328402ac 100644 --- a/main.py +++ b/main.py @@ -36,7 +36,7 @@ def main(): import gradio as gr if gr.__version__ not in ['3.32.9', '3.32.10', '3.32.11']: raise ModuleNotFoundError("使用项目内置Gradio获取最优体验! 请运行 `pip install -r requirements.txt` 指令安装内置Gradio及其他依赖, 详情信息见requirements.txt.") - + # 一些基础工具 from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith