thechaiexperiment commited on
Commit
b6c209a
·
verified ·
1 Parent(s): 5c5b992

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ import docx
5
+ from docx.shared import Pt
6
+
7
+ # OpenRouter API Key
8
+ OPENROUTER_API_KEY = "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"
9
+ OPENROUTER_MODEL = "sophosympatheia/rogue-rose-103b-v0.2:free"
10
+
11
+ # Initialize OpenAI client with OpenRouter base URL
12
+ print(f"Using API Key: {OPENROUTER_API_KEY}")
13
+ openai_client = openai.OpenAI(
14
+ api_key=OPENROUTER_API_KEY,
15
+ base_url="https://openrouter.ai/api/v1" # OpenRouter API endpoint
16
+ )
17
+
18
+ # Few-shot examples for test case generation
19
+ FEW_SHOT_EXAMPLES = """
20
+ Example 1:
21
+ Project: E-commerce Website
22
+ Requirement: Users should be able to add items to their cart.
23
+ Test Case:
24
+ 1. Navigate to the product page.
25
+ 2. Click the "Add to Cart" button.
26
+ 3. Verify that the item appears in the cart.
27
+
28
+ Example 2:
29
+ Project: Banking App
30
+ Requirement: Users should be able to transfer funds between accounts.
31
+ Test Case:
32
+ 1. Log in to the banking app.
33
+ 2. Navigate to the "Transfer Funds" section.
34
+ 3. Select the source and destination accounts.
35
+ 4. Enter the amount and confirm the transfer.
36
+ 5. Verify that the transaction is reflected in both accounts.
37
+ """
38
+
39
+ # Function to generate test cases using OpenRouter API
40
+ def generate_test_cases(project_details):
41
+ print("Generating test cases...")
42
+ try:
43
+ response = openai_client.chat.completions.create(
44
+ model=OPENROUTER_MODEL,
45
+ messages=[
46
+ {
47
+ "role": "system",
48
+ "content": "You are a software testing expert. Your task is to generate test cases based on the provided project details. "
49
+ "Use the following examples as a guide:\n" + FEW_SHOT_EXAMPLES
50
+ },
51
+ {
52
+ "role": "user",
53
+ "content": f"Project Details:\n{project_details}\n\nGenerate test cases for the above project."
54
+ }
55
+ ]
56
+ )
57
+ print("Test cases generated.")
58
+ return response.choices[0].message.content
59
+ except Exception as e:
60
+ print(f"Error generating test cases: {e}")
61
+ return f"Error: {e}"
62
+
63
+ # Function to create a Word document
64
+ def create_word_document(content, filename="test_cases.docx"):
65
+ print("Creating Word document...")
66
+ doc = docx.Document()
67
+ doc.add_heading("Generated Test Cases", level=1)
68
+
69
+ # Add content to the document
70
+ for line in content.split("\n"):
71
+ if line.strip():
72
+ doc.add_paragraph(line)
73
+
74
+ # Save the document
75
+ doc.save(filename)
76
+ print(f"Word document saved as {filename}.")
77
+ return filename
78
+
79
+ # Gradio UI for software testing automation
80
+ def gradio_ui():
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("## Software Testing Automation Tool")
83
+ gr.Markdown("Upload project details or enter them as text to generate test cases and documentation.")
84
+
85
+ # Inputs
86
+ project_input = gr.Textbox(label="Enter Project Details", lines=5, placeholder="Describe the project requirements...")
87
+ file_upload = gr.File(label="Or Upload Project Document (TXT, PDF, DOCX)", file_types=[".txt", ".pdf", ".docx"])
88
+
89
+ # Outputs
90
+ output_text = gr.Textbox(label="Generated Test Cases", lines=10, interactive=False)
91
+ download_link = gr.File(label="Download Test Cases as Word Document")
92
+
93
+ # Buttons
94
+ generate_btn = gr.Button("Generate Test Cases")
95
+
96
+ # Event handling
97
+ generate_btn.click(
98
+ fn=lambda project_details: generate_test_cases(project_details),
99
+ inputs=[project_input],
100
+ outputs=[output_text]
101
+ )
102
+ generate_btn.click(
103
+ fn=lambda project_details: create_word_document(generate_test_cases(project_details)),
104
+ inputs=[project_input],
105
+ outputs=[download_link]
106
+ )
107
+
108
+ return demo
109
+
110
+ # Launch Gradio UI
111
+ demo = gradio_ui()
112
+ print("Launching Gradio UI...")
113
+ demo.launch()