Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
import docx2txt
|
| 5 |
+
import PyPDF2
|
| 6 |
+
|
| 7 |
+
# ------------------------------
|
| 8 |
+
# Hugging Face Spaces: API Key from Secrets
|
| 9 |
+
# ------------------------------
|
| 10 |
+
# Set your API key in the Space's Secrets with the name "GROQ_API_KEY"
|
| 11 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 12 |
+
|
| 13 |
+
# ------------------------------
|
| 14 |
+
# Streamlit page config and theme
|
| 15 |
+
# ------------------------------
|
| 16 |
+
st.set_page_config(page_title="🧪 TestCase Studio", layout="wide")
|
| 17 |
+
st.title("TestCase Studio")
|
| 18 |
+
|
| 19 |
+
# Custom CSS for clean theme
|
| 20 |
+
st.markdown(
|
| 21 |
+
"""
|
| 22 |
+
<style>
|
| 23 |
+
body {background-color: #f0f4f8; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}
|
| 24 |
+
.stButton>button {background-color: #00796b; color: white; font-size:16px; border-radius:8px; height: 40px; width: 100%;}
|
| 25 |
+
.stTextArea>div>div>textarea {font-size: 14px;}
|
| 26 |
+
.stFileUploader>div>div>label {font-weight: bold;}
|
| 27 |
+
.stExpanderHeader {font-weight: bold; font-size: 16px; color: #00796b;}
|
| 28 |
+
</style>
|
| 29 |
+
""",
|
| 30 |
+
unsafe_allow_html=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
st.write("Choose one input method to provide requirements:")
|
| 34 |
+
|
| 35 |
+
# ------------------------------
|
| 36 |
+
# Input method selection
|
| 37 |
+
# ------------------------------
|
| 38 |
+
input_method = st.radio("Select Input Type:", ("Upload File", "Paste Text"))
|
| 39 |
+
|
| 40 |
+
uploaded_file = None
|
| 41 |
+
manual_text = None
|
| 42 |
+
|
| 43 |
+
if input_method == "Upload File":
|
| 44 |
+
uploaded_file = st.file_uploader("Upload requirement file (TXT / PDF / DOCX)", type=["txt", "pdf", "docx"])
|
| 45 |
+
else:
|
| 46 |
+
manual_text = st.text_area("Paste requirement text here", height=200)
|
| 47 |
+
|
| 48 |
+
num_testcases = st.number_input("Approximate number of test cases to generate", min_value=1, max_value=50, value=10)
|
| 49 |
+
|
| 50 |
+
generate_btn = st.button("Generate Test Cases")
|
| 51 |
+
|
| 52 |
+
# ------------------------------
|
| 53 |
+
# Helper function to read file
|
| 54 |
+
# ------------------------------
|
| 55 |
+
def read_file(file):
|
| 56 |
+
if file.name.endswith(".txt"):
|
| 57 |
+
return file.read().decode("utf-8")
|
| 58 |
+
elif file.name.endswith(".pdf"):
|
| 59 |
+
reader = PyPDF2.PdfReader(file)
|
| 60 |
+
text = ""
|
| 61 |
+
for page in reader.pages:
|
| 62 |
+
text += page.extract_text() + "\n"
|
| 63 |
+
return text
|
| 64 |
+
elif file.name.endswith(".docx"):
|
| 65 |
+
return docx2txt.process(file)
|
| 66 |
+
else:
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
# ------------------------------
|
| 70 |
+
# Call Groq API to generate test cases
|
| 71 |
+
# ------------------------------
|
| 72 |
+
def call_groq_generate_testcases(text, num_testcases=10):
|
| 73 |
+
prompt = f"""
|
| 74 |
+
You are a QA expert. For the following requirement, generate approximately {num_testcases} detailed test cases in the following clean format:
|
| 75 |
+
|
| 76 |
+
====================================================
|
| 77 |
+
ID: TC_001
|
| 78 |
+
Title: <Test Case Title>
|
| 79 |
+
Preconditions: <Preconditions>
|
| 80 |
+
Steps:
|
| 81 |
+
1. <Step 1>
|
| 82 |
+
2. <Step 2>
|
| 83 |
+
...
|
| 84 |
+
Expected Result: <Expected Result>
|
| 85 |
+
====================================================
|
| 86 |
+
|
| 87 |
+
ONLY return the test cases in this clean format with lines separating each test case. Do not include anything else.
|
| 88 |
+
Requirement:
|
| 89 |
+
\"\"\"{text}\"\"\"
|
| 90 |
+
"""
|
| 91 |
+
response = client.chat.completions.create(
|
| 92 |
+
model="llama-3.1-8b-instant",
|
| 93 |
+
messages=[{"role":"user", "content": prompt}],
|
| 94 |
+
max_tokens=1500
|
| 95 |
+
)
|
| 96 |
+
return response.choices[0].message.content
|
| 97 |
+
|
| 98 |
+
# ------------------------------
|
| 99 |
+
# Generate test cases
|
| 100 |
+
# ------------------------------
|
| 101 |
+
if generate_btn:
|
| 102 |
+
if input_method == "Upload File":
|
| 103 |
+
if uploaded_file is not None:
|
| 104 |
+
try:
|
| 105 |
+
requirement_text = read_file(uploaded_file)
|
| 106 |
+
except Exception:
|
| 107 |
+
st.error("Could not read uploaded file. Make sure it is a TXT, PDF, or DOCX.")
|
| 108 |
+
st.stop()
|
| 109 |
+
else:
|
| 110 |
+
st.error("Please upload a file to continue.")
|
| 111 |
+
st.stop()
|
| 112 |
+
else: # Paste Text
|
| 113 |
+
if manual_text and manual_text.strip() != "":
|
| 114 |
+
requirement_text = manual_text.strip()
|
| 115 |
+
else:
|
| 116 |
+
st.error("Please paste requirement text to continue.")
|
| 117 |
+
st.stop()
|
| 118 |
+
|
| 119 |
+
st.info("Generating Test Cases — please wait ...")
|
| 120 |
+
try:
|
| 121 |
+
testcases_output = call_groq_generate_testcases(requirement_text, num_testcases)
|
| 122 |
+
except Exception as e:
|
| 123 |
+
st.error(f"Error generating test cases: {e}")
|
| 124 |
+
st.stop()
|
| 125 |
+
|
| 126 |
+
st.subheader("🔸 Generated Test Cases")
|
| 127 |
+
with st.expander("View Test Cases", expanded=True):
|
| 128 |
+
st.text(testcases_output)
|
| 129 |
+
|
| 130 |
+
st.download_button(
|
| 131 |
+
"Download Test Cases as TXT",
|
| 132 |
+
testcases_output,
|
| 133 |
+
file_name="Generated_Test_Cases.txt",
|
| 134 |
+
mime="text/plain"
|
| 135 |
+
)
|