check.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import json
  2. import os
  3. import requests
  4. from io import BytesIO
  5. from google import genai
  6. from google.genai import types
  7. from datetime import datetime
  8. from google.genai import Client
  9. from google.genai import types
  10. GEMINI_BASE_URL = os.environ.get("GEMINI_API_BASE_URL", "https://api.openaius.com")
  11. GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY","sk-qnsfJw0vsAitlnrXcOeBYrLbTv9LXfsN1m3jIUfMJagan5IR")
  12. _client: Client | None = None
  13. def _get_client() -> Client:
  14. global _client
  15. if _client is None:
  16. _client = Client(
  17. api_key=GEMINI_API_KEY,
  18. http_options={"base_url": GEMINI_BASE_URL}
  19. )
  20. return _client
  21. def get_image_mime_type(image_url, content=None):
  22. """
  23. 根据URL或内容判断图片的MIME类型
  24. Args:
  25. image_url: 图片URL
  26. content: 图片内容(可选)
  27. Returns:
  28. MIME类型字符串
  29. """
  30. # 首先尝试从URL判断
  31. if image_url.endswith('.png'):
  32. return 'image/png'
  33. elif image_url.endswith('.jpg') or image_url.endswith('.jpeg'):
  34. return 'image/jpeg'
  35. elif image_url.endswith('.gif'):
  36. return 'image/gif'
  37. elif image_url.endswith('.webp'):
  38. return 'image/webp'
  39. # 如果URL无法判断,尝试从内容判断
  40. if content:
  41. # 检查文件头
  42. if content.startswith(b'\x89PNG'):
  43. return 'image/png'
  44. elif content.startswith(b'\xff\xd8\xff'):
  45. return 'image/jpeg'
  46. elif content.startswith(b'GIF'):
  47. return 'image/gif'
  48. elif content.startswith(b'RIFF') and b'WEBP' in content[:12]:
  49. return 'image/webp'
  50. # 默认返回jpeg
  51. return 'image/jpeg'
  52. def download_image_from_url(image_url):
  53. """
  54. 从URL下载图片并返回字节数据和MIME类型
  55. Args:
  56. image_url: 图片URL
  57. Returns:
  58. (图片字节数据, MIME类型),如果失败返回(None, None)
  59. """
  60. try:
  61. response = requests.get(image_url, timeout=30)
  62. response.raise_for_status()
  63. content = response.content
  64. # 尝试从响应头获取Content-Type
  65. content_type = response.headers.get('Content-Type', '')
  66. if content_type and content_type.startswith('image/'):
  67. mime_type = content_type
  68. else:
  69. # 从URL或内容判断
  70. mime_type = get_image_mime_type(image_url, content)
  71. return content, mime_type
  72. except Exception as e:
  73. print(f"❌ 下载图片失败 {image_url}: {e}")
  74. return None, None
  75. def process_image_pair_with_gemini(image1_url, image2_url, prompt, model="gemini-2.5-flash"):
  76. """
  77. 使用Gemini处理成对的图片
  78. Args:
  79. image1_url: 第一张图片的URL
  80. image2_url: 第二张图片的URL
  81. prompt: 提示词
  82. model: 使用的模型名称
  83. Returns:
  84. Gemini的响应文本,如果失败返回None
  85. """
  86. try:
  87. # 下载第一张图片并获取MIME类型
  88. img1_bytes, img1_mime = download_image_from_url(image1_url)
  89. if not img1_bytes:
  90. return None
  91. # 下载第二张图片并获取MIME类型
  92. img2_bytes, img2_mime = download_image_from_url(image2_url)
  93. if not img2_bytes:
  94. return None
  95. # 创建请求内容 - 直接使用内联图片数据,避免使用 files.upload
  96. contents = [
  97. prompt,
  98. types.Part.from_bytes(
  99. data=img1_bytes,
  100. mime_type=img1_mime
  101. ),
  102. types.Part.from_bytes(
  103. data=img2_bytes,
  104. mime_type=img2_mime
  105. )
  106. ]
  107. # 调用Gemini API
  108. response = _get_client().models.generate_content(
  109. model=model,
  110. contents=contents
  111. )
  112. return response.text
  113. except Exception as e:
  114. print(f"❌ 处理图片对时出错: {e}")
  115. return None
  116. if __name__ == "__main__":
  117. prompt = """图2是不是图1的衣服的线稿款式图,而且除了衣服的线稿外没有其他的元素了?是不是只有黑白线稿没有颜色的,是不是不含人台(mannequin)的线稿,是不是衣服的数量和特征都对应上了,是的话回答是,否则回答否"""
  118. result=process_image_pair_with_gemini( "https://testdgxcx-oss.gloria.com.cn/design_image/cropped_20251121_120610_072.png","https://testdgxcx-oss.gloria.com.cn/design_image/cropped_20251121_120624_088.png",prompt)
  119. print(result)