XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

Add image metadata (width, height, file_size) to usage statistics for image responses

Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>

40a32561
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
提交于

代码差异

1 个文件 +20 -0
Modified g4f/gui/server/api.py +20 -0
@@ -223,6 +223,7 @@ class Api:
223 223 yield self._format_json("preview", chunk.to_string(), urls=chunk.urls, alt=chunk.alt)
224 224 elif isinstance(chunk, MediaResponse):
225 225 media = chunk
226 image_metadata = None
226 227 if download_media or chunk.get("cookies"):
227 228 chunk.alt = format_media_prompt(kwargs.get("messages"), chunk.alt)
228 229 width, height = get_width_height(chunk.get("width"), chunk.get("height"))
@@ -249,8 +250,27 @@ class Api:
249 250 except Exception as e:
250 251 logger.exception(e)
251 252 options["target_paths"] = target_paths
253 # Collect image metadata for usage statistics
254 if isinstance(chunk, ImageResponse):
255 image_metadata = {
256 "images": len(target_paths),
257 "width": width,
258 "height": height,
259 }
260 # Calculate total file size
261 total_size = 0
262 for path in target_paths:
263 try:
264 total_size += os.path.getsize(path)
265 except OSError:
266 pass
267 if total_size > 0:
268 image_metadata["file_size"] = total_size
252 269 media = ImageResponse(urls, chunk.alt, options) if isinstance(chunk, ImageResponse) else VideoResponse(media, chunk.alt)
253 270 yield self._format_json("content", str(media), urls=media.urls, alt=media.alt)
271 # Yield usage statistics with image metadata for image responses
272 if image_metadata:
273 yield self._format_json("usage", image_metadata)
254 274 elif isinstance(chunk, SynthesizeData):
255 275 yield self._format_json("synthesize", chunk.get_dict())
256 276 elif isinstance(chunk, TitleGeneration):