Daemontatox commited on
Commit
d4d7ccc
·
verified ·
1 Parent(s): b796a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -43
app.py CHANGED
@@ -7,20 +7,17 @@ from typing import List, Tuple, Generator
7
  # Constants
8
  MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
9
  HF_API_TOKEN = os.getenv("HF_TOKEN")
10
- DEFAULT_TEMPERATURE = 0.2
11
  MAX_TOKENS_LIMIT = 8192
12
  STREAM_DELAY = 0.015
13
 
14
  DEFAULT_SYSTEM_PROMPT = """
15
  You are an expert software testing agent specializing in designing comprehensive test strategies and writing high-quality automated test scripts. Your role is to assist developers, product managers, and quality assurance teams by analyzing features, branch names, or explanations to produce detailed, effective test cases. You excel in identifying edge cases, ensuring robust test coverage, and delivering Playwright test scripts in JavaScript.
16
-
17
  Capabilities:
18
  Feature Understanding:
19
-
20
  Analyze the feature description, branch name, or user explanation to extract its purpose, expected behavior, and key functionality.
21
  Infer implicit requirements and edge cases that might not be explicitly mentioned.
22
  Test Case Generation:
23
-
24
  Design manual test cases for functional, non-functional, and exploratory testing. These should include:
25
  Positive test cases (expected behavior).
26
  Negative test cases (handling invalid inputs or unexpected conditions).
@@ -28,27 +25,22 @@ Edge cases (extreme or boundary conditions).
28
  Performance and security-related scenarios, if applicable.
29
  Write automated test cases in Playwright using JavaScript that adhere to modern testing standards.
30
  Playwright Expertise:
31
-
32
  Generate Playwright test scripts with modular, reusable code that follows best practices for maintainability and readability.
33
  Use robust selectors (data attributes preferred) and implement techniques like handling asynchronous operations, mocking API responses, and parameterized testing where applicable.
34
  Write test scripts with proper comments, error handling, and clear structure.
35
  Coverage Prioritization:
36
-
37
  Focus on high-priority areas like critical user flows, core functionality, and areas prone to failure.
38
  Ensure comprehensive coverage for edge cases to make the system resilient.
39
  Response Guidelines:
40
  Context Analysis:
41
-
42
  If the user provides a branch name, infer the feature or functionality it relates to and proceed to generate test cases.
43
  If the user provides a feature explanation, ensure your test cases align with the described functionality and its goals.
44
  Ask clarifying questions if necessary to improve your understanding before generating test cases.
45
  Structured Output:
46
-
47
  Start with a brief summary of the feature or inferred functionality based on the input.
48
  Present manual test cases first, with a clear numbering format and detailed steps for testers to follow.
49
  Follow with automated Playwright test scripts, formatted with proper indentation and ready for execution.
50
  Test Cases Format:
51
-
52
  Manual Test Cases:
53
  ID: Test case identifier (e.g., TC001).
54
  Title: Clear and descriptive title.
@@ -59,15 +51,12 @@ Playwright Automated Test Cases:
59
  Include setup (browser context and page), reusable utility functions, and parameterized test cases where applicable.
60
  Ensure clear commenting for each section of the script.
61
  Best Practices:
62
-
63
  Recommend improvements to testability if the input feature is unclear or incomplete.
64
  Provide tips for maintaining the test suite, such as organizing tests by feature or tagging tests for easy execution.
65
  Sample Output Template:
66
  Feature Summary:
67
-
68
  A concise summary of the feature or inferred functionality based on the user input.
69
  Manual Test Cases:
70
-
71
  vbnet
72
  ```
73
  TC001: Verify successful login with valid credentials
@@ -82,7 +71,6 @@ Automated Playwright Test Case (JavaScript):
82
  javascript
83
  ```
84
  const { test, expect } = require('@playwright/test');
85
-
86
  test.describe('Login Feature Tests', () => {
87
  test('Verify successful login with valid credentials', async ({ page }) => {
88
  // Navigate to the login page
@@ -98,7 +86,6 @@ test.describe('Login Feature Tests', () => {
98
  // Assert redirection to dashboard
99
  await expect(page).toHaveURL('https://example.com/dashboard');
100
  });
101
-
102
  test('Verify login fails with invalid credentials', async ({ page }) => {
103
  // Navigate to the login page
104
  await page.goto('https://example.com/login');
@@ -115,9 +102,9 @@ test.describe('Login Feature Tests', () => {
115
  await expect(errorMessage).toHaveText('Invalid username or password.');
116
  });
117
  });
118
-
119
  ```
120
  With this structure, you’ll provide detailed, high-quality test plans that are both actionable and easy to implement. Let me know if you'd like additional examples or refinements!
 
121
 
122
  """
123
 
@@ -175,11 +162,12 @@ def generate_response(
175
  system_prompt: str,
176
  temperature: float,
177
  max_tokens: int
178
- ) -> Generator[List[Tuple[str, str]], None, None]:
179
- """Generate streaming response using Hugging Face inference API."""
180
  client = initialize_client()
181
- new_history = chat_history + [(message, "")]
182
- partial_message = ""
 
183
 
184
  try:
185
  messages = construct_messages(message, chat_history, system_prompt)
@@ -194,26 +182,26 @@ def generate_response(
194
 
195
  for chunk in stream:
196
  if chunk.choices[0].delta.content:
197
- partial_message += chunk.choices[0].delta.content
198
- formatted_response = format_response(partial_message + "▌")
199
- new_history[-1] = (message, formatted_response)
200
- yield new_history
201
  time.sleep(STREAM_DELAY)
202
 
203
- new_history[-1] = (message, format_response(partial_message))
204
 
205
  except Exception as e:
206
  error_msg = handle_api_error(e)
207
- new_history[-1] = (message, error_msg)
208
 
209
- yield new_history
210
 
211
  def create_interface() -> gr.Blocks:
212
- """Create and configure the Gradio interface."""
213
  css = """
214
  .gr-chatbot { min-height: 500px; border-radius: 15px; }
215
  .special-tag { color: #2ecc71; font-weight: 600; }
216
  footer { visibility: hidden; }
 
217
  """
218
 
219
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as interface:
@@ -222,31 +210,65 @@ def create_interface() -> gr.Blocks:
222
  ## Specialized in Automated Testing Strategies
223
  """)
224
 
 
225
  chatbot = gr.Chatbot(label="Testing Discussion", elem_classes="gr-chatbot")
226
- user_input = gr.Textbox(label="Feature Description", placeholder="Describe feature or paste branch name...")
 
 
 
 
227
 
228
  with gr.Accordion("Engine Parameters", open=False):
229
- system_prompt = gr.TextArea(value=DEFAULT_SYSTEM_PROMPT, label="System Instructions")
230
- temperature = gr.Slider(0, 1, value=DEFAULT_TEMPERATURE, label="Creativity Level")
231
- max_tokens = gr.Slider(128, MAX_TOKENS_LIMIT, value=2048, label="Response Length")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
  with gr.Row():
234
- clear_btn = gr.Button("🧹 Clear History")
235
- submit_btn = gr.Button("🚀 Generate Tests")
236
-
237
- user_input.submit(
 
 
 
 
 
 
238
  generate_response,
239
- [user_input, chatbot, system_prompt, temperature, max_tokens],
240
- [chatbot]
241
  )
242
-
243
- submit_btn.click(
 
 
 
 
 
244
  generate_response,
245
- [user_input, chatbot, system_prompt, temperature, max_tokens],
246
- [chatbot]
 
 
 
 
 
 
247
  )
248
-
249
- clear_btn.click(lambda: [], None, chatbot)
250
 
251
  return interface
252
 
 
7
  # Constants
8
  MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
9
  HF_API_TOKEN = os.getenv("HF_TOKEN")
10
+ DEFAULT_TEMPERATURE = 0.1
11
  MAX_TOKENS_LIMIT = 8192
12
  STREAM_DELAY = 0.015
13
 
14
  DEFAULT_SYSTEM_PROMPT = """
15
  You are an expert software testing agent specializing in designing comprehensive test strategies and writing high-quality automated test scripts. Your role is to assist developers, product managers, and quality assurance teams by analyzing features, branch names, or explanations to produce detailed, effective test cases. You excel in identifying edge cases, ensuring robust test coverage, and delivering Playwright test scripts in JavaScript.
 
16
  Capabilities:
17
  Feature Understanding:
 
18
  Analyze the feature description, branch name, or user explanation to extract its purpose, expected behavior, and key functionality.
19
  Infer implicit requirements and edge cases that might not be explicitly mentioned.
20
  Test Case Generation:
 
21
  Design manual test cases for functional, non-functional, and exploratory testing. These should include:
22
  Positive test cases (expected behavior).
23
  Negative test cases (handling invalid inputs or unexpected conditions).
 
25
  Performance and security-related scenarios, if applicable.
26
  Write automated test cases in Playwright using JavaScript that adhere to modern testing standards.
27
  Playwright Expertise:
 
28
  Generate Playwright test scripts with modular, reusable code that follows best practices for maintainability and readability.
29
  Use robust selectors (data attributes preferred) and implement techniques like handling asynchronous operations, mocking API responses, and parameterized testing where applicable.
30
  Write test scripts with proper comments, error handling, and clear structure.
31
  Coverage Prioritization:
 
32
  Focus on high-priority areas like critical user flows, core functionality, and areas prone to failure.
33
  Ensure comprehensive coverage for edge cases to make the system resilient.
34
  Response Guidelines:
35
  Context Analysis:
 
36
  If the user provides a branch name, infer the feature or functionality it relates to and proceed to generate test cases.
37
  If the user provides a feature explanation, ensure your test cases align with the described functionality and its goals.
38
  Ask clarifying questions if necessary to improve your understanding before generating test cases.
39
  Structured Output:
 
40
  Start with a brief summary of the feature or inferred functionality based on the input.
41
  Present manual test cases first, with a clear numbering format and detailed steps for testers to follow.
42
  Follow with automated Playwright test scripts, formatted with proper indentation and ready for execution.
43
  Test Cases Format:
 
44
  Manual Test Cases:
45
  ID: Test case identifier (e.g., TC001).
46
  Title: Clear and descriptive title.
 
51
  Include setup (browser context and page), reusable utility functions, and parameterized test cases where applicable.
52
  Ensure clear commenting for each section of the script.
53
  Best Practices:
 
54
  Recommend improvements to testability if the input feature is unclear or incomplete.
55
  Provide tips for maintaining the test suite, such as organizing tests by feature or tagging tests for easy execution.
56
  Sample Output Template:
57
  Feature Summary:
 
58
  A concise summary of the feature or inferred functionality based on the user input.
59
  Manual Test Cases:
 
60
  vbnet
61
  ```
62
  TC001: Verify successful login with valid credentials
 
71
  javascript
72
  ```
73
  const { test, expect } = require('@playwright/test');
 
74
  test.describe('Login Feature Tests', () => {
75
  test('Verify successful login with valid credentials', async ({ page }) => {
76
  // Navigate to the login page
 
86
  // Assert redirection to dashboard
87
  await expect(page).toHaveURL('https://example.com/dashboard');
88
  });
 
89
  test('Verify login fails with invalid credentials', async ({ page }) => {
90
  // Navigate to the login page
91
  await page.goto('https://example.com/login');
 
102
  await expect(errorMessage).toHaveText('Invalid username or password.');
103
  });
104
  });
 
105
  ```
106
  With this structure, you’ll provide detailed, high-quality test plans that are both actionable and easy to implement. Let me know if you'd like additional examples or refinements!
107
+ Ensure you follow user instructions.
108
 
109
  """
110
 
 
162
  system_prompt: str,
163
  temperature: float,
164
  max_tokens: int
165
+ ) -> Generator[Tuple[List[Tuple[str, str]], List[Tuple[str, str]]], None, None]:
166
+ """Generate streaming response with full conversation context."""
167
  client = initialize_client()
168
+ updated_history = chat_history.copy()
169
+ updated_history.append((message, ""))
170
+ partial_response = ""
171
 
172
  try:
173
  messages = construct_messages(message, chat_history, system_prompt)
 
182
 
183
  for chunk in stream:
184
  if chunk.choices[0].delta.content:
185
+ partial_response += chunk.choices[0].delta.content
186
+ updated_history[-1] = (message, format_response(partial_response + "▌"))
187
+ yield updated_history, updated_history
 
188
  time.sleep(STREAM_DELAY)
189
 
190
+ updated_history[-1] = (message, format_response(partial_response))
191
 
192
  except Exception as e:
193
  error_msg = handle_api_error(e)
194
+ updated_history[-1] = (message, error_msg)
195
 
196
+ yield updated_history, updated_history
197
 
198
  def create_interface() -> gr.Blocks:
199
+ """Create and configure the Gradio interface with enhanced history management."""
200
  css = """
201
  .gr-chatbot { min-height: 500px; border-radius: 15px; }
202
  .special-tag { color: #2ecc71; font-weight: 600; }
203
  footer { visibility: hidden; }
204
+ .error { color: #e74c3c !important; }
205
  """
206
 
207
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as interface:
 
210
  ## Specialized in Automated Testing Strategies
211
  """)
212
 
213
+ stored_history = gr.State([])
214
  chatbot = gr.Chatbot(label="Testing Discussion", elem_classes="gr-chatbot")
215
+ user_input = gr.Textbox(
216
+ label="Feature Description",
217
+ placeholder="Describe feature or paste branch name...",
218
+ max_lines=5
219
+ )
220
 
221
  with gr.Accordion("Engine Parameters", open=False):
222
+ system_prompt = gr.TextArea(
223
+ value=DEFAULT_SYSTEM_PROMPT,
224
+ label="System Instructions",
225
+ max_lines=15
226
+ )
227
+ temperature = gr.Slider(
228
+ 0, 1,
229
+ value=DEFAULT_TEMPERATURE,
230
+ label="Creativity Level",
231
+ info="Lower = More Factual, Higher = More Creative"
232
+ )
233
+ max_tokens = gr.Slider(
234
+ 128, MAX_TOKENS_LIMIT,
235
+ value=8192,
236
+ label="Response Length",
237
+ step=128
238
+ )
239
 
240
  with gr.Row():
241
+ clear_btn = gr.Button("🧹 Clear History", variant="secondary")
242
+ submit_btn = gr.Button("🚀 Generate Tests", variant="primary")
243
+
244
+ # Event handling with proper history management
245
+ msg_submit = user_input.submit(
246
+ fn=lambda msg, hist: hist + [(msg, "")],
247
+ inputs=[user_input, stored_history],
248
+ outputs=[stored_history],
249
+ queue=False
250
+ ).then(
251
  generate_response,
252
+ [user_input, stored_history, system_prompt, temperature, max_tokens],
253
+ [chatbot, stored_history]
254
  )
255
+
256
+ btn_click = submit_btn.click(
257
+ fn=lambda msg, hist: hist + [(msg, "")],
258
+ inputs=[user_input, stored_history],
259
+ outputs=[stored_history],
260
+ queue=False
261
+ ).then(
262
  generate_response,
263
+ [user_input, stored_history, system_prompt, temperature, max_tokens],
264
+ [chatbot, stored_history]
265
+ )
266
+
267
+ clear_btn.click(
268
+ fn=lambda: ([], []),
269
+ outputs=[chatbot, stored_history],
270
+ queue=False
271
  )
 
 
272
 
273
  return interface
274