-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel_api_new.py
More file actions
206 lines (175 loc) · 6.6 KB
/
Copy pathModel_api_new.py
File metadata and controls
206 lines (175 loc) · 6.6 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import requests
import json
from io import BytesIO
import matplotlib.pyplot as plt
from PIL import Image
import base64
import os
from upload import upload_file
# 上传需要
from upload import upload_file, convert_upload_url
AK = "F915WYG9INM7JCMKWYA8"
SK = "bWMR0xxcVBxOA6URk86efzREAOXLzoZvu6lkU00M"
ENDPOINT = "https://obs.cn-south-1.myhuaweicloud.com"
bucket_name = "qg23onnx"
folder_path = 'save'
object_key_first = 'output'
def personalized_api():
print('请给出你的请求文件')
with open('./params.json', 'r', encoding='utf-8') as f:
info = json.load(f)
api = info["api"]
print(api)
params = info["params"][0]
print(params)
headers = info["headers"][0]
print(headers)
response = requests.post(api, json=params, headers=headers, stream=False)
if response.status_code == 200:
response_json = response.json()
with open('./response.json', 'w', encoding='utf-8') as f:
json.dump(response_json, f, indent=4, ensure_ascii=False)
else:
body = response.content.decode('utf-8')
print(f'request failed,status_code:{response.status_code},body:{body}')
def get_access_token_qianfan():
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=DTNX9fIESYPHsOLYRSgGEPBJ&client_secret=PUfU7ew9kXJOBpZRzlmkY3OBRZ7ksB1m"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")
def get_zidongtaichu(question, per):
# 紫东太初文本生成
api = 'https://ai-maas.wair.ac.cn/maas/v1/chat/completions'
headers = {'Authorization': 'Bearer tlf3tc8sltk89etyx0p16u5p'}
params = {
'model': 'taichu_llm',
'messages': [{"role": "user", "content": f"'{question}':{per}"}],
'stream': False
}
response = requests.post(api, json=params, headers=headers, stream=True)
if response.status_code == 200:
response_json = response.json()
content = response_json['choices'][0]['message']['content']
return content
else:
body = response.content.decode('utf-8')
print(f'request failed,status_code:{response.status_code},body:{body}')
def get_qianfan_text(question, per):
# 千帆文本生成
access_token = get_access_token_qianfan()
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + str(
access_token)
payload = json.dumps({
"messages": [
{
"role": "user",
"content": f"'{question}':{per}"
}
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 200:
response_json = response.json()
content = response_json['result']
return content
else:
body = response.content.decode('utf-8')
print(f'request failed,status_code:{response.status_code},body:{body}')
def get_qianfan_graph(question, per):
access_token = get_access_token_qianfan()
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/text2image/sd_xl?access_token=" + str(
access_token)
payload = json.dumps({
"prompt": f"'{question}':{per}",
"size": "1024x1024",
"n": 1,
"steps": 20,
"sampler_index": "Euler a"
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code != 200:
body = response.content.decode('utf-8')
print(f'request failed,status_code:{response.status_code},body:{body}')
response_dict = response.json()
b64_image = response_dict['data'][0]['b64_image']
image_id = response_dict['id']
# plt显示图片
image_data = base64.b64decode(b64_image)
image = Image.open(BytesIO(image_data))
plt.imshow(image)
plt.axis('off')
plt.show()
# 上传文件
image_local_folder = os.path.join(folder_path, image_id + '.png')
object_key = os.path.join(object_key_first + '/' + image_id + '.png')
image.save(image_local_folder)
image_url = upload_file(bucket_name, object_key, image_local_folder, ENDPOINT, AK, SK)
# 处理image_url变成可以下载的url,例子在upload文件里
image_url = convert_upload_url(image_url, bucket_name)
return image_url
def get_qianfan_read(question, per):
access_token = get_access_token_qianfan()
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/image2text/fuyu_8b?access_token=" + access_token
question = image_to_base64(question)
payload = json.dumps({
"prompt": f"{per}",
"image": f"{question}"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
response_dict = response.json()
return response_dict['result']
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
image_data = image_file.read()
base64_encoded = base64.b64encode(image_data).decode('utf-8')
return base64_encoded
def choice(n):
switcher = {
'1': '千帆大模型文本功能',
'2': '紫东太初大模型文本功能',
'3': '千帆大模型图片生成功能',
'4': '千帆大模型图片解析功能',
'5': '用户自定义api'
}
return switcher.get(n, '没有该功能')
def api_check(n, question, per):
'''
n = input( '需要的功能:' ) # 需要模型的编号
per = input( '输入想问的问题:') # 用户对于结果自定义的提问
question = input( 'question: ' ) # 模型输出的文本结果
'''
# n = 5
# per = '这是什么'
# question = '苹果'
n = str(n)
need = choice(n)
print(f'我是{need},正在对模型的输出结果进行最后一步处理...')
if n != '5':
if n == '1':
result = get_qianfan_text(question, per)
elif n == '2':
result = get_zidongtaichu(question, per)
elif n == '3':
result = get_qianfan_graph(question, per)
elif n == '4':
result = get_qianfan_read(question, per)
print('处理完成!')
print(result,"api.check")
return result
else:
personalized_api()
print('处理完成!')