yangtb24 commited on
Commit
064de81
·
verified ·
1 Parent(s): a561b24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -636,29 +636,29 @@ def handsome_chat_completions():
636
  images = response_json.get("images", [])
637
  openai_images = []
638
  for item in images:
639
- if isinstance(item, dict) and "url" in item:
640
- image_url = item["url"]
641
- logging.info(f"Fetched image URL: {image_url}") # Log the URL
642
- try:
643
- image_data = requests.get(image_url, stream=True).raw
644
- image = Image.open(image_data)
645
- buffered = io.BytesIO()
646
- image.save(buffered, format="PNG")
647
- img_str = base64.b64encode(buffered.getvalue()).decode()
648
- openai_images.append({"b64_json": img_str})
649
- except Exception as e:
650
- logging.error(f"图片转base64失败: {e}")
651
- openai_images.append({"url": image_url})
652
- else:
653
- logging.error(f"无效的图片数据: {item}")
654
- openai_images.append({"url": item})
655
 
656
  # Construct the expected JSON output
657
  response_data = {
658
- "images": openai_images, # use this key so the frontend can parse
659
  "created": int(time.time()),
660
  "timings": {
661
- "inference": total_time # add inference time as shown in example
662
  }
663
  }
664
  if "seed" in response_json:
@@ -693,8 +693,8 @@ def handsome_chat_completions():
693
  logging.error(f"请求转发异常: {e}")
694
  return jsonify({"error": str(e)}), 500
695
  else:
696
- # Existing text-based model handling logic
697
- try:
698
  start_time = time.time()
699
  response = requests.post(
700
  TEST_MODEL_ENDPOINT,
@@ -707,7 +707,6 @@ def handsome_chat_completions():
707
  return jsonify(response.json()), 429
708
 
709
  if data.get("stream", False):
710
- # ... (Existing stream response handling)
711
  def generate():
712
  first_chunk_time = None
713
  full_response_content = ""
@@ -815,14 +814,10 @@ def handsome_chat_completions():
815
  request_timestamps.append(time.time())
816
  token_counts.append(prompt_tokens+completion_tokens)
817
 
818
-
819
-
820
-
821
-
822
- return Response(
823
- stream_with_context(generate()),
824
- content_type=response.headers['Content-Type']
825
- )
826
  else:
827
  response.raise_for_status()
828
  end_time = time.time()
@@ -859,7 +854,8 @@ def handsome_chat_completions():
859
  item.get("type") == "text"
860
  ):
861
  user_content += (
862
- item.get("text", "") + " "
 
863
  )
864
 
865
  user_content = user_content.strip()
 
636
  images = response_json.get("images", [])
637
  openai_images = []
638
  for item in images:
639
+ if isinstance(item, dict) and "url" in item:
640
+ image_url = item["url"]
641
+ logging.info(f"Extracted image URL: {image_url}") # Log the URL
642
+ try:
643
+ image_data = requests.get(image_url, stream=True).raw
644
+ image = Image.open(image_data)
645
+ buffered = io.BytesIO()
646
+ image.save(buffered, format="PNG")
647
+ img_str = base64.b64encode(buffered.getvalue()).decode()
648
+ openai_images.append({"b64_json": img_str})
649
+ except Exception as e:
650
+ logging.error(f"图片转base64失败: {e}")
651
+ openai_images.append({"url": image_url})
652
+ else:
653
+ logging.error(f"无效的图片数据: {item}")
654
+ openai_images.append({"url": item})
655
 
656
  # Construct the expected JSON output
657
  response_data = {
658
+ "images": openai_images,
659
  "created": int(time.time()),
660
  "timings": {
661
+ "inference": total_time
662
  }
663
  }
664
  if "seed" in response_json:
 
693
  logging.error(f"请求转发异常: {e}")
694
  return jsonify({"error": str(e)}), 500
695
  else:
696
+ # Existing text-based model handling logic
697
+ try:
698
  start_time = time.time()
699
  response = requests.post(
700
  TEST_MODEL_ENDPOINT,
 
707
  return jsonify(response.json()), 429
708
 
709
  if data.get("stream", False):
 
710
  def generate():
711
  first_chunk_time = None
712
  full_response_content = ""
 
814
  request_timestamps.append(time.time())
815
  token_counts.append(prompt_tokens+completion_tokens)
816
 
817
+ return Response(
818
+ stream_with_context(generate()),
819
+ content_type=response.headers['Content-Type']
820
+ )
 
 
 
 
821
  else:
822
  response.raise_for_status()
823
  end_time = time.time()
 
854
  item.get("type") == "text"
855
  ):
856
  user_content += (
857
+ item.get("text", "") +
858
+ " "
859
  )
860
 
861
  user_content = user_content.strip()