Preethamreddy799 commited on
Commit
4682e8b
·
1 Parent(s): 3720c80

new update

Browse files
Files changed (1) hide show
  1. app.py +62 -46
app.py CHANGED
@@ -1,16 +1,61 @@
1
  import streamlit as st
2
  import numpy as np
3
- from tensorflow.keras.preprocessing.text import Tokenizer
4
- from tensorflow.keras.preprocessing.sequence import pad_sequences
5
  from tensorflow.keras.models import load_model
6
  from huggingface_hub import hf_hub_download
 
 
 
 
7
 
8
- # Load model from Hugging Face
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def load_model_test_steps():
10
  repo_id = 'Preethamreddy799/NLP_MODEL'
11
- filename = 'model_test_steps.h5' # Assuming the model is in HDF5 format
12
 
13
- # Download the model from Hugging Face repository
14
  cached_model_path = hf_hub_download(repo_id=repo_id, filename=filename)
15
 
16
  # Load the model from the cached path
@@ -19,45 +64,13 @@ def load_model_test_steps():
19
  print(f"Model loaded successfully from {cached_model_path}")
20
  return model
21
 
22
- model = load_model_test_steps()
23
-
24
- # Initialize Tokenizer (Should match training tokenizer)
25
- tokenizer = Tokenizer(num_words=1000)
26
-
27
- # Function to preprocess text data
28
- def preprocess_text(input_text):
29
- # Convert text to lowercase
30
- input_text = input_text.lower()
31
-
32
- # Remove punctuation (Optional, depending on model)
33
- input_text = ''.join([char for char in input_text if char.isalnum() or char.isspace()])
34
-
35
- # Fit tokenizer (you should ideally fit the tokenizer during training and save it)
36
- tokenizer.fit_on_texts([input_text])
37
-
38
- # Convert the input text to a sequence of integers
39
- sequence = tokenizer.texts_to_sequences([input_text])
40
-
41
- # Pad the sequence to ensure uniform input size
42
- input_features = pad_sequences(sequence, maxlen=100) # Ensure length matches the expected input (100)
43
-
44
- # Reshape input to match model input shape: (batch_size, time_steps, features)
45
- input_features = np.reshape(input_features, (input_features.shape[0], input_features.shape[1], 1))
46
-
47
- return input_features
48
-
49
- # Function to generate test steps
50
- def generate_test_steps(acceptance_criteria):
51
- # Preprocess the input text
52
- input_features = preprocess_text(f"{acceptance_criteria}")
53
-
54
- # Generate prediction
55
- predicted_steps = model.predict(input_features)
56
- return predicted_steps
57
 
58
  # Streamlit App
59
  st.title("Test Case Steps Generator")
60
- st.write("This app generates test steps based on Test Case Acceptance Criteria")
61
 
62
  # Input section
63
  acceptance_criteria = st.text_area("Enter Test Case Acceptance Criteria")
@@ -65,14 +78,17 @@ acceptance_criteria = st.text_area("Enter Test Case Acceptance Criteria")
65
  # Generate Test Steps
66
  if st.button("Generate Test Steps"):
67
  if acceptance_criteria:
68
- if model:
69
- # Call the function to generate predictions
70
- test_steps = generate_test_steps(acceptance_criteria)
 
 
 
71
 
72
  # Display the results
73
  st.subheader("Generated Test Steps")
74
- st.write(test_steps)
75
  else:
76
  st.error("Model not loaded. Please check the model repository and file.")
77
  else:
78
- st.warning("Please fill Acceptance Criteria")
 
1
  import streamlit as st
2
  import numpy as np
 
 
3
  from tensorflow.keras.models import load_model
4
  from huggingface_hub import hf_hub_download
5
+ from nltk.corpus import stopwords
6
+ from nltk.stem import WordNetLemmatizer
7
+ import gensim
8
+ import json
9
 
10
+ # Load stop words and lemmatizer
11
+ stop_words = set(stopwords.words('english'))
12
+ lemmatizer = WordNetLemmatizer()
13
+
14
+ # Function to preprocess input text (matching training pipeline)
15
+ def preprocess_text(input_text, word2vec_model):
16
+ # Convert to lowercase
17
+ input_text = input_text.lower()
18
+
19
+ # Tokenize words
20
+ tokens = input_text.split()
21
+
22
+ # Remove stop words
23
+ tokens = [token for token in tokens if token not in stop_words]
24
+
25
+ # Lemmatize tokens
26
+ tokens = [lemmatizer.lemmatize(token, pos='v') for token in tokens]
27
+
28
+ # Generate Word2Vec embeddings for tokens
29
+ embeddings = []
30
+ for token in tokens:
31
+ if token in word2vec_model.wv:
32
+ embeddings.append(word2vec_model.wv[token])
33
+ else:
34
+ embeddings.append(np.zeros(word2vec_model.vector_size)) # Handle OOV words
35
+
36
+ # Pad or truncate embeddings to match time_steps (e.g., 100)
37
+ max_timesteps = 100
38
+ if len(embeddings) > max_timesteps:
39
+ embeddings = embeddings[:max_timesteps]
40
+ else:
41
+ padding = [np.zeros(word2vec_model.vector_size)] * (max_timesteps - len(embeddings))
42
+ embeddings.extend(padding)
43
+
44
+ # Convert to NumPy array and reshape
45
+ input_features = np.array(embeddings).reshape((1, max_timesteps, word2vec_model.vector_size))
46
+ return input_features
47
+
48
+ # Load Word2Vec model
49
+ def load_word2vec_model():
50
+ word2vec_path = '/Users/preethamreddygollapalli/Downloads/word2vec_model.bin' # Update with actual Word2Vec model path
51
+ return gensim.models.Word2Vec.load(word2vec_path)
52
+
53
+ # Load LSTM model from Hugging Face
54
  def load_model_test_steps():
55
  repo_id = 'Preethamreddy799/NLP_MODEL'
56
+ filename = 'model_test_steps.h5' # Update with actual file name
57
 
58
+ # Download the model from Hugging Face
59
  cached_model_path = hf_hub_download(repo_id=repo_id, filename=filename)
60
 
61
  # Load the model from the cached path
 
64
  print(f"Model loaded successfully from {cached_model_path}")
65
  return model
66
 
67
+ # Initialize models
68
+ word2vec_model = load_word2vec_model()
69
+ lstm_model = load_model_test_steps()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  # Streamlit App
72
  st.title("Test Case Steps Generator")
73
+ st.write("This app generates test steps based on Test Case Acceptance Criteria.")
74
 
75
  # Input section
76
  acceptance_criteria = st.text_area("Enter Test Case Acceptance Criteria")
 
78
  # Generate Test Steps
79
  if st.button("Generate Test Steps"):
80
  if acceptance_criteria:
81
+ if lstm_model:
82
+ # Preprocess input text
83
+ input_features = preprocess_text(acceptance_criteria, word2vec_model)
84
+
85
+ # Generate prediction
86
+ predicted_steps = lstm_model.predict(input_features)
87
 
88
  # Display the results
89
  st.subheader("Generated Test Steps")
90
+ st.write(predicted_steps)
91
  else:
92
  st.error("Model not loaded. Please check the model repository and file.")
93
  else:
94
+ st.warning("Please fill Acceptance Criteria.")