Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PyPDF2
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
from huggingface_hub import HfApi
|
5 |
+
|
6 |
+
api = HfApi()
|
7 |
+
openai.api_key = api.get_secret("OPENAI_API_KEY")
|
8 |
+
|
9 |
+
def extract_text_from_block(pdf_path, block_pages):
|
10 |
+
text = ""
|
11 |
+
with open(pdf_path, 'rb') as file:
|
12 |
+
reader = PyPDF2.PdfReader(file)
|
13 |
+
for page_num in range(block_pages[0] - 1, block_pages[1]):
|
14 |
+
page = reader.pages[page_num]
|
15 |
+
text += page.extract_text()
|
16 |
+
return text
|
17 |
+
|
18 |
+
def generate_test_cases(text, prompt_template):
|
19 |
+
prompt = prompt_template.format(text=text)
|
20 |
+
response = openai.ChatCompletion.create(
|
21 |
+
model="gpt-4o-mini",
|
22 |
+
messages=[
|
23 |
+
{"role": "system", "content": "You are a helpful assistant that generates test cases based on given text."},
|
24 |
+
{"role": "user", "content": prompt}
|
25 |
+
],
|
26 |
+
max_tokens=1000,
|
27 |
+
n=1,
|
28 |
+
temperature=0.7,
|
29 |
+
)
|
30 |
+
return response.choices[0].message['content'].strip()
|
31 |
+
|
32 |
+
def process_pdf(pdf_file, selected_block, prompt_template):
|
33 |
+
if pdf_file is None:
|
34 |
+
return "Please upload a PDF file."
|
35 |
+
|
36 |
+
block_config = {
|
37 |
+
"5.4.1 OSPI-xSPI-QSPI-SPI Boot": (482, 488),
|
38 |
+
"5.4.2 I2C Boot": (489, 489),
|
39 |
+
"5.4.3 SD Card Boot": (490, 491),
|
40 |
+
"5.4.4 eMMC Boot": (492, 493),
|
41 |
+
"5.4.5 Ethernet Boot": (494, 497),
|
42 |
+
"5.4.6 USB Boot": (498, 498),
|
43 |
+
"5.4.7 UART Boot": (499, 500),
|
44 |
+
"5.4.8 GPMC NOR Boot": (501, 502),
|
45 |
+
"5.4.9 GPMC NAND Boot": (503, 503),
|
46 |
+
"5.4.10 Serial NAND Boot": (504, 505),
|
47 |
+
"5.4.11 No boot/Development boot": (506, 506)
|
48 |
+
}
|
49 |
+
|
50 |
+
try:
|
51 |
+
temp_pdf_path = pdf_file.name
|
52 |
+
selected_pages = block_config[selected_block]
|
53 |
+
|
54 |
+
text = extract_text_from_block(temp_pdf_path, selected_pages)
|
55 |
+
test_cases = generate_test_cases(text, prompt_template)
|
56 |
+
|
57 |
+
return f"Generated Test Cases for {selected_block}:\n\n{test_cases}"
|
58 |
+
except Exception as e:
|
59 |
+
return f"An error occurred: {str(e)}"
|
60 |
+
|
61 |
+
block_names = [
|
62 |
+
"5.4.1 OSPI-xSPI-QSPI-SPI Boot",
|
63 |
+
"5.4.2 I2C Boot",
|
64 |
+
"5.4.3 SD Card Boot",
|
65 |
+
"5.4.4 eMMC Boot",
|
66 |
+
"5.4.5 Ethernet Boot",
|
67 |
+
"5.4.6 USB Boot",
|
68 |
+
"5.4.7 UART Boot",
|
69 |
+
"5.4.8 GPMC NOR Boot",
|
70 |
+
"5.4.9 GPMC NAND Boot",
|
71 |
+
"5.4.10 Serial NAND Boot",
|
72 |
+
"5.4.11 No boot/Development boot"
|
73 |
+
]
|
74 |
+
|
75 |
+
default_prompt = """Generate test cases for the following text:
|
76 |
+
|
77 |
+
{text}
|
78 |
+
|
79 |
+
Please structure each test case as follows:
|
80 |
+
- Test ID
|
81 |
+
- Test Summary
|
82 |
+
- Pre-requisite
|
83 |
+
- Steps
|
84 |
+
- Expected Result
|
85 |
+
|
86 |
+
Test Cases:"""
|
87 |
+
|
88 |
+
app = gr.Interface(
|
89 |
+
fn=process_pdf,
|
90 |
+
inputs=[
|
91 |
+
gr.File(label="Upload PDF"),
|
92 |
+
gr.Dropdown(choices=block_names, label="Select Block"),
|
93 |
+
gr.Textbox(label="Prompt Template", value=default_prompt, lines=10)
|
94 |
+
],
|
95 |
+
outputs="text",
|
96 |
+
title="PDF Block Test Case Generator",
|
97 |
+
description="Upload a PDF, select a block, edit the prompt if needed, and generate test cases for that block."
|
98 |
+
)
|
99 |
+
|
100 |
+
app.launch()
|