File size: 4,536 Bytes
2fc6c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1c3845
2fc6c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1c3845
 
2fc6c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1c3845
 
2fc6c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
964b71d
61df3f6
 
 
2fc6c45
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
from PIL import Image
from io import BytesIO
import openai
import os
from dotenv import load_dotenv
from image_processor import ImageProcessor
from evaluation_processor import EvaluationProcessor

load_dotenv()

client = openai.OpenAI(
    api_key=os.getenv("OPENAI_API_KEY")
)

# 设置OpenAI API密钥
openai.api_key = os.getenv("OPENAI_API_KEY")
engine = "gpt-4o-mini"

# 设置Music API密钥
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536'
image_processor = ImageProcessor(api_key)
evaluation_processor = EvaluationProcessor(api_key)

# 定义处理函数
def process_input(text=None, images=None, audio=None, video=None):
    # 创建GPT请求的描述
    system = "1.你是一个专业的钢琴音乐教师,只能回答音乐知识,回复的内容为普通文本格式,不用任何markdown符号。如果提供的乐谱是abc记谱法,则回复时不要用abc记谱法,需要转换为传统的普通记谱法使用专业词汇进行回答问题2.你将根据下面指令回答问题,但是不能违反第一条指令,也不能在回复中提及。"
    messages = [{"role": "system", "content": system}]
    prompt = ""
    if text:
        prompt += f"\nText input: {text}"
    
    if images:
        # 使用ImageProcessor处理图像
        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:
            processed_image_result = image_processor.process_images(image_bytes_list)
            #prompt += f"\n乐谱的内容如下,这是一首杜维诺伊的曲子,请你根据曲子的曲风回答问题: {processed_image_result}"
            prompt += f"\n乐谱的内容如下,请你根据曲子的曲风回答问题: {processed_image_result}"
        
        except Exception as e:
            return f"Error processing image: {e}", None
    
    if audio:
        try:
            # 使用EvaluationProcessor处理音频
            audio_path = audio.name
            result,title = evaluation_processor.process_evaluation(audio_path, is_video=False)
            prompt += f'''如果有曲名{title},请你根据这首歌的名字作者,并且'''
            prompt += f'''请你根据
            "eva_all":综合得分
            "eva_completion":完整性
            "eva_note":按键
            "eva_stability":稳定性
            "eva_tempo_sync":节奏
            评价一下评测结果: {result}'''
        
        except Exception as e:
            return f"Error processing audio: {e}", None
    
    if video:
        try:
            # 使用EvaluationProcessor处理视频
            video_path = video.name
            result,title = evaluation_processor.process_evaluation(video_path, is_video=True)
            prompt += f'''如果有曲名{title},请你根据这首歌的名字作者,并且'''
            prompt += f'''请你根据
            "eva_all":综合得分
            "eva_completion":完整性
            "eva_note":按键
            "eva_stability":稳定性
            "eva_tempo_sync":节奏
            评价一下评测结果: {result}'''
        
        except Exception as e:
            return f"Error processing video: {e}", None
    
    # 使用GPT API进行处理
    try:
        messages.append({"role": "user", "content": prompt})
        response = client.chat.completions.create(
            model=engine,
            messages=messages,
            temperature=0.2,
            max_tokens=4096,
            top_p=0.95,
            frequency_penalty=0,
            presence_penalty=0,
            stop=None
        )
        print(response.choices[0].message.content)
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {e}", None

# 创建Gradio接口
iface = gr.Interface(
    fn=process_input,
    inputs=[
        gr.Textbox(label="Input Text", placeholder="我是音乐分析大模型,您可以上传曲谱,演奏的音/视频", lines=2),  # 文本输入
        gr.File(label="Input Images", file_count="multiple", type="filepath"),  # 多文件上传
        gr.File(label="Input Audio", type="filepath"),  # 音频文件上传
        gr.File(label="Input Video", type="filepath")  # 视频文件上传
    ],
    outputs=[
        gr.Textbox(label="Output Text")
    ],
    live=False,
)

# 启动Gradio应用
iface.launch()