import base64
from mimetypes import guess_type
# Function to encode a local image into data URL
def local_image_to_data_url(image_path):
# Guess the MIME type of the image based on the file extension
mime_type, _ = guess_type(image_path)
if mime_type is None:
mime_type = 'application/octet-stream' # Default MIME type if none is found
# Read and encode the image file
with open(image_path, "rb") as image_file:
base64_encoded_data = base64.b64encode(image_file.read()).decode('utf-8')
# Construct the data URL
return f"data:{mime_type};base64,{base64_encoded_data}"
# Example usage
image_path = '<path_to_image>'
data_url = local_image_to_data_url(image_path)
print("Data URL:", data_url)
得到 base64 字串後,在輸入框輸入以下格式即可
"data:image/jpeg;base64,<your_image_data>"
Gradio Sample code
# Please update your assistant id and api key here:
API_KEY = ""
ASSISTANT_ID = ""
ASSISTANT_API = "https://prod.dvcbot.net/api/assts/v1"
import micropip; await micropip.install('openai==1.39.0'); from pyodide.http import pyfetch; import httpx; import gradio as gr
from openai import AsyncOpenAI
from datetime import datetime
import json
class Transport(httpx.AsyncBaseTransport):
async def handle_async_request(self, request: httpx.Request):
resp = await pyfetch(str(request.url), method=request.method, headers=dict(request.headers.items()), body=json.dumps(json.loads(request.content), ensure_ascii=False).encode() if request.method != 'GET' and request.method != 'DELETE' else None)
return httpx.Response(resp.status, headers=resp.headers, stream=httpx.ByteStream(await resp.bytes()))
client = AsyncOpenAI(base_url=ASSISTANT_API, api_key=API_KEY, http_client=httpx.AsyncClient(transport=Transport()))
if __name__ == "__main__":
async def send_message(message, history):
thread = await client.beta.threads.create(messages=[{"role": "user" if i == 0 else "assistant", "content": c} for p in history for i, c in enumerate(p)])
await client.beta.threads.messages.create(thread_id=thread.id, role='user', content=message)
run = await client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=ASSISTANT_ID, additional_instructions=f"\nThe current time is: {datetime.now()}", timeout=2.0)
while run.status == 'requires_action' and run.required_action:
outputs = []
for call in run.required_action.submit_tool_outputs.tool_calls:
resp = await client._client.post(ASSISTANT_API+'/pluginapi', params={"tid": thread.id, "aid": ASSISTANT_ID, "pid": call.function.name}, headers={"Authorization": "Bearer " + API_KEY}, json=json.loads(call.function.arguments))
outputs.append({"tool_call_id": call.id, "output": resp.text[:8000]})
run = await client.beta.threads.runs.submit_tool_outputs_and_poll(run_id=run.id, thread_id=thread.id, tool_outputs=outputs, timeout=2.0)
if run.status == 'failed' and run.last_error:
return run.last_error.model_dump_json()
msgs = await client.beta.threads.messages.list(thread_id=thread.id, order='desc')
await client.beta.threads.delete(thread_id=thread.id)
return msgs.data[0].content[0].text.value
demo = gr.ChatInterface(send_message)
demo.launch()