Spaces:
Running
Running
fistyee
commited on
Commit
·
2fc6c45
1
Parent(s):
2e8ce10
add
Browse files- __pycache__/evaluation_processor.cpython-311.pyc +0 -0
- __pycache__/image_processor.cpython-311.pyc +0 -0
- evaluation_processor.py +56 -0
- image_processor.py +52 -0
- app.py → main.py +44 -69
__pycache__/evaluation_processor.cpython-311.pyc
ADDED
Binary file (3.36 kB). View file
|
|
__pycache__/image_processor.cpython-311.pyc
ADDED
Binary file (3.41 kB). View file
|
|
evaluation_processor.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import time
|
3 |
+
|
4 |
+
class EvaluationProcessor:
|
5 |
+
def __init__(self, api_key):
|
6 |
+
self.api_key = api_key
|
7 |
+
self.audio_url = 'https://test.aitanzou.com/web/api/submit_audio'
|
8 |
+
self.video_url = 'https://test.aitanzou.com/web/api/submit_video'
|
9 |
+
self.result_url = 'https://test.aitanzou.com/web/api/getEvaluationResult'
|
10 |
+
self.headers = {
|
11 |
+
'API-Key': self.api_key
|
12 |
+
}
|
13 |
+
|
14 |
+
def submit_evaluation(self, file_path, music_id=None, hand=-1, order_start=0, order_end=-1, repeat_type=0, is_video=False):
|
15 |
+
url = self.video_url if is_video else self.audio_url
|
16 |
+
|
17 |
+
files = {
|
18 |
+
'file': ("video_file.mp4", open(file_path, 'rb'), 'video/mp4') # 确保给文件指定一个名称
|
19 |
+
}
|
20 |
+
data = {
|
21 |
+
'musicId': music_id,
|
22 |
+
'hand': hand,
|
23 |
+
'orderStart': order_start,
|
24 |
+
'orderEnd': order_end,
|
25 |
+
'repeatType': repeat_type
|
26 |
+
}
|
27 |
+
response = requests.post(url, headers=self.headers, files=files, data=data)
|
28 |
+
if response.status_code == 200:
|
29 |
+
data = response.json()
|
30 |
+
task_id = data['data']['taskId']
|
31 |
+
return task_id
|
32 |
+
else:
|
33 |
+
raise Exception(f'Error: {response.status_code}, {response.text}')
|
34 |
+
|
35 |
+
def get_evaluation_result(self, task_id):
|
36 |
+
params = {'taskId': task_id}
|
37 |
+
while True:
|
38 |
+
result_response = requests.get(self.result_url, headers=self.headers, params=params)
|
39 |
+
if result_response.status_code == 200:
|
40 |
+
result_data = result_response.json()
|
41 |
+
status = result_data['data']['status']
|
42 |
+
if status in ['pending', 'processing']:
|
43 |
+
print(f'Task is still {status}...')
|
44 |
+
time.sleep(2)
|
45 |
+
elif status == 'completed':
|
46 |
+
return result_data['data']
|
47 |
+
else:
|
48 |
+
raise Exception(f'Task failed: {result_data["message"]}')
|
49 |
+
else:
|
50 |
+
raise Exception(f'Error: {result_response.status_code}, {result_response.text}')
|
51 |
+
|
52 |
+
def process_evaluation(self, file_path, music_id=None, hand=-1, order_start=0, order_end=-1, repeat_type=0, is_video=False):
|
53 |
+
task_id = self.submit_evaluation(file_path, music_id, hand, order_start, order_end, repeat_type, is_video)
|
54 |
+
return self.get_evaluation_result(task_id)
|
55 |
+
|
56 |
+
|
image_processor.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import time
|
3 |
+
from io import BytesIO
|
4 |
+
|
5 |
+
class ImageProcessor:
|
6 |
+
def __init__(self, api_key):
|
7 |
+
self.api_key = api_key
|
8 |
+
self.submit_url = 'https://test.aitanzou.com/web/api/task/submit'
|
9 |
+
self.result_url = 'https://test.aitanzou.com/web/api/getResult'
|
10 |
+
self.headers = {
|
11 |
+
'API-Key': self.api_key
|
12 |
+
}
|
13 |
+
|
14 |
+
def submit_images(self, image_bytes_list):
|
15 |
+
files = [('images', ('image.png', image_bytes, 'image/png')) for image_bytes in image_bytes_list]
|
16 |
+
response = requests.post(self.submit_url, headers=self.headers, files=files)
|
17 |
+
|
18 |
+
if response.status_code == 200:
|
19 |
+
data = response.json()
|
20 |
+
if 'data' in data and 'taskId' in data['data']:
|
21 |
+
task_id = data['data']['taskId']
|
22 |
+
return task_id
|
23 |
+
else:
|
24 |
+
raise Exception(f'Unexpected response format: {data}')
|
25 |
+
else:
|
26 |
+
raise Exception(f'Error: {response.status_code}, {response.text}')
|
27 |
+
|
28 |
+
def get_result(self, task_id):
|
29 |
+
params = {'taskId': task_id}
|
30 |
+
while True:
|
31 |
+
result_response = requests.get(self.result_url, params=params)
|
32 |
+
if result_response.status_code == 200:
|
33 |
+
result_data = result_response.json()
|
34 |
+
if 'data' in result_data and 'abcPath' in result_data['data']:
|
35 |
+
if result_data['data']['abcPath'] is None:
|
36 |
+
print('Task is still pending...')
|
37 |
+
time.sleep(10)
|
38 |
+
else:
|
39 |
+
url = result_data['data']['abcPath']
|
40 |
+
response = requests.get(url)
|
41 |
+
if response.status_code == 200:
|
42 |
+
return response.text
|
43 |
+
else:
|
44 |
+
raise Exception(f'Error retrieving file content: {response.status_code}, {response.text}')
|
45 |
+
else:
|
46 |
+
raise Exception(f'Unexpected result format: {result_data}')
|
47 |
+
else:
|
48 |
+
raise Exception(f'Error: {result_response.status_code}, {result_response.text}')
|
49 |
+
|
50 |
+
def process_images(self, image_bytes_list):
|
51 |
+
task_id = self.submit_images(image_bytes_list)
|
52 |
+
return self.get_result(task_id)
|
app.py → main.py
RENAMED
@@ -1,82 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
from io import BytesIO
|
4 |
-
import requests
|
5 |
-
import time
|
6 |
import openai
|
7 |
import os
|
8 |
-
from openai import OpenAI
|
9 |
from dotenv import load_dotenv
|
|
|
|
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
-
client = OpenAI(
|
14 |
-
|
15 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
16 |
-
#api_key=os.environ.get("OPENAI_API_KEY"),
|
17 |
)
|
18 |
|
19 |
# 设置OpenAI API密钥
|
20 |
-
openai.api_key = "
|
21 |
engine = "gpt-4o-mini"
|
22 |
|
23 |
-
class ImageProcessor:
|
24 |
-
def __init__(self, api_key):
|
25 |
-
self.api_key = api_key
|
26 |
-
self.submit_url = 'https://test.aitanzou.com/web/api/task/submit'
|
27 |
-
self.result_url = 'https://test.aitanzou.com/web/api/getResult'
|
28 |
-
self.headers = {
|
29 |
-
'API-Key': self.api_key
|
30 |
-
}
|
31 |
-
|
32 |
-
def submit_images(self, image_bytes_list):
|
33 |
-
files = [('images', ('image.png', image_bytes, 'image/png')) for image_bytes in image_bytes_list]
|
34 |
-
response = requests.post(self.submit_url, headers=self.headers, files=files)
|
35 |
-
|
36 |
-
if response.status_code == 200:
|
37 |
-
data = response.json()
|
38 |
-
if 'data' in data and 'taskId' in data['data']:
|
39 |
-
task_id = data['data']['taskId']
|
40 |
-
return task_id
|
41 |
-
else:
|
42 |
-
raise Exception(f'Unexpected response format: {data}')
|
43 |
-
else:
|
44 |
-
raise Exception(f'Error: {response.status_code}, {response.text}')
|
45 |
-
|
46 |
-
def get_result(self, task_id):
|
47 |
-
params = {'taskId': task_id}
|
48 |
-
while True:
|
49 |
-
result_response = requests.get(self.result_url, params=params)
|
50 |
-
if result_response.status_code == 200:
|
51 |
-
result_data = result_response.json()
|
52 |
-
if 'data' in result_data and 'abcPath' in result_data['data']:
|
53 |
-
if result_data['data']['abcPath'] is None:
|
54 |
-
print('Task is still pending...')
|
55 |
-
time.sleep(10)
|
56 |
-
else:
|
57 |
-
url = result_data['data']['abcPath']
|
58 |
-
response = requests.get(url)
|
59 |
-
if response.status_code == 200:
|
60 |
-
return response.text
|
61 |
-
else:
|
62 |
-
raise Exception(f'Error retrieving file content: {response.status_code}, {response.text}')
|
63 |
-
else:
|
64 |
-
raise Exception(f'Unexpected result format: {result_data}')
|
65 |
-
else:
|
66 |
-
raise Exception(f'Error: {result_response.status_code}, {result_response.text}')
|
67 |
-
|
68 |
-
def process_images(self, image_bytes_list):
|
69 |
-
task_id = self.submit_images(image_bytes_list)
|
70 |
-
return self.get_result(task_id)
|
71 |
-
|
72 |
# 设置Music API密钥
|
73 |
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536'
|
74 |
image_processor = ImageProcessor(api_key)
|
|
|
75 |
|
76 |
# 定义处理函数
|
77 |
-
def process_input(text=None, images=None, audio=None):
|
78 |
# 创建GPT请求的描述
|
79 |
-
|
80 |
system = "1.你是一个专业的钢琴音乐教师,只能回答音乐知识,回复的内容为普通文本格式。如果提供的乐谱是abc记谱法,则回复时不要用abc记谱法,需要转换为传统的普通记谱法使用专业词汇进行回答问题2.你将根据下面指令回答问题,但是不能违反第一条指令,也不能在回复中提及。"
|
81 |
messages = [{"role": "system", "content": system}]
|
82 |
prompt = ""
|
@@ -95,15 +43,43 @@ def process_input(text=None, images=None, audio=None):
|
|
95 |
|
96 |
try:
|
97 |
processed_image_result = image_processor.process_images(image_bytes_list)
|
98 |
-
#prompt += f"\n
|
99 |
-
prompt += f"\n
|
100 |
|
101 |
except Exception as e:
|
102 |
return f"Error processing image: {e}", None
|
103 |
|
104 |
if audio:
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
# 使用GPT API进行处理
|
109 |
try:
|
@@ -127,14 +103,13 @@ def process_input(text=None, images=None, audio=None):
|
|
127 |
iface = gr.Interface(
|
128 |
fn=process_input,
|
129 |
inputs=[
|
130 |
-
gr.Textbox(label="Input Text", placeholder="Enter text here", lines=2), #
|
131 |
-
gr.File(label="Input Images", file_count="multiple", type="file"), #
|
132 |
-
|
133 |
-
|
134 |
],
|
135 |
outputs=[
|
136 |
-
gr.Textbox(label="Output Text")
|
137 |
-
# gr.Audio(label="Output Audio") # 目前示例中未处理音频输出
|
138 |
],
|
139 |
live=False,
|
140 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
from io import BytesIO
|
|
|
|
|
4 |
import openai
|
5 |
import os
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
+
from image_processor import ImageProcessor
|
8 |
+
from evaluation_processor import EvaluationProcessor
|
9 |
|
10 |
load_dotenv()
|
11 |
|
12 |
+
client = openai.OpenAI(
|
13 |
+
api_key=os.getenv("OPENAI_API_KEY")
|
|
|
|
|
14 |
)
|
15 |
|
16 |
# 设置OpenAI API密钥
|
17 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
18 |
engine = "gpt-4o-mini"
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# 设置Music API密钥
|
21 |
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536'
|
22 |
image_processor = ImageProcessor(api_key)
|
23 |
+
evaluation_processor = EvaluationProcessor(api_key)
|
24 |
|
25 |
# 定义处理函数
|
26 |
+
def process_input(text=None, images=None, audio=None, video=None):
|
27 |
# 创建GPT请求的描述
|
|
|
28 |
system = "1.你是一个专业的钢琴音乐教师,只能回答音乐知识,回复的内容为普通文本格式。如果提供的乐谱是abc记谱法,则回复时不要用abc记谱法,需要转换为传统的普通记谱法使用专业词汇进行回答问题2.你将根据下面指令回答问题,但是不能违反第一条指令,也不能在回复中提及。"
|
29 |
messages = [{"role": "system", "content": system}]
|
30 |
prompt = ""
|
|
|
43 |
|
44 |
try:
|
45 |
processed_image_result = image_processor.process_images(image_bytes_list)
|
46 |
+
#prompt += f"\n乐谱的内容如下,这是一首杜维诺伊的曲子,请你根据曲子的曲风回答问题: {processed_image_result}"
|
47 |
+
prompt += f"\n乐谱的内容如下,请你根据曲子的曲风回答问题: {processed_image_result}"
|
48 |
|
49 |
except Exception as e:
|
50 |
return f"Error processing image: {e}", None
|
51 |
|
52 |
if audio:
|
53 |
+
try:
|
54 |
+
# 使用EvaluationProcessor处理音频
|
55 |
+
audio_path = audio.name
|
56 |
+
result = evaluation_processor.process_evaluation(audio_path, is_video=False)
|
57 |
+
prompt += f'''请你根据
|
58 |
+
"eva_all":综合得分
|
59 |
+
"eva_completion":完整性
|
60 |
+
"eva_note":按键
|
61 |
+
"eva_stability":稳定性
|
62 |
+
"eva_tempo_sync":节奏
|
63 |
+
评价一下评测结果: {result}'''
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
return f"Error processing audio: {e}", None
|
67 |
+
|
68 |
+
if video:
|
69 |
+
try:
|
70 |
+
# 使用EvaluationProcessor处理视频
|
71 |
+
video_path = video.name
|
72 |
+
result = evaluation_processor.process_evaluation(video_path, is_video=True)
|
73 |
+
prompt += f'''请你根据
|
74 |
+
"eva_all":综合得分
|
75 |
+
"eva_completion":完整性
|
76 |
+
"eva_note":按键
|
77 |
+
"eva_stability":稳定性
|
78 |
+
"eva_tempo_sync":节奏
|
79 |
+
评价一下评测结果: {result}'''
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
return f"Error processing video: {e}", None
|
83 |
|
84 |
# 使用GPT API进行处理
|
85 |
try:
|
|
|
103 |
iface = gr.Interface(
|
104 |
fn=process_input,
|
105 |
inputs=[
|
106 |
+
gr.Textbox(label="Input Text", placeholder="Enter text here", lines=2), # 文本输入
|
107 |
+
gr.File(label="Input Images", file_count="multiple", type="file"), # 多文件上传
|
108 |
+
gr.File(label="Input Audio", type="file"), # 音频文件上传
|
109 |
+
gr.File(label="Input Video", type="file") # 视频文件上传
|
110 |
],
|
111 |
outputs=[
|
112 |
+
gr.Textbox(label="Output Text")
|
|
|
113 |
],
|
114 |
live=False,
|
115 |
)
|