Guest User

Untitled

a guest
Nov 11th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. # Import necessary libraries
  2. import re
  3. from langchain_ollama.llms import OllamaLLM
  4. from langchain.prompts import ChatPromptTemplate
  5. from langchain_core.output_parsers import StrOutputParser
  6.  
  7. # Dataset: A conversation between individuals of British & American origin
  8. # odd Index  -> British Origin
  9. # even Index -> American Origin
  10. sentences = [
  11.     "I think the weather's absolutely dreadful today, isn't it?",
  12.     "Yeah, it's so hot out here. I can't stand the heat!",
  13.     "Have you been to the new pub down the street? They serve a brilliant pint of ale.",
  14.     "Oh, I love craft beer. The local brewery just released a new IPA.",
  15.     "I fancy a cup of tea. Would you like one as well?",
  16.     "Coffee's more my thing. A strong black coffee to get me through the day.",
  17.     "I do hope this meeting doesn't run too late. I need to catch the last train.",
  18.     "No worries, the meeting should wrap up soon. I'm just hoping to get out in time for happy hour.",
  19.     "I prefer a good stroll through the park in the afternoon.",
  20.     "I usually go for a run after work to clear my head.",
  21.     "I can't quite believe how expensive rent is getting in London these days.",
  22.     "Housing prices in California are crazy, too. It's getting out of hand.",
  23.     "I reckon I'll stay in this weekend and catch up on some reading.",
  24.     "I'm planning to binge-watch a few episodes of my favorite series this weekend.",
  25.     "Oh, don't get me started on the traffic in central London. It's an absolute nightmare.",
  26.     "Traffic here is brutal too, especially during rush hour. It's always backed up."
  27. ]
  28.  
  29. # Step 2: Load the Indianism rules from rules.txt
  30. with open("newrules.txt", "r", encoding="utf-8") as file:
  31.     indianism_rules = file.read()
  32.    
  33. # Step 3: Initialize the Ollama model without retaining any previous context
  34. llm = OllamaLLM(model="llama3.1:8b", temperature=0, base_url="http://localhost:11434/")
  35.  
  36. # Step 4: Define the prompt dynamically with rules, enforcing sentence independence
  37. rules_text = "".join(indianism_rules)  # Concatenate rules into a single text block
  38. prompt = ChatPromptTemplate([
  39.     ("system", "For each input, generate an independent Indianized English sentence according to the rules provided, "
  40.                "treating each sentence as standalone without reference to previous responses. "
  41.                "Output only the Indianized sentence, without commentary, formatted as: !!~!! sentence !!~!!. "
  42.                "If an Indianized sentence cannot be generated, return !!~!! None !!~!!. Rules: {rules}"),
  43.     ("human", "{sentence}")
  44. ])
  45.  
  46. # Output parser for string type
  47. parser = StrOutputParser()
  48.  
  49. # Create an LLMChain for transformation
  50. chain = prompt | llm | parser
  51.  
  52. # Define the regex pattern to extract the output within !!~!! delimiters
  53. output_pattern = r"!!~!!(.*?)!!~!!"
  54.  
  55. # Process each sentence with enhanced error handling
  56. for sentence in sentences:
  57.     try:
  58.         # Invoke the model with the rules and sentence, ensuring fresh context
  59.         response = chain.invoke({
  60.             "rules": rules_text,
  61.             "sentence": sentence
  62.         })
  63.  
  64.         # Extract the Indianized sentence using regex, with DOTALL in case response spans multiple lines
  65.         match = re.search(output_pattern, response, re.DOTALL)
  66.  
  67.         # Check if the match exists and is not "None"
  68.         if match and match.group(1).strip().lower() != "none":
  69.             indianized_sentence = match.group(1).strip()
  70.             print("Original:", sentence)
  71.             print("With Indianism:", indianized_sentence, "\n")
  72.         else:
  73.             # Separate skipped transformations clearly
  74.             print(f"Original: {sentence}")
  75.             print("With Indianism: No valid transformation available. Skipping it.\n")
  76.  
  77.     except Exception as e:
  78.         print(f"Error processing sentence '{sentence}': {e}. Skipping it!\n")
  79.  
Advertisement
Add Comment
Please, Sign In to add comment