|
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 |
|
|
|
from supabase import create_client, Client |
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res = "**Important Announcement:** \n\nThis space is shutting down now. \n\nVisit [chatgpt-4o](https://chatgpt-4o.streamlit.app/) for an improved UI experience and future enhancements." |
|
return res |
|
|
|
|
|
|
|
|
|
title = "Ask Image with GPT-4o" |
|
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.Markdown(label="Answer")], |
|
title = title, |
|
description = description |
|
) |
|
demo.queue(max_size = 20) |
|
|
|
demo.launch(share = True) |
|
|