AightBits

LLM Multi-Stage Language Translation

Dec 21st, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | Source Code | 0 0
  1. import requests
  2. import argparse
  3. import json
  4.  
  5. # Constants for configuration
  6. API_ENDPOINT = "http://localhost:5000/v1/completions"
  7. API_KEY = None  # Replace with your API key if required
  8. DEFAULT_MODEL = "text-davinci-003"
  9. DEFAULT_TEMPERATURE = 0.7
  10. HEADERS = {
  11.     "Content-Type": "application/json",
  12.     "Authorization": f"Bearer {API_KEY}" if API_KEY else ""
  13. }
  14.  
  15. def make_api_call(prompt, max_tokens=1000, temperature=DEFAULT_TEMPERATURE, model=DEFAULT_MODEL):
  16.     """
  17.    Makes a call to the API using the `requests` library.
  18.  
  19.    Args:
  20.        prompt (str): The prompt to send to the API.
  21.        max_tokens (int): Maximum tokens for the response.
  22.        temperature (float): Sampling temperature.
  23.        model (str): Model to use for the API call.
  24.  
  25.    Returns:
  26.        str: The response text from the API.
  27.    """
  28.     payload = {
  29.         "model": model,
  30.         "prompt": prompt.strip(),
  31.         "max_tokens": max_tokens,
  32.         "temperature": temperature,
  33.     }
  34.  
  35.     try:
  36.         response = requests.post(API_ENDPOINT, headers=HEADERS, json=payload)
  37.         response.raise_for_status()
  38.         json_response = response.json()
  39.         text = json_response.get("choices", [{}])[0].get("text", "").strip()
  40.         if not text:
  41.             raise RuntimeError("API returned an empty response.")
  42.         return text
  43.     except requests.exceptions.RequestException as e:
  44.         raise RuntimeError(f"API call failed: {e}") from e
  45.  
  46. def translate_text(source_text, source_language, target_language):
  47.     """
  48.    Performs a multi-step translation workflow programmatically.
  49.  
  50.    Args:
  51.        source_text (str): The text to translate.
  52.        source_language (str): The source language.
  53.        target_language (str): The target language.
  54.  
  55.    Returns:
  56.        str: The final translated text.
  57.    """
  58.     # Step 1: Initial Translation
  59.     initial_prompt = f"""
  60. Translate the following text from {source_language} to {target_language}:
  61. {source_text}
  62. Ensure the translation is accurate, fluent, and captures all nuances of the original text. Provide only the translation in {target_language}.
  63.    """
  64.     initial_translation = make_api_call(initial_prompt)
  65.  
  66.     # Step 2: Refine Translation
  67.     refine_prompt = f"""
  68. Improve the following translation for accuracy and fluency. Make sure it captures the original meaning and reads naturally in {target_language}:
  69. {initial_translation}
  70.    """
  71.     refined_translation = make_api_call(refine_prompt)
  72.  
  73.     # Step 3: Compare Translations
  74.     compare_prompt = f"""
  75. Between the two translations below, indicate which is better based on accuracy, fluency, and naturalness in {target_language}. Respond with 'Version 1' or 'Version 2':
  76. Version 1: {initial_translation}
  77. Version 2: {refined_translation}
  78.    """
  79.     best_version = make_api_call(compare_prompt)
  80.  
  81.     if "Version 1" in best_version:
  82.         return initial_translation
  83.     elif "Version 2" in best_version:
  84.         return refined_translation
  85.     else:
  86.         raise ValueError("Unexpected output from API during comparison.")
  87.  
  88. if __name__ == "__main__":
  89.     parser = argparse.ArgumentParser(description="Translate text from one language to another and save the result to a file.")
  90.     parser.add_argument("infile", help="Path to the input file containing the text to translate.")
  91.     parser.add_argument("outfile", help="Path to the output file where the translated text will be saved.")
  92.     parser.add_argument("source_language", help="Language of the input text (e.g., 'English').")
  93.     parser.add_argument("target_language", help="Language for the translated text (e.g., 'Spanish').")
  94.     args = parser.parse_args()
  95.  
  96.     try:
  97.         with open(args.infile, "r", encoding="utf-8") as infile:
  98.             source_text = infile.read()
  99.  
  100.         translated_text = translate_text(source_text, args.source_language, args.target_language)
  101.  
  102.         with open(args.outfile, "w", encoding="utf-8") as outfile:
  103.             outfile.write(translated_text)
  104.  
  105.     except FileNotFoundError as e:
  106.         print(f"Error: {e}")
  107.         exit(1)
  108.     except RuntimeError as e:
  109.         print(f"Error during translation: {e}")
  110.         exit(1)
  111.     except ValueError as e:
  112.         print(f"Translation error: {e}")
  113.         exit(1)
  114.     except Exception as e:
  115.         print(f"Unexpected error: {e}")
  116.         exit(1)
Add Comment
Please, Sign In to add comment