@@ -71,17 +71,22 @@ def execute(self, model_id, prompt, negative_prompt, dialogue_number, dialogue_t
7171 'history_message' : history_message , 'question' : question }, {})
7272
7373 def get_file_base64 (self , image_url ):
74- if isinstance (image_url , list ):
75- image_url = image_url [0 ].get ('file_id' )
76- if isinstance (image_url , str ) and not image_url .startswith ('http' ):
77- file = QuerySet (File ).filter (id = image_url ).first ()
78- file_bytes = file .get_bytes ()
79- # 如果我不知道content_type 可以用 magic 库去检测
80- file_type = file .file_name .split ("." )[- 1 ].lower ()
81- content_type = mime_types .get (file_type , 'application/octet-stream' )
82- encoded_bytes = base64 .b64encode (file_bytes )
83- return f'data:{ content_type } ;base64,{ encoded_bytes .decode ()} '
84- return image_url
74+ try :
75+ if isinstance (image_url , list ):
76+ image_url = image_url [0 ].get ('file_id' )
77+ if isinstance (image_url , str ) and not image_url .startswith ('http' ):
78+ file = QuerySet (File ).filter (id = image_url ).first ()
79+ file_bytes = file .get_bytes ()
80+ # 如果我不知道content_type 可以用 magic 库去检测
81+ file_type = file .file_name .split ("." )[- 1 ].lower ()
82+ content_type = mime_types .get (file_type , 'application/octet-stream' )
83+ encoded_bytes = base64 .b64encode (file_bytes )
84+ return f'data:{ content_type } ;base64,{ encoded_bytes .decode ()} '
85+ return image_url
86+ except Exception as e :
87+ raise ValueError (
88+ gettext ("Failed to obtain the image" ))
89+
8590
8691 def generate_history_ai_message (self , chat_record ):
8792 for val in chat_record .details .values ():
0 commit comments