Views
No views yet
1
2# Load model and tokenizer
3from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4from peft import PeftModel, PeftConfig
5
6# Load the PEFT configuration
7peft_model_id = "sugiv/bluey-poor-flant5"
8peft_config = PeftConfig.from_pretrained(peft_model_id)
9
10# Load the base model
11base_model = AutoModelForSeq2SeqLM.from_pretrained(peft_config.base_model_name_or_path)
12
13# Load the PEFT model
14model = PeftModel.from_pretrained(base_model, peft_model_id)
15
16# Load the tokenizer
17tokenizer = AutoTokenizer.from_pretrained(peft_config.base_model_name_or_path)
18
19# Set the model to evaluation mode
20model.eval()
21
22def generate_transform_prompt(input_text, filter_combination):
23 return f'''You are an advanced text transformation AI. Your task is to {filter_combination['Task']} the given input text according to the specified parameters. {filter_combination['Task'].capitalize()}ing means expressing the same meaning using different words, while maintaining the original intent. Always correct spelling and grammatical errors implicitly.
24
25User: Transform the following text based on these parameters:
26Task: {filter_combination['Task']}
27Tone: {filter_combination['Tone']}
28Target Audience: {filter_combination['Target Audience']}
29Complexity: {filter_combination['Complexity']}
30Purpose: {filter_combination['Purpose']}
31Style: {filter_combination['Style']}
32Verbosity: {filter_combination['Verbosity']}
33
34Input Text: {input_text}
35
36Instructions:
371. {filter_combination['Task']} the text according to the specified parameters.
382. Maintain the original meaning, context, jargon, and entities.
393. Adjust the language complexity and verbosity as specified.
404. Optimize the text for the target audience and purpose.
415. Ensure the output is coherent and flows naturally.
426. Implicitly correct any spelling or grammatical errors.
43
44Transformed text:'''
45
46# Example usage
47input_text = "The quick brown fox jumps over the lazy dog."
48filter_combination = {
49 "Task": "Rephrase",
50 "Tone": "Professional",
51 "Target Audience": "Business executives",
52 "Complexity": "Advanced",
53 "Purpose": "Inform",
54 "Style": "Analytical",
55 "Verbosity": "Concise"
56}
57
58prompt = generate_transform_prompt(input_text, filter_combination)
59inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
60outputs = model.generate(**inputs, max_length=150, num_return_sequences=1)
61transformed_text = tokenizer.decode(outputs, skip_special_tokens=True)
62print(transformed_text)