Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
|
4 |
|
@@ -6,17 +8,39 @@ import gradio as gr
|
|
6 |
import base64
|
7 |
# Function to encode the image
|
8 |
def encode_image(image_path):
|
9 |
-
|
10 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
11 |
|
12 |
-
import requests
|
13 |
-
import os
|
14 |
|
15 |
openai_api_key = os.environ.get('openai_api_key')
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def ask_image(text,image,api_token=openai_api_key):
|
18 |
-
|
19 |
-
|
20 |
{
|
21 |
"role": "user",
|
22 |
"content": [
|
@@ -24,44 +48,44 @@ def ask_image(text,image,api_token=openai_api_key):
|
|
24 |
{
|
25 |
"type": "image_url",
|
26 |
"image_url": {
|
27 |
-
"url":f"data:image/jpeg;base64,{base64_image}"
|
28 |
-
|
29 |
},
|
30 |
},
|
31 |
],
|
32 |
}
|
33 |
]
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
'Authorization': f'Bearer {api_token}'
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
'model': 'gpt-4o', # 可以根据需要更换其他模型
|
43 |
'messages': messages,
|
44 |
'temperature': 0.7 # 可以根据需要调整
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
try:
|
53 |
# 发送请求
|
54 |
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
|
55 |
-
|
56 |
# 解析响应内容
|
57 |
response_data = response.json()
|
58 |
response_content = response_data['choices'][0]['message']['content']
|
59 |
usage = response_data['usage']
|
60 |
-
|
61 |
# response_content = 'test response'
|
62 |
-
|
63 |
return response_content
|
64 |
-
|
65 |
except Exception as e:
|
66 |
# 如果已经达到最大重试次数,那么返回空值
|
67 |
if i == max_retry - 1:
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
|
5 |
|
6 |
|
|
|
8 |
import base64
|
9 |
# Function to encode the image
|
10 |
def encode_image(image_path):
|
11 |
+
with open(image_path, "rb") as image_file:
|
12 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
13 |
|
|
|
|
|
14 |
|
15 |
openai_api_key = os.environ.get('openai_api_key')
|
16 |
|
17 |
+
# 将图片上传到google cloud storage
|
18 |
+
def upload_image_to_gcs_blob(image):
|
19 |
+
|
20 |
+
google_creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
21 |
+
|
22 |
+
creds_json = json.loads(google_creds)
|
23 |
+
credentials = service_account.Credentials.from_service_account_info(creds_json)
|
24 |
+
|
25 |
+
# 现在您可以使用这些凭证对Google Cloud服务进行认证
|
26 |
+
storage_client = storage.Client(credentials=credentials, project=creds_json['project_id'])
|
27 |
+
|
28 |
+
bucket_name = os.environ.get('bucket_name')
|
29 |
+
bucket = storage_client.bucket(bucket_name)
|
30 |
+
|
31 |
+
destination_blob_name = os.path.basename(image)
|
32 |
+
blob = bucket.blob(destination_blob_name)
|
33 |
+
|
34 |
+
blob.upload_from_filename(image)
|
35 |
+
|
36 |
+
public_url = blob.public_url
|
37 |
+
|
38 |
+
return public_url
|
39 |
+
|
40 |
+
|
41 |
def ask_image(text,image,api_token=openai_api_key):
|
42 |
+
public_url = upload_image_to_gcs_blob(image)
|
43 |
+
messages=[
|
44 |
{
|
45 |
"role": "user",
|
46 |
"content": [
|
|
|
48 |
{
|
49 |
"type": "image_url",
|
50 |
"image_url": {
|
51 |
+
# "url":f"data:image/jpeg;base64,{base64_image}"
|
52 |
+
"url": public_url
|
53 |
},
|
54 |
},
|
55 |
],
|
56 |
}
|
57 |
]
|
58 |
+
|
59 |
+
# 请求头部信息
|
60 |
+
headers = {
|
61 |
'Authorization': f'Bearer {api_token}'
|
62 |
+
}
|
63 |
+
|
64 |
+
# 请求体信息
|
65 |
+
data = {
|
66 |
'model': 'gpt-4o', # 可以根据需要更换其他模型
|
67 |
'messages': messages,
|
68 |
'temperature': 0.7 # 可以根据需要调整
|
69 |
+
}
|
70 |
+
|
71 |
+
|
72 |
+
# 设定最大重试次数
|
73 |
+
max_retry = 3
|
74 |
+
|
75 |
+
for i in range(max_retry):
|
76 |
try:
|
77 |
# 发送请求
|
78 |
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
|
79 |
+
|
80 |
# 解析响应内容
|
81 |
response_data = response.json()
|
82 |
response_content = response_data['choices'][0]['message']['content']
|
83 |
usage = response_data['usage']
|
84 |
+
|
85 |
# response_content = 'test response'
|
86 |
+
|
87 |
return response_content
|
88 |
+
|
89 |
except Exception as e:
|
90 |
# 如果已经达到最大重试次数,那么返回空值
|
91 |
if i == max_retry - 1:
|