Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,29 +14,37 @@ import os
|
|
14 |
import requests
|
15 |
|
16 |
# upload image to google cloud storage
|
17 |
-
def
|
18 |
|
19 |
google_creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
20 |
|
21 |
creds_json = json.loads(google_creds)
|
22 |
credentials = service_account.Credentials.from_service_account_info(creds_json)
|
23 |
|
24 |
-
#
|
25 |
storage_client = storage.Client(credentials=credentials, project=creds_json['project_id'])
|
26 |
|
27 |
bucket_name=os.environ.get('bucket_name')
|
28 |
bucket = storage_client.bucket(bucket_name)
|
29 |
|
30 |
-
destination_blob_name = os.path.basename(
|
31 |
blob = bucket.blob(destination_blob_name)
|
32 |
|
33 |
-
blob.upload_from_filename(
|
34 |
|
35 |
public_url = blob.public_url
|
36 |
|
37 |
return public_url
|
38 |
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# def respond(
|
42 |
# message,
|
@@ -70,33 +78,33 @@ def upload_image_to_gcs_blob(image):
|
|
70 |
# response += token
|
71 |
# yield response
|
72 |
|
73 |
-
def get_completion(message,history
|
74 |
-
# base64_image = encode_image(image)
|
75 |
if message["text"].strip() == "" and not message["files"]:
|
76 |
-
gr.Error("Please input a query and optionally image(s).")
|
77 |
|
78 |
if message["text"].strip() == "" and message["files"]:
|
79 |
-
gr.Error("Please input a text query along the image(s).")
|
80 |
|
81 |
text = message['text']
|
82 |
content = [
|
83 |
{"type": "text", "text": text},
|
84 |
]
|
85 |
if message['files']:
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
"
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
94 |
|
95 |
-
init_message = [{"role": "system", "content": system_message}]
|
96 |
-
|
97 |
history_openai_format = []
|
98 |
for human, assistant in history:
|
99 |
-
#
|
100 |
if isinstance(human, tuple):
|
101 |
continue
|
102 |
history_openai_format.append({"role": "user", "content": human })
|
@@ -104,47 +112,63 @@ def get_completion(message,history,system_message,max_tokens,temperature):
|
|
104 |
history_openai_format.append({"role": "user", "content": content})
|
105 |
print(history_openai_format)
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
#
|
109 |
openai_api_key = os.environ.get('openai_api_key')
|
110 |
headers = {
|
111 |
'Authorization': f'Bearer {openai_api_key}'
|
112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
-
#
|
115 |
data = {
|
116 |
-
'model': 'gpt-4o', #
|
117 |
-
'messages':
|
118 |
-
'temperature': temperature,
|
119 |
'max_tokens':max_tokens,
|
120 |
# 'stream':True,
|
121 |
}
|
122 |
-
|
|
|
123 |
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
|
124 |
-
|
125 |
-
# 解析响应内容
|
126 |
response_data = response.json()
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
129 |
|
130 |
return response_content
|
131 |
|
132 |
|
133 |
|
134 |
-
|
135 |
"""
|
136 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
137 |
"""
|
138 |
demo = gr.ChatInterface(
|
139 |
get_completion,
|
140 |
multimodal=True,
|
141 |
-
additional_inputs=[
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
],
|
146 |
)
|
147 |
|
|
|
|
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
demo.launch()
|
|
|
14 |
import requests
|
15 |
|
16 |
# upload image to google cloud storage
|
17 |
+
def upload_file_to_gcs_blob(file):
|
18 |
|
19 |
google_creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
20 |
|
21 |
creds_json = json.loads(google_creds)
|
22 |
credentials = service_account.Credentials.from_service_account_info(creds_json)
|
23 |
|
24 |
+
# Google Cloud credentials
|
25 |
storage_client = storage.Client(credentials=credentials, project=creds_json['project_id'])
|
26 |
|
27 |
bucket_name=os.environ.get('bucket_name')
|
28 |
bucket = storage_client.bucket(bucket_name)
|
29 |
|
30 |
+
destination_blob_name = os.path.basename(file)
|
31 |
blob = bucket.blob(destination_blob_name)
|
32 |
|
33 |
+
blob.upload_from_filename(file)
|
34 |
|
35 |
public_url = blob.public_url
|
36 |
|
37 |
return public_url
|
38 |
|
39 |
|
40 |
+
from PIL import Image
|
41 |
+
|
42 |
+
def is_image(file_path):
|
43 |
+
try:
|
44 |
+
Image.open(file_path)
|
45 |
+
return True
|
46 |
+
except IOError:
|
47 |
+
return False
|
48 |
|
49 |
# def respond(
|
50 |
# message,
|
|
|
78 |
# response += token
|
79 |
# yield response
|
80 |
|
81 |
+
def get_completion(message,history):
|
|
|
82 |
if message["text"].strip() == "" and not message["files"]:
|
83 |
+
raise gr.Error("Please input a query and optionally image(s).")
|
84 |
|
85 |
if message["text"].strip() == "" and message["files"]:
|
86 |
+
raise gr.Error("Please input a text query along the image(s).")
|
87 |
|
88 |
text = message['text']
|
89 |
content = [
|
90 |
{"type": "text", "text": text},
|
91 |
]
|
92 |
if message['files']:
|
93 |
+
file = message['files'][0]
|
94 |
+
public_url = upload_file_to_gcs_blob(file)
|
95 |
+
if is_image(file): # only support image file now
|
96 |
+
content_image = {
|
97 |
+
"type": "image_url",
|
98 |
+
"image_url": {
|
99 |
+
"url": public_url,
|
100 |
+
},}
|
101 |
+
content.append(content_image)
|
102 |
+
else:
|
103 |
+
raise gr.Error("Only support image files now.")
|
104 |
|
|
|
|
|
105 |
history_openai_format = []
|
106 |
for human, assistant in history:
|
107 |
+
# check if there is image info in the history message
|
108 |
if isinstance(human, tuple):
|
109 |
continue
|
110 |
history_openai_format.append({"role": "user", "content": human })
|
|
|
112 |
history_openai_format.append({"role": "user", "content": content})
|
113 |
print(history_openai_format)
|
114 |
|
115 |
+
system_message = '''You are GPT-4o("o" for omni), OpenAI's new flagship model that can reason across audio, vision, and text in real time.
|
116 |
+
GPT-4o matches GPT-4 Turbo performance on text in English and code, with significant improvement on text in non-English languages, while also being much faster.
|
117 |
+
GPT-4o is especially better at vision and audio understanding compared to existing models.
|
118 |
+
GPT-4o's text and image capabilities are avaliable for users now. More capabilities like audio, and video will be rolled out iteratively in the future.
|
119 |
+
'''
|
120 |
+
|
121 |
|
122 |
+
# headers
|
123 |
openai_api_key = os.environ.get('openai_api_key')
|
124 |
headers = {
|
125 |
'Authorization': f'Bearer {openai_api_key}'
|
126 |
}
|
127 |
+
|
128 |
+
temperature = 0.7
|
129 |
+
max_tokens = 2048
|
130 |
+
|
131 |
+
init_message = [{"role": "system", "content": system_message}]
|
132 |
+
messages = init_message + history_openai_format[-5:], #system message + latest 2 round dialogues + user input
|
133 |
|
134 |
+
# request body
|
135 |
data = {
|
136 |
+
'model': 'gpt-4o', # we use gpt-4o here
|
137 |
+
'messages': messages
|
138 |
+
'temperature': temperature,
|
139 |
'max_tokens':max_tokens,
|
140 |
# 'stream':True,
|
141 |
}
|
142 |
+
|
143 |
+
# get response
|
144 |
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
|
|
|
|
|
145 |
response_data = response.json()
|
146 |
+
|
147 |
+
if 'error' in response_data:
|
148 |
+
response_content = response_data['error']['message']
|
149 |
+
else:
|
150 |
+
response_content = response_data['choices'][0]['message']['content']
|
151 |
+
usage = response_data['usage']
|
152 |
|
153 |
return response_content
|
154 |
|
155 |
|
156 |
|
|
|
157 |
"""
|
158 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
159 |
"""
|
160 |
demo = gr.ChatInterface(
|
161 |
get_completion,
|
162 |
multimodal=True,
|
163 |
+
# additional_inputs=[
|
164 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
165 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
166 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
167 |
+
# ],
|
168 |
)
|
169 |
|
170 |
+
demo.queue(max_size = 10)
|
171 |
+
|
172 |
|
173 |
if __name__ == "__main__":
|
174 |
demo.launch()
|