2025-04-18 11:56:07 +08:00
|
|
|
|
import json
|
2025-04-18 11:30:51 +08:00
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
from dashscope.audio.asr import Transcription
|
|
|
|
|
import dashscope
|
|
|
|
|
import requests
|
2025-04-18 11:33:24 +08:00
|
|
|
|
import os
|
2025-04-18 11:56:07 +08:00
|
|
|
|
from openai import OpenAI
|
2025-04-18 11:30:51 +08:00
|
|
|
|
|
2025-04-18 11:56:07 +08:00
|
|
|
|
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY","*****")
|
2025-04-18 11:30:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2025-04-18 11:56:07 +08:00
|
|
|
|
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': """
|
|
|
|
|
将文本的链接提取出来,只留链接的有效信息
|
|
|
|
|
# 输出
|
|
|
|
|
只输出链接不需要分析过程
|
|
|
|
|
"""
|
|
|
|
|
},
|
|
|
|
|
{'role': 'user', 'content': video_url}],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print(completion.model_dump())
|
|
|
|
|
for choices in completion.model_dump()['choices']:
|
|
|
|
|
text+=choices['message']['content']
|
|
|
|
|
|
2025-04-18 11:30:51 +08:00
|
|
|
|
if __name__ == '__main__':
|
2025-04-18 11:56:07 +08:00
|
|
|
|
# music_analysis('https://lf26-music-east.douyinstatic.com/obj/ies-music-hj/7494207652008839996.mp3')
|
|
|
|
|
chat_analysis('4.17 复制打开抖音,看看【三维地球科普的作品】河南省最奇怪的城市——信阳市 # 信阳市 # 河南... https://v.douyin.com/RBU9atEeafc/ AGI:/ i@p.QX 11/14 ')
|
|
|
|
|
|