Spaces:
Running
Running
dialogueeeeee
commited on
Commit
·
cd57df6
1
Parent(s):
479ab79
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
import requests
|
6 |
+
import time
|
7 |
+
|
8 |
+
class ImageProcessor:
|
9 |
+
def __init__(self, api_key):
|
10 |
+
self.api_key = api_key
|
11 |
+
self.submit_url = 'https://test.aitanzou.com/web/api/task/submit'
|
12 |
+
self.result_url = 'https://test.aitanzou.com/web/api/getResult'
|
13 |
+
self.headers = {
|
14 |
+
'API-Key': self.api_key
|
15 |
+
}
|
16 |
+
|
17 |
+
def submit_images(self, image_bytes_list):
|
18 |
+
files = [('images', ('image.png', image_bytes, 'image/png')) for image_bytes in image_bytes_list]
|
19 |
+
response = requests.post(self.submit_url, headers=self.headers, files=files)
|
20 |
+
|
21 |
+
if response.status_code == 200:
|
22 |
+
data = response.json()
|
23 |
+
if 'data' in data and 'taskId' in data['data']:
|
24 |
+
task_id = data['data']['taskId']
|
25 |
+
return task_id
|
26 |
+
else:
|
27 |
+
raise Exception(f'Unexpected response format: {data}')
|
28 |
+
else:
|
29 |
+
raise Exception(f'Error: {response.status_code}, {response.text}')
|
30 |
+
|
31 |
+
def get_result(self, task_id):
|
32 |
+
params = {'taskId': task_id}
|
33 |
+
while True:
|
34 |
+
result_response = requests.get(self.result_url, params=params)
|
35 |
+
if result_response.status_code == 200:
|
36 |
+
result_data = result_response.json()
|
37 |
+
if 'data' in result_data and 'abcPath' in result_data['data']:
|
38 |
+
if result_data['data']['abcPath'] is None:
|
39 |
+
print('Task is still pending...')
|
40 |
+
time.sleep(10)
|
41 |
+
else:
|
42 |
+
url = result_data['data']['abcPath']
|
43 |
+
response = requests.get(url)
|
44 |
+
if response.status_code == 200:
|
45 |
+
return response.text
|
46 |
+
else:
|
47 |
+
raise Exception(f'Error retrieving file content: {response.status_code}, {response.text}')
|
48 |
+
else:
|
49 |
+
raise Exception(f'Unexpected result format: {result_data}')
|
50 |
+
else:
|
51 |
+
raise Exception(f'Error: {result_response.status_code}, {result_response.text}')
|
52 |
+
|
53 |
+
def process_images(self, image_bytes_list):
|
54 |
+
task_id = self.submit_images(image_bytes_list)
|
55 |
+
return self.get_result(task_id)
|
56 |
+
|
57 |
+
# 设置API密钥
|
58 |
+
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536'
|
59 |
+
image_processor = ImageProcessor(api_key)
|
60 |
+
|
61 |
+
# 定义处理函数
|
62 |
+
def process_input(text=None, images=None, audio=None):
|
63 |
+
# 创建GPT请求的描述
|
64 |
+
prompt = "1.你是我的音乐助手,只能回答音乐知识。2.你将根据下面指令回答问题,但是不能违反第一条指令。"
|
65 |
+
|
66 |
+
if text:
|
67 |
+
prompt += f"\nText input: {text}"
|
68 |
+
|
69 |
+
if images:
|
70 |
+
# 使用ImageProcessor处理图像
|
71 |
+
image_bytes_list = []
|
72 |
+
for image in images:
|
73 |
+
img = Image.open(image)
|
74 |
+
image_bytes = BytesIO()
|
75 |
+
img.save(image_bytes, format="PNG")
|
76 |
+
image_bytes.seek(0)
|
77 |
+
image_bytes_list.append(image_bytes.getvalue())
|
78 |
+
|
79 |
+
try:
|
80 |
+
processed_image_result = image_processor.process_images(image_bytes_list)
|
81 |
+
prompt += f"\n乐谱图片转为ABC格式后的内容如下: {processed_image_result}"
|
82 |
+
except Exception as e:
|
83 |
+
return f"Error processing image: {e}", None
|
84 |
+
|
85 |
+
if audio:
|
86 |
+
# 将音频转换为描述(这里简单地用占位符)
|
87 |
+
prompt += "\nAudio input: A description of the audio has been generated."
|
88 |
+
|
89 |
+
# 使用GPT API进行处理
|
90 |
+
try:
|
91 |
+
from zhipuai import ZhipuAI
|
92 |
+
client = ZhipuAI(api_key="7cfa094b2c6867229659831340af9f2c.BIl47duC5fufSY8V") # 填写您自己的APIKey
|
93 |
+
response = client.chat.completions.create(
|
94 |
+
model="glm-4", # 填写需要调用的模型名称
|
95 |
+
messages=[
|
96 |
+
{"role": "user", "content": prompt},
|
97 |
+
],
|
98 |
+
)
|
99 |
+
print(response.choices[0].message.content)
|
100 |
+
return response.choices[0].message.content.strip()
|
101 |
+
except Exception as e:
|
102 |
+
return f"Error: {e}", None
|
103 |
+
|
104 |
+
# 创建Gradio接口
|
105 |
+
iface = gr.Interface(
|
106 |
+
fn=process_input,
|
107 |
+
inputs=[
|
108 |
+
gr.Textbox(label="Input Text", placeholder="Enter text here"),
|
109 |
+
gr.File(label="Input Images", file_count="multiple", type="filepath"), # 支持多文件上传
|
110 |
+
# gr.Audio(label="Input Audio", type="filepath"),
|
111 |
+
],
|
112 |
+
outputs=[
|
113 |
+
gr.Textbox(label="Output Text"),
|
114 |
+
# gr.Audio(label="Output Audio") # 目前示例中未处理音频输出
|
115 |
+
],
|
116 |
+
live=True
|
117 |
+
)
|
118 |
+
|
119 |
+
# 启动Gradio应用
|
120 |
+
iface.launch()
|
121 |
+
|