Skip to content

Commit

Permalink
🐛 Bug: Fix the bug where grok cannot read webp format images.
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Nov 27, 2024
1 parent afbea4f commit 76396bf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ pex -r requirements.txt \
## Sponsors

We thank the following sponsors for their support:
<!-- ¥1000 -->
- @PowerHunter: ¥1800
<!-- ¥2050 -->
- @PowerHunter: ¥2000
- @ioi:¥50

## How to sponsor us
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ pex -r requirements.txt \
## 赞助商

我们感谢以下赞助商的支持:
<!-- ¥1000 -->
- @PowerHunter:¥1800
<!-- ¥2050 -->
- @PowerHunter:¥2000
- @ioi:¥50

## 如何赞助我们
Expand Down
20 changes: 20 additions & 0 deletions request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import httpx
import base64
import urllib.parse
from PIL import Image
import io

from models import RequestModel
from utils import c35s, c3s, c3o, c3h, gem, BaseAPI, get_model_dict, provider_api_circular_list, safe_get
Expand Down Expand Up @@ -78,6 +80,24 @@ async def get_image_message(base64_image, engine = None):
semicolon_index = base64_image.index(";")
image_type = base64_image[colon_index + 1:semicolon_index]

if image_type == "image/webp":
# 将webp转换为png

# 解码base64获取图片数据
image_data = base64.b64decode(base64_image.split(",")[1])

# 使用PIL打开webp图片
image = Image.open(io.BytesIO(image_data))

# 转换为PNG格式
png_buffer = io.BytesIO()
image.save(png_buffer, format="PNG")
png_base64 = base64.b64encode(png_buffer.getvalue()).decode('utf-8')

# 返回PNG格式的base64
base64_image = f"data:image/png;base64,{png_base64}"
image_type = "image/png"

if "gpt" == engine or "openrouter" == engine:
return {
"type": "image_url",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
xue
pytest
pillow
uvicorn
fastapi
aiofiles
Expand Down

0 comments on commit 76396bf

Please sign in to comment.