Spaces:
Running
Running
Merge pull request #742 from FutureUnreal/new_branch
Browse files- crazy_functional.py +10 -0
- crazy_functions/图片生成.py +1 -0
- crazy_functions/总结音视频.py +184 -0
- crazy_functions/解析JupyterNotebook.py +1 -0
- crazy_functions/询问多个大语言模型.py +1 -0
crazy_functional.py
CHANGED
@@ -246,5 +246,15 @@ def get_crazy_functions():
|
|
246 |
"Function": HotReload(图片生成)
|
247 |
},
|
248 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
###################### 第n组插件 ###########################
|
250 |
return function_plugins
|
|
|
246 |
"Function": HotReload(图片生成)
|
247 |
},
|
248 |
})
|
249 |
+
from crazy_functions.总结音视频 import 总结音视频
|
250 |
+
function_plugins.update({
|
251 |
+
"批量总结音视频(输入路径或上传压缩包)": {
|
252 |
+
"Color": "stop",
|
253 |
+
"AsButton": False,
|
254 |
+
"AdvancedArgs": True,
|
255 |
+
"ArgsReminder": "调用openai api 使用whisper-1模型, 目前支持的格式:mp4, m4a, wav, mpga, mpeg, mp3。此处可以输入解析提示,例如:解析为简体中文(默认)。",
|
256 |
+
"Function": HotReload(总结音视频)
|
257 |
+
}
|
258 |
+
})
|
259 |
###################### 第n组插件 ###########################
|
260 |
return function_plugins
|
crazy_functions/图片生成.py
CHANGED
@@ -55,6 +55,7 @@ def 图片生成(prompt, llm_kwargs, plugin_kwargs, chatbot, history, system_pro
|
|
55 |
history = [] # 清空历史,以免输入溢出
|
56 |
chatbot.append(("这是什么功能?", "[Local Message] 生成图像, 请先把模型切换至gpt-xxxx或者api2d-xxxx。如果中文效果不理想, 尝试Prompt。正在处理中 ....."))
|
57 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
|
|
58 |
resolution = plugin_kwargs.get("advanced_arg", '256x256')
|
59 |
image_url, image_path = gen_image(llm_kwargs, prompt, resolution)
|
60 |
chatbot.append([prompt,
|
|
|
55 |
history = [] # 清空历史,以免输入溢出
|
56 |
chatbot.append(("这是什么功能?", "[Local Message] 生成图像, 请先把模型切换至gpt-xxxx或者api2d-xxxx。如果中文效果不理想, 尝试Prompt。正在处理中 ....."))
|
57 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
58 |
+
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
59 |
resolution = plugin_kwargs.get("advanced_arg", '256x256')
|
60 |
image_url, image_path = gen_image(llm_kwargs, prompt, resolution)
|
61 |
chatbot.append([prompt,
|
crazy_functions/总结音视频.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from toolbox import CatchException, report_execption, select_api_key, update_ui, write_results_to_file, get_conf
|
2 |
+
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
3 |
+
|
4 |
+
def split_audio_file(filename, split_duration=1000):
|
5 |
+
"""
|
6 |
+
根据给定的切割时长将音频文件切割成多个片段。
|
7 |
+
|
8 |
+
Args:
|
9 |
+
filename (str): 需要被切割的音频文件名。
|
10 |
+
split_duration (int, optional): 每个切割音频片段的时长(以秒为单位)。默认值为1000。
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
filelist (list): 一个包含所有切割音频片段文件路径的列表。
|
14 |
+
|
15 |
+
"""
|
16 |
+
from moviepy.editor import AudioFileClip
|
17 |
+
import os
|
18 |
+
os.makedirs('gpt_log/mp3/cut/', exist_ok=True) # 创建存储切割音频的文件夹
|
19 |
+
|
20 |
+
# 读取音频文件
|
21 |
+
audio = AudioFileClip(filename)
|
22 |
+
|
23 |
+
# 计算文件总时长和切割点
|
24 |
+
total_duration = audio.duration
|
25 |
+
split_points = list(range(0, int(total_duration), split_duration))
|
26 |
+
split_points.append(int(total_duration))
|
27 |
+
filelist = []
|
28 |
+
|
29 |
+
# 切割音频文件
|
30 |
+
for i in range(len(split_points) - 1):
|
31 |
+
start_time = split_points[i]
|
32 |
+
end_time = split_points[i + 1]
|
33 |
+
split_audio = audio.subclip(start_time, end_time)
|
34 |
+
split_audio.write_audiofile(f"gpt_log/mp3/cut/{filename[0]}_{i}.mp3")
|
35 |
+
filelist.append(f"gpt_log/mp3/cut/{filename[0]}_{i}.mp3")
|
36 |
+
|
37 |
+
audio.close()
|
38 |
+
return filelist
|
39 |
+
|
40 |
+
def AnalyAudio(parse_prompt, file_manifest, llm_kwargs, chatbot, history):
|
41 |
+
import os, requests
|
42 |
+
from moviepy.editor import AudioFileClip
|
43 |
+
from request_llm.bridge_all import model_info
|
44 |
+
|
45 |
+
# 设置OpenAI密钥和模型
|
46 |
+
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])
|
47 |
+
chat_endpoint = model_info[llm_kwargs['llm_model']]['endpoint']
|
48 |
+
|
49 |
+
whisper_endpoint = chat_endpoint.replace('chat/completions', 'audio/transcriptions')
|
50 |
+
url = whisper_endpoint
|
51 |
+
headers = {
|
52 |
+
'Authorization': f"Bearer {api_key}"
|
53 |
+
}
|
54 |
+
|
55 |
+
os.makedirs('gpt_log/mp3/', exist_ok=True)
|
56 |
+
for index, fp in enumerate(file_manifest):
|
57 |
+
audio_history = []
|
58 |
+
# 提取文件扩展名
|
59 |
+
ext = os.path.splitext(fp)[1]
|
60 |
+
# 提取视频中的音频
|
61 |
+
if ext not in [".mp3", ".wav", ".m4a", ".mpga"]:
|
62 |
+
audio_clip = AudioFileClip(fp)
|
63 |
+
audio_clip.write_audiofile(f'gpt_log/mp3/output{index}.mp3')
|
64 |
+
fp = f'gpt_log/mp3/output{index}.mp3'
|
65 |
+
# 调用whisper模型音频转文字
|
66 |
+
voice = split_audio_file(fp)
|
67 |
+
for j, i in enumerate(voice):
|
68 |
+
with open(i, 'rb') as f:
|
69 |
+
file_content = f.read() # 读取文件内容到内存
|
70 |
+
files = {
|
71 |
+
'file': (os.path.basename(i), file_content),
|
72 |
+
}
|
73 |
+
data = {
|
74 |
+
"model": "whisper-1",
|
75 |
+
"prompt": parse_prompt,
|
76 |
+
'response_format': "text"
|
77 |
+
}
|
78 |
+
|
79 |
+
chatbot.append([f"将 {i} 发送到openai音频解析终端 (whisper),当前参数:{parse_prompt}", "正在处理 ..."])
|
80 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
81 |
+
proxies, = get_conf('proxies')
|
82 |
+
response = requests.post(url, headers=headers, files=files, data=data, proxies=proxies).text
|
83 |
+
|
84 |
+
chatbot.append(["音频解析结果", response])
|
85 |
+
history.extend(["音频解析结果", response])
|
86 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
87 |
+
|
88 |
+
i_say = f'请对下面的音频片段做概述,音频内容是 ```{response}```'
|
89 |
+
i_say_show_user = f'第{index + 1}段音频的第{j + 1} / {len(voice)}片段。'
|
90 |
+
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
91 |
+
inputs=i_say,
|
92 |
+
inputs_show_user=i_say_show_user,
|
93 |
+
llm_kwargs=llm_kwargs,
|
94 |
+
chatbot=chatbot,
|
95 |
+
history=[],
|
96 |
+
sys_prompt=f"总结音频。音频文件名{fp}"
|
97 |
+
)
|
98 |
+
|
99 |
+
chatbot[-1] = (i_say_show_user, gpt_say)
|
100 |
+
history.extend([i_say_show_user, gpt_say])
|
101 |
+
audio_history.extend([i_say_show_user, gpt_say])
|
102 |
+
|
103 |
+
# 已经对该文章的所有片段总结完毕,如果文章被切分了
|
104 |
+
result = "".join(audio_history)
|
105 |
+
if len(audio_history) > 1:
|
106 |
+
i_say = f"根据以上的对话,使用中文总结音频“{result}”的主要内容。"
|
107 |
+
i_say_show_user = f'第{index + 1}段音频的主要内容:'
|
108 |
+
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
109 |
+
inputs=i_say,
|
110 |
+
inputs_show_user=i_say_show_user,
|
111 |
+
llm_kwargs=llm_kwargs,
|
112 |
+
chatbot=chatbot,
|
113 |
+
history=audio_history,
|
114 |
+
sys_prompt="总结文章。"
|
115 |
+
)
|
116 |
+
|
117 |
+
history.extend([i_say, gpt_say])
|
118 |
+
audio_history.extend([i_say, gpt_say])
|
119 |
+
|
120 |
+
res = write_results_to_file(history)
|
121 |
+
chatbot.append((f"第{index + 1}段音频完成了吗?", res))
|
122 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
123 |
+
|
124 |
+
# 删除中间文件夹
|
125 |
+
import shutil
|
126 |
+
shutil.rmtree('gpt_log/mp3')
|
127 |
+
res = write_results_to_file(history)
|
128 |
+
chatbot.append(("所有音频都总结完成了吗?", res))
|
129 |
+
yield from update_ui(chatbot=chatbot, history=history)
|
130 |
+
|
131 |
+
|
132 |
+
@CatchException
|
133 |
+
def 总结音视频(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, WEB_PORT):
|
134 |
+
import glob, os
|
135 |
+
|
136 |
+
# 基本信息:功能、贡献者
|
137 |
+
chatbot.append([
|
138 |
+
"函数插件功能?",
|
139 |
+
"总结音视频内容,函数插件贡献者: dalvqw & BinaryHusky"])
|
140 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
141 |
+
|
142 |
+
try:
|
143 |
+
from moviepy.editor import AudioFileClip
|
144 |
+
except:
|
145 |
+
report_execption(chatbot, history,
|
146 |
+
a=f"解析项目: {txt}",
|
147 |
+
b=f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade moviepy```。")
|
148 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
149 |
+
return
|
150 |
+
|
151 |
+
# 清空历史,以免输入溢出
|
152 |
+
history = []
|
153 |
+
|
154 |
+
# 检测输入参数,如没有给定输入参数,直接退出
|
155 |
+
if os.path.exists(txt):
|
156 |
+
project_folder = txt
|
157 |
+
else:
|
158 |
+
if txt == "": txt = '空空如也的输入栏'
|
159 |
+
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
|
160 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
161 |
+
return
|
162 |
+
|
163 |
+
# 搜索需要处理的文件清单
|
164 |
+
extensions = ['.mp4', '.m4a', '.wav', '.mpga', '.mpeg', '.mp3', '.avi', '.mkv', '.flac', '.aac']
|
165 |
+
|
166 |
+
if txt.endswith(tuple(extensions)):
|
167 |
+
file_manifest = [txt]
|
168 |
+
else:
|
169 |
+
file_manifest = []
|
170 |
+
for extension in extensions:
|
171 |
+
file_manifest.extend(glob.glob(f'{project_folder}/**/*{extension}', recursive=True))
|
172 |
+
|
173 |
+
# 如果没找到任何文件
|
174 |
+
if len(file_manifest) == 0:
|
175 |
+
report_execption(chatbot, history, a=f"解析项目: {txt}", b=f"找不到任何音频或视频文件: {txt}")
|
176 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
177 |
+
return
|
178 |
+
|
179 |
+
# 开始正式执行任务
|
180 |
+
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
181 |
+
parse_prompt = plugin_kwargs.get("advanced_arg", '将音频解析为简体中文')
|
182 |
+
yield from AnalyAudio(parse_prompt, file_manifest, llm_kwargs, chatbot, history)
|
183 |
+
|
184 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
crazy_functions/解析JupyterNotebook.py
CHANGED
@@ -67,6 +67,7 @@ def parseNotebook(filename, enable_markdown=1):
|
|
67 |
def ipynb解释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
68 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
69 |
|
|
|
70 |
enable_markdown = plugin_kwargs.get("advanced_arg", "1")
|
71 |
try:
|
72 |
enable_markdown = int(enable_markdown)
|
|
|
67 |
def ipynb解释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
68 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
69 |
|
70 |
+
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
71 |
enable_markdown = plugin_kwargs.get("advanced_arg", "1")
|
72 |
try:
|
73 |
enable_markdown = int(enable_markdown)
|
crazy_functions/询问多个大语言模型.py
CHANGED
@@ -45,6 +45,7 @@ def 同时问询_指定模型(txt, llm_kwargs, plugin_kwargs, chatbot, history,
|
|
45 |
chatbot.append((txt, "正在同时咨询ChatGPT和ChatGLM……"))
|
46 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
47 |
|
|
|
48 |
# llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo&api2d-gpt-3.5-turbo' # 支持任意数量的llm接口,用&符号分隔
|
49 |
llm_kwargs['llm_model'] = plugin_kwargs.get("advanced_arg", 'chatglm&gpt-3.5-turbo') # 'chatglm&gpt-3.5-turbo' # 支持任意数量的llm接口,用&符号分隔
|
50 |
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
|
|
45 |
chatbot.append((txt, "正在同时咨询ChatGPT和ChatGLM……"))
|
46 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
47 |
|
48 |
+
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
49 |
# llm_kwargs['llm_model'] = 'chatglm&gpt-3.5-turbo&api2d-gpt-3.5-turbo' # 支持任意数量的llm接口,用&符号分隔
|
50 |
llm_kwargs['llm_model'] = plugin_kwargs.get("advanced_arg", 'chatglm&gpt-3.5-turbo') # 'chatglm&gpt-3.5-turbo' # 支持任意数量的llm接口,用&符号分隔
|
51 |
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|