Spaces:
Running
Running
import requests | |
from PIL import Image | |
from io import BytesIO | |
from image_processor import ImageProcessor | |
from evaluation_processor import EvaluationProcessor | |
# Initialize image and evaluation processors | |
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536' | |
def process_json(url): | |
#url = "https://test.aitanzou.com/reports/9/c5993371-26b1-4536-af4c-847e43d4c293/result/analysis_report.json" | |
response = requests.get(url) | |
bar_regions = response.json()["bar_regions"] | |
result = [] | |
# 遍历列表 | |
cnt = 1 | |
for item in bar_regions: | |
# 提取每个元素的字段 | |
data = { | |
"小节": cnt, | |
"des_note": item["des_note"], | |
"des_speed": item["des_speed"], | |
"des_stable": item["des_stable"], | |
"des_tempo": item["des_tempo"] | |
} | |
# 将提取的数据追加到结果列表中 | |
result.append(data) | |
cnt = cnt+1 | |
#print(result) | |
return result | |
def process_images(images): | |
image_bytes_list = [] | |
for image in images: | |
img = Image.open(image.name) | |
image_bytes = BytesIO() | |
img.save(image_bytes, format="PNG") | |
image_bytes.seek(0) | |
image_bytes_list.append(image_bytes.getvalue()) | |
try: | |
image_processor = ImageProcessor(api_key) | |
processed_image_result = image_processor.process_images(image_bytes_list) | |
return fr"\n乐谱的内容如下: {processed_image_result}" | |
except Exception as e: | |
return f"Error processing image: {e}" | |
def process_audio_video(file, is_video): | |
path = file.name | |
print(path) | |
try: | |
evaluation_processor = EvaluationProcessor(api_key) | |
result, title = evaluation_processor.process_evaluation(path, is_video) | |
print(result) | |
all_result = process_json(result["analysis_report_path"]) | |
prompt = ( | |
fr"如果有曲名《{title}》,请你根据这首歌的名字和作者," | |
fr"先从以下几个总的方面评价一下这首曲子的演奏结果。\n" | |
fr"1. 综合得分: {result['evaAll']}\n" | |
fr"2. 完整性: {result['evaCompletion']}\n" | |
fr"3. 按键: {result['evaNote']}\n" | |
fr"4. 稳定性: {result['evaStability']}\n" | |
fr"5. 节奏: {result['evaTempoSync']}\n" | |
fr"这首曲子共{len(all_result)}小节,请注意如果用户指令问更详细的结果则结合小节进行评价:\n" | |
fr"{' '.join(map(str, all_result))}" | |
) | |
return prompt, result.get('result_path', '') | |
except Exception as e: | |
return f"Error processing audio: {e}", None | |