Spaces:
Running
Running
Commit
·
2d9a138
1
Parent(s):
a0a6f9a
newwwww
Browse files- model/generate.py +20 -68
model/generate.py
CHANGED
@@ -82,55 +82,33 @@ def get_optimal_model_for_memory():
|
|
82 |
available_memory = psutil.virtual_memory().available / (1024 * 1024) # MB
|
83 |
logger.info(f"Available memory: {available_memory:.1f}MB")
|
84 |
|
85 |
-
if available_memory <
|
86 |
return None # Use template fallback
|
87 |
-
elif available_memory <
|
88 |
return "microsoft/DialoGPT-small"
|
89 |
else:
|
90 |
return "distilgpt2"
|
91 |
|
92 |
def load_model_with_memory_optimization(model_name):
|
93 |
-
"""Load model with low memory settings
|
94 |
try:
|
95 |
logger.info(f"Loading {model_name} with memory optimizations...")
|
96 |
|
97 |
-
|
98 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
99 |
-
model_name,
|
100 |
-
padding_side='left',
|
101 |
-
use_fast=True
|
102 |
-
)
|
103 |
|
104 |
-
# Fix tokenizer pad token issue
|
105 |
if tokenizer.pad_token is None:
|
106 |
-
|
107 |
-
tokenizer.pad_token = tokenizer.eos_token
|
108 |
-
else:
|
109 |
-
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
110 |
|
111 |
-
# Load model with corrected parameters
|
112 |
model = AutoModelForCausalLM.from_pretrained(
|
113 |
model_name,
|
114 |
-
torch_dtype=torch.float16
|
115 |
-
device_map=
|
116 |
-
low_cpu_mem_usage=
|
117 |
-
use_cache=
|
118 |
-
trust_remote_code=True
|
119 |
)
|
120 |
|
121 |
-
# Move to CPU explicitly if needed
|
122 |
-
if torch.cuda.is_available():
|
123 |
-
model = model.to('cpu')
|
124 |
-
|
125 |
model.eval()
|
126 |
-
|
127 |
-
# Only enable gradient checkpointing if model supports it
|
128 |
-
if hasattr(model, 'gradient_checkpointing_enable'):
|
129 |
-
try:
|
130 |
-
model.gradient_checkpointing_enable()
|
131 |
-
except Exception as e:
|
132 |
-
logger.warning(f"Could not enable gradient checkpointing: {e}")
|
133 |
-
|
134 |
logger.info(f"✅ Model {model_name} loaded successfully")
|
135 |
return tokenizer, model
|
136 |
|
@@ -190,20 +168,6 @@ def generate_authentication_tests(matches: List[str]) -> List[Dict]:
|
|
190 |
],
|
191 |
"expected": "Login fails with appropriate error message 'Invalid credentials'",
|
192 |
"postconditions": ["User remains on login page", "Account security maintained"],
|
193 |
-
"test_data": "Invalid username: [email protected], Valid password: Test@123"
|
194 |
-
},
|
195 |
-
{
|
196 |
-
"title": "Invalid Password Login",
|
197 |
-
"description": "Verify that login fails with invalid password",
|
198 |
-
"preconditions": ["Application is accessible"],
|
199 |
-
"steps": [
|
200 |
-
"Navigate to login page",
|
201 |
-
"Enter valid username",
|
202 |
-
"Enter invalid password",
|
203 |
-
"Click login button"
|
204 |
-
],
|
205 |
-
"expected": "Login fails with appropriate error message 'Invalid credentials'",
|
206 |
-
"postconditions": ["User remains on login page", "Account security maintained"],
|
207 |
"test_data": "Valid username: [email protected], Invalid password: WrongPass123"
|
208 |
},
|
209 |
{
|
@@ -595,7 +559,7 @@ def parse_generated_test_cases(generated_text: str) -> List[Dict]:
|
|
595 |
return test_cases
|
596 |
|
597 |
def generate_with_ai_model(srs_text: str, tokenizer, model) -> List[Dict]:
|
598 |
-
"""Generate test cases using AI model
|
599 |
max_input_length = 300
|
600 |
if len(srs_text) > max_input_length:
|
601 |
srs_text = srs_text[:max_input_length]
|
@@ -607,40 +571,27 @@ Test Cases:
|
|
607 |
1."""
|
608 |
|
609 |
try:
|
610 |
-
|
611 |
-
inputs = tokenizer(
|
612 |
prompt,
|
613 |
return_tensors="pt",
|
614 |
max_length=200,
|
615 |
-
truncation=True
|
616 |
-
padding=True,
|
617 |
-
return_attention_mask=True
|
618 |
)
|
619 |
|
620 |
-
# Extract input_ids and attention_mask
|
621 |
-
input_ids = inputs['input_ids']
|
622 |
-
attention_mask = inputs['attention_mask']
|
623 |
-
|
624 |
with torch.no_grad():
|
625 |
outputs = model.generate(
|
626 |
-
|
627 |
-
attention_mask=attention_mask, # Provide attention mask
|
628 |
max_new_tokens=150,
|
629 |
num_return_sequences=1,
|
630 |
temperature=0.7,
|
631 |
do_sample=True,
|
632 |
-
pad_token_id=tokenizer.
|
633 |
-
|
634 |
-
use_cache=True,
|
635 |
)
|
636 |
|
637 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
638 |
-
|
639 |
-
|
640 |
-
del inputs, input_ids, attention_mask, outputs
|
641 |
-
if torch.cuda.is_available():
|
642 |
-
torch.cuda.empty_cache()
|
643 |
-
|
644 |
return parse_generated_test_cases(generated_text)
|
645 |
|
646 |
except Exception as e:
|
@@ -729,4 +680,5 @@ def get_algorithm_reason(model_name: str) -> str:
|
|
729 |
"Uses comprehensive pattern matching, requirement analysis, and structured test case templates for robust test coverage.")
|
730 |
else:
|
731 |
return ("Model selected based on optimal tradeoff between memory usage, language generation capability, "
|
732 |
-
"and test case quality requirements.")
|
|
|
|
82 |
available_memory = psutil.virtual_memory().available / (1024 * 1024) # MB
|
83 |
logger.info(f"Available memory: {available_memory:.1f}MB")
|
84 |
|
85 |
+
if available_memory < 300:
|
86 |
return None # Use template fallback
|
87 |
+
elif available_memory < 600:
|
88 |
return "microsoft/DialoGPT-small"
|
89 |
else:
|
90 |
return "distilgpt2"
|
91 |
|
92 |
def load_model_with_memory_optimization(model_name):
|
93 |
+
"""Load model with low memory settings."""
|
94 |
try:
|
95 |
logger.info(f"Loading {model_name} with memory optimizations...")
|
96 |
|
97 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side='left', use_fast=True)
|
|
|
|
|
|
|
|
|
|
|
98 |
|
|
|
99 |
if tokenizer.pad_token is None:
|
100 |
+
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
|
|
|
101 |
|
|
|
102 |
model = AutoModelForCausalLM.from_pretrained(
|
103 |
model_name,
|
104 |
+
torch_dtype=torch.float16,
|
105 |
+
device_map="cpu",
|
106 |
+
low_cpu_mem_usage=True,
|
107 |
+
use_cache=False,
|
|
|
108 |
)
|
109 |
|
|
|
|
|
|
|
|
|
110 |
model.eval()
|
111 |
+
model.gradient_checkpointing_enable()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
logger.info(f"✅ Model {model_name} loaded successfully")
|
113 |
return tokenizer, model
|
114 |
|
|
|
168 |
],
|
169 |
"expected": "Login fails with appropriate error message 'Invalid credentials'",
|
170 |
"postconditions": ["User remains on login page", "Account security maintained"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
"test_data": "Valid username: [email protected], Invalid password: WrongPass123"
|
172 |
},
|
173 |
{
|
|
|
559 |
return test_cases
|
560 |
|
561 |
def generate_with_ai_model(srs_text: str, tokenizer, model) -> List[Dict]:
|
562 |
+
"""Generate test cases using AI model"""
|
563 |
max_input_length = 300
|
564 |
if len(srs_text) > max_input_length:
|
565 |
srs_text = srs_text[:max_input_length]
|
|
|
571 |
1."""
|
572 |
|
573 |
try:
|
574 |
+
inputs = tokenizer.encode(
|
|
|
575 |
prompt,
|
576 |
return_tensors="pt",
|
577 |
max_length=200,
|
578 |
+
truncation=True
|
|
|
|
|
579 |
)
|
580 |
|
|
|
|
|
|
|
|
|
581 |
with torch.no_grad():
|
582 |
outputs = model.generate(
|
583 |
+
inputs,
|
|
|
584 |
max_new_tokens=150,
|
585 |
num_return_sequences=1,
|
586 |
temperature=0.7,
|
587 |
do_sample=True,
|
588 |
+
pad_token_id=tokenizer.eos_token_id,
|
589 |
+
use_cache=False,
|
|
|
590 |
)
|
591 |
|
592 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
593 |
+
del inputs, outputs
|
594 |
+
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
|
|
|
|
|
|
|
|
595 |
return parse_generated_test_cases(generated_text)
|
596 |
|
597 |
except Exception as e:
|
|
|
680 |
"Uses comprehensive pattern matching, requirement analysis, and structured test case templates for robust test coverage.")
|
681 |
else:
|
682 |
return ("Model selected based on optimal tradeoff between memory usage, language generation capability, "
|
683 |
+
"and test case quality requirements.")
|
684 |
+
|