| import json | |
| import base64 | |
| import os | |
| import requests | |
| import json | |
| from PIL import Image, ImageDraw, ImageFont | |
| import ast | |
| import re | |
| def encode_image_to_base64(img_path): | |
| """ | |
| :param img_path: image path | |
| :return: Base64 encoding | |
| """ | |
| try: | |
| with open(img_path, 'rb') as img_file: | |
| img_data = img_file.read() | |
| base64_str = base64.b64encode(img_data).decode('utf-8') | |
| return base64_str | |
| except Exception as e: | |
| print(f"Encoding Error: {e}") | |
| return None | |
| def infer_image_with_api(image_path, prompt): | |
| url = "http://127.0.0.1:8000/v1/chat/completions" | |
| base64_image = encode_image_to_base64(image_path) | |
| payload = json.dumps({ | |
| "messages": [ | |
| { | |
| "role": "system", | |
| "content": [ | |
| {"type": "text", "text": "你是华为公司开发的多模态大模型,名字是openPangu-VL-7B。你能够处理文本和视觉模态的输入,并给出文本输出。"}, | |
| ] | |
| }, | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "image_url", "image_url": {"url": f"data:image/jpg;base64,{base64_image }"}}, | |
| {"type": "text", "text": prompt}, | |
| ] | |
| } | |
| ], | |
| "model": "pangu_vl", | |
| "max_tokens": 2048, | |
| "temperature": 0, | |
| "stream": False, | |
| }) | |
| headers = { | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.request("POST", url, headers=headers, data=payload) | |
| return json.loads(response.text)["choices"][0]["message"]["content"] | |
| def infer_message_with_api(prompt): | |
| url = "http://127.0.0.1:8000/v1/chat/completions" | |
| payload = json.dumps({ | |
| "messages": [ | |
| { | |
| "role": "system", | |
| "content": [ | |
| {"type": "text", "text": "你是华为公司开发的多模态大模型,名字是openPangu-VL-7B。你能够处理文本和视觉模态的输入,并给出文本输出。"}, | |
| ] | |
| }, | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": prompt}, | |
| ] | |
| } | |
| ], | |
| "model": "pangu_vl", | |
| "max_tokens": 2048, | |
| "temperature": 0, | |
| "stream": False, | |
| }) | |
| headers = { | |
| 'Content-Type': 'application/json' | |
| } | |
| response = requests.request("POST", url, headers=headers, data=payload) | |
| return json.loads(response.text)["choices"][0]["message"]["content"] | |
| res = infer_message_with_api("你好,你是谁?") | |
| print(res) | |