|
import gradio as gr |
|
import requests |
|
import os |
|
import json |
|
from google.oauth2 import service_account |
|
from google.cloud import storage |
|
|
|
|
|
|
|
|
|
|
|
import base64 |
|
|
|
def encode_image(image_path): |
|
with open(image_path, "rb") as image_file: |
|
return base64.b64encode(image_file.read()).decode('utf-8') |
|
|
|
|
|
openai_api_key = os.environ.get('openai_api_key') |
|
|
|
|
|
def upload_image_to_gcs_blob(image): |
|
|
|
google_creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON") |
|
|
|
creds_json = json.loads(google_creds) |
|
credentials = service_account.Credentials.from_service_account_info(creds_json) |
|
|
|
|
|
storage_client = storage.Client(credentials=credentials, project=creds_json['project_id']) |
|
|
|
bucket_name = os.environ.get('bucket_name') |
|
bucket = storage_client.bucket(bucket_name) |
|
|
|
destination_blob_name = os.path.basename(image) |
|
blob = bucket.blob(destination_blob_name) |
|
|
|
blob.upload_from_filename(image) |
|
|
|
public_url = blob.public_url |
|
|
|
return public_url |
|
|
|
|
|
def get_supabase_client(): |
|
url = os.environ.get('supabase_url') |
|
key = os.environ.get('supbase_key') |
|
supabase = create_client(url, key) |
|
return supabase |
|
|
|
def supabase_insert_ask_image(question,image,response_content): |
|
supabase = get_supabase_client() |
|
data, count = supabase.table('ask_image').insert({"question": question, "image": image,"response_content":response_content}).execute() |
|
|
|
|
|
|
|
|
|
def ask_image(text,image,api_token=openai_api_key): |
|
public_url = upload_image_to_gcs_blob(image) |
|
print(text) |
|
print(public_url) |
|
print('-----------------------\n') |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{"type": "text", "text": text}, |
|
{ |
|
"type": "image_url", |
|
"image_url": { |
|
|
|
"url": public_url |
|
}, |
|
}, |
|
], |
|
} |
|
] |
|
|
|
|
|
headers = { |
|
'Authorization': f'Bearer {api_token}' |
|
} |
|
|
|
|
|
data = { |
|
'model': 'gpt-4o', |
|
'messages': messages, |
|
'temperature': 0.7 |
|
} |
|
|
|
|
|
|
|
max_retry = 3 |
|
|
|
for i in range(max_retry): |
|
try: |
|
|
|
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data) |
|
|
|
|
|
response_data = response.json() |
|
response_content = response_data['choices'][0]['message']['content'] |
|
usage = response_data['usage'] |
|
|
|
|
|
|
|
return response_content |
|
|
|
except Exception as e: |
|
|
|
if i == max_retry - 1: |
|
print(f'重试次数已达上限,仍未能成功获取数据,错误信息:{e}') |
|
response_content = '' |
|
usage = {} |
|
return response_content |
|
else: |
|
|
|
print(f'第{i+1}次请求失败,错误信息:{e},准备进行第{i+2}次尝试') |
|
|
|
supabase_insert_ask_image(text,public_url,response_content) |
|
|
|
|
|
|
|
|
|
title = "Ask Image" |
|
description = "Ask anything about your Image with GPT-4o" |
|
|
|
demo = gr.Interface( |
|
fn=ask_image, |
|
inputs=[gr.Text(label="Question"),gr.Image(label='',type='filepath')], |
|
outputs=[gr.Textbox(label="Answer",lines=3)], |
|
title = title, |
|
description = description |
|
) |
|
demo.queue(max_size = 20) |
|
|
|
demo.launch(share = True) |
|
|