78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
import json
|
||
from http import HTTPStatus
|
||
from dashscope.audio.asr import Transcription
|
||
import dashscope
|
||
import requests
|
||
import os
|
||
from openai import OpenAI
|
||
|
||
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
|
||
content = """
|
||
将文本的链接提取出来,只留链接的有效信息
|
||
# 输出
|
||
只输出链接不需要分析过程
|
||
"""
|
||
|
||
|
||
def music_analysis(music_url):
|
||
transcribe_response = Transcription.async_call(
|
||
model='paraformer-v2',
|
||
file_urls=[music_url],
|
||
language_hints=['zh', 'en'] # “language_hints”只支持paraformer-v2模型
|
||
)
|
||
|
||
while True:
|
||
if transcribe_response.output.task_status == 'SUCCEEDED' or transcribe_response.output.task_status == 'FAILED':
|
||
break
|
||
transcribe_response = Transcription.fetch(task=transcribe_response.output.task_id)
|
||
|
||
if transcribe_response.status_code == HTTPStatus.OK:
|
||
url = transcribe_response.output['results'][0]['transcription_url']
|
||
print(url)
|
||
|
||
# 发送GET请求
|
||
response = requests.get(url)
|
||
text = ''
|
||
|
||
# 验证响应状态
|
||
if response.status_code == 200:
|
||
# 解析JSON数据
|
||
data = response.json()
|
||
for transcripts in data['transcripts']:
|
||
text += transcripts['text']
|
||
|
||
else:
|
||
print(f"请求失败,状态码:{response.status_code}")
|
||
return text
|
||
|
||
|
||
def chat_analysis(video_url):
|
||
text = ''
|
||
client = OpenAI(
|
||
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
|
||
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
||
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||
)
|
||
completion = client.chat.completions.create(
|
||
model="qwen-plus",
|
||
# 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
||
messages=[
|
||
{
|
||
'role': 'system',
|
||
'content': content
|
||
},
|
||
{'role': 'user', 'content': video_url}],
|
||
)
|
||
|
||
print(f'语言模型返回数据: {completion.model_dump()}')
|
||
for choices in completion.model_dump()['choices']:
|
||
text += choices['message']['content']
|
||
return text
|
||
|
||
def video_url(video_url):
|
||
print(video_url)
|
||
if __name__ == '__main__':
|
||
# music_analysis('https://lf26-music-east.douyinstatic.com/obj/ies-music-hj/7494207652008839996.mp3')
|
||
chat_analysis(
|
||
'原始视频url为:9.79 复制打开抖音,看看【学钓鱼的佳琪的作品】少与人纠缠,多跟鱼拉扯 # dou是钓鱼人 # 爱... https://v.douyin.com/1skvRRYPEgA/ DHI:/ n@D.uf 05/06')
|