ivmpfa commited on
Commit
f70eb75
·
verified ·
1 Parent(s): 26bc490

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -71
app.py CHANGED
@@ -1,76 +1,43 @@
1
- import torch
2
- from transformers import pipeline, set_seed
3
- import time
4
 
5
- # Set random seed for reproducibility
6
- set_seed(42)
7
 
8
- # Initialize model pipeline with better error handling
9
- print("Initializing distilgpt2 model pipeline...")
10
 
11
- def initialize_model():
12
- try:
13
- # Set up the model pipeline with distilgpt2
14
- model = pipeline(
15
- "text-generation",
16
- model="distilgpt2", # Load the distilgpt2 model
17
- tokenizer="distilgpt2", # Ensure tokenizer is loaded as well
18
- torch_dtype=torch.float32 # Ensures compatibility with CPU
19
- )
20
- print("Model pipeline initialized successfully.")
21
- return model
22
- except Exception as e:
23
- print(f"Error during model initialization: {e}")
24
- return None
25
-
26
- # Retry logic to handle initialization issues
27
- model = None
28
- attempts = 0
29
- max_attempts = 3
30
- while model is None and attempts < max_attempts:
31
- print(f"Attempt {attempts + 1}/{max_attempts} to initialize the model...")
32
- model = initialize_model()
33
- attempts += 1
34
- if model is None:
35
- print(f"Retrying after a delay...")
36
- time.sleep(10) # Wait 10 seconds before retrying
37
-
38
- if model is None:
39
- print("Failed to initialize the model after several attempts. Exiting.")
40
- exit(1) # Exit if model is not initialized successfully
41
-
42
- # Generate test cases function
43
- def generate_test_cases(requirement):
44
- prompt = f"Generate detailed test cases in JSON format for the following software requirement: '{requirement}'. Please return only the JSON array with test cases."
45
-
46
- try:
47
- print("Generating test cases...")
48
- result = model(prompt, max_length=500, num_return_sequences=1)[0]["generated_text"]
49
- print("Test cases generated successfully.")
50
-
51
- # Clean up output to only return JSON array
52
- clean_result = result.strip().split("\n")[0]
53
- if clean_result.startswith('[') and clean_result.endswith(']'):
54
- return clean_result
55
- else:
56
- return f"Error: Unexpected output format.\n{result.strip()}"
57
- except Exception as e:
58
- print(f"Error during generation: {e}")
59
- return None
60
-
61
- # Example usage
62
- if __name__ == "__main__":
63
- requirements = [
64
- "User login functionality with email and password",
65
- "Search functionality on an e-commerce website",
66
- "Order placement process for an online store",
67
- "Admin dashboard to manage users and content"
68
- ]
69
-
70
- for req in requirements:
71
- print(f"\nTest cases for requirement: {req}")
72
- test_cases = generate_test_cases(req)
73
  if test_cases:
74
- print(test_cases)
 
 
75
  else:
76
- print("Failed to generate test cases.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import backend # Importing backend functions
 
3
 
4
+ # Title
5
+ st.title("Test Case Generator - QA User Story")
6
 
7
+ # Input field for the user story
8
+ user_story = st.text_area("Enter the QA User Story", "")
9
 
10
+ # Button to generate test cases
11
+ if st.button("Generate Test Cases"):
12
+ if user_story:
13
+ with st.spinner("Generating test cases..."):
14
+ test_cases = backend.generate_testcases(user_story)
15
+ st.subheader("Generated Test Cases")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  if test_cases:
17
+ for case in test_cases:
18
+ st.write(f"- **Test Case**: {case.get('test_case', 'N/A')}")
19
+ st.session_state.test_cases = test_cases # Store in session state
20
  else:
21
+ st.error("Failed to generate test cases. Try a clearer input.")
22
+ else:
23
+ st.error("Please enter a user story.")
24
+
25
+ # Export test cases
26
+ st.sidebar.title("Export Test Cases")
27
+ if st.sidebar.button("Export Test Cases as CSV"):
28
+ if 'test_cases' in st.session_state:
29
+ test_cases = st.session_state.test_cases
30
+ csv_content = backend.export_test_cases(test_cases, 'csv')
31
+ if csv_content:
32
+ st.sidebar.download_button(
33
+ label="Download CSV",
34
+ data=csv_content,
35
+ file_name="test_cases.csv",
36
+ mime="text/csv"
37
+ )
38
+ else:
39
+ st.sidebar.error("No test cases available.")
40
+
41
+ # Footer
42
+ st.markdown("---")
43
+ st.write("Built with ❤️ by QA Team")