mirror of
				https://github.com/LibreTranslate/LibreTranslate.git
				synced 2025-11-03 19:17:17 -05:00 
			
		
		
		
	feat: support batch translation
This commit is contained in:
		
							parent
							
								
									72248f0050
								
							
						
					
					
						commit
						2c5eba14b4
					
				
							
								
								
									
										29
									
								
								app/app.py
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								app/app.py
									
									
									
									
									
								
							@ -126,10 +126,13 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
 | 
			
		||||
          - in: formData
 | 
			
		||||
            name: q
 | 
			
		||||
            schema:
 | 
			
		||||
              type: string
 | 
			
		||||
              example: Hello world!
 | 
			
		||||
              oneOf:
 | 
			
		||||
                - type: string
 | 
			
		||||
                  example: Hello world!
 | 
			
		||||
                - type: array
 | 
			
		||||
                  example: ['Hello world!']
 | 
			
		||||
            required: true
 | 
			
		||||
            description: Text to translate
 | 
			
		||||
            description: Text(s) to translate
 | 
			
		||||
          - in: formData
 | 
			
		||||
            name: source
 | 
			
		||||
            schema:
 | 
			
		||||
@ -152,8 +155,10 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
 | 
			
		||||
              type: object
 | 
			
		||||
              properties:
 | 
			
		||||
                translatedText:
 | 
			
		||||
                  type: string
 | 
			
		||||
                  description: Translated text
 | 
			
		||||
                  oneOf:
 | 
			
		||||
                    - type: string
 | 
			
		||||
                    - type: array
 | 
			
		||||
                  description: Translated text(s)
 | 
			
		||||
          400:
 | 
			
		||||
            description: Invalid request
 | 
			
		||||
            schema:
 | 
			
		||||
@ -200,8 +205,16 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
 | 
			
		||||
        if not target_lang:
 | 
			
		||||
            abort(400, description="Invalid request: missing target parameter")
 | 
			
		||||
 | 
			
		||||
        batch = isinstance(q, list)
 | 
			
		||||
 | 
			
		||||
        if char_limit != -1:
 | 
			
		||||
            q = q[:char_limit]
 | 
			
		||||
            if batch:
 | 
			
		||||
              chars = sum([len(text) for text in q])
 | 
			
		||||
            else:
 | 
			
		||||
              chars = len(q)
 | 
			
		||||
 | 
			
		||||
            if char_limit < chars:
 | 
			
		||||
              abort(400, description="Invalid request: Request (%d) exceeds character limit (%d)" % (chars, char_limit))
 | 
			
		||||
 | 
			
		||||
        if source_lang == 'auto':
 | 
			
		||||
            candidate_langs = list(filter(lambda l: l.lang in language_map, detect_langs(q)))
 | 
			
		||||
@ -230,7 +243,11 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
 | 
			
		||||
            abort(400, description="%s is not supported" % target_lang)
 | 
			
		||||
 | 
			
		||||
        translator = src_lang.get_translation(tgt_lang)
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
          if batch:
 | 
			
		||||
            return jsonify({"translatedText": [translator.translate(text) for text in q] })
 | 
			
		||||
          else:
 | 
			
		||||
            return jsonify({"translatedText": translator.translate(q) })
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            abort(500, description="Cannot translate text: %s" % str(e))
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user