Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Import the TestCaseGenerator
|
| 6 |
+
from test_case_generator import TestCaseGenerator
|
| 7 |
+
|
| 8 |
+
def main():
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Question Generation App",
|
| 11 |
+
page_icon="📝",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Custom CSS for styling
|
| 16 |
+
st.markdown("""
|
| 17 |
+
<style>
|
| 18 |
+
.main-title {
|
| 19 |
+
font-size: 3em;
|
| 20 |
+
color: #2C3E50;
|
| 21 |
+
text-align: center;
|
| 22 |
+
margin-bottom: 30px;
|
| 23 |
+
}
|
| 24 |
+
.stButton>button {
|
| 25 |
+
background-color: #3498DB;
|
| 26 |
+
color: white;
|
| 27 |
+
border: none;
|
| 28 |
+
padding: 10px 20px;
|
| 29 |
+
border-radius: 5px;
|
| 30 |
+
transition: all 0.3s;
|
| 31 |
+
}
|
| 32 |
+
.stButton>button:hover {
|
| 33 |
+
background-color: #2980B9;
|
| 34 |
+
transform: scale(1.05);
|
| 35 |
+
}
|
| 36 |
+
</style>
|
| 37 |
+
""", unsafe_allow_html=True)
|
| 38 |
+
|
| 39 |
+
# Title
|
| 40 |
+
st.markdown("<h1 class='main-title'>📝 AI Question Generator</h1>", unsafe_allow_html=True)
|
| 41 |
+
|
| 42 |
+
# Sidebar for inputs
|
| 43 |
+
st.sidebar.header("Configuration")
|
| 44 |
+
|
| 45 |
+
# File uploader
|
| 46 |
+
uploaded_file = st.sidebar.file_uploader(
|
| 47 |
+
"Upload PDF Document",
|
| 48 |
+
type=['pdf'],
|
| 49 |
+
help="Please upload a PDF file to generate questions from"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Question type selection
|
| 53 |
+
generator = TestCaseGenerator()
|
| 54 |
+
question_types = st.sidebar.multiselect(
|
| 55 |
+
"Select Question Types",
|
| 56 |
+
generator.available_question_types,
|
| 57 |
+
default=['cause_and_effect_reasoning', 'temporal_reasoning']
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Number of questions
|
| 61 |
+
num_questions = st.sidebar.slider(
|
| 62 |
+
"Number of Questions per Type",
|
| 63 |
+
min_value=1,
|
| 64 |
+
max_value=20,
|
| 65 |
+
value=5
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Generate button
|
| 69 |
+
generate_button = st.sidebar.button("Generate Questions", use_container_width=True)
|
| 70 |
+
|
| 71 |
+
# Main content area
|
| 72 |
+
main_content = st.container()
|
| 73 |
+
|
| 74 |
+
# Generation logic
|
| 75 |
+
if generate_button and uploaded_file and question_types:
|
| 76 |
+
with st.spinner('Generating questions...'):
|
| 77 |
+
# Create results DataFrame
|
| 78 |
+
final_df = pd.DataFrame()
|
| 79 |
+
|
| 80 |
+
# Generate questions for each selected type
|
| 81 |
+
for q_type in question_types:
|
| 82 |
+
try:
|
| 83 |
+
type_df = generator.generate_testcases(
|
| 84 |
+
uploaded_file,
|
| 85 |
+
question_type=q_type,
|
| 86 |
+
num_testcases=num_questions
|
| 87 |
+
)
|
| 88 |
+
type_df['question_type'] = q_type
|
| 89 |
+
final_df = pd.concat([final_df, type_df], ignore_index=True)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
st.error(f"Error generating {q_type} questions: {e}")
|
| 92 |
+
|
| 93 |
+
# Display results
|
| 94 |
+
if not final_df.empty:
|
| 95 |
+
st.success(f"Generated {len(final_df)} questions!")
|
| 96 |
+
|
| 97 |
+
# Display questions in an interactive table
|
| 98 |
+
st.dataframe(
|
| 99 |
+
final_df[['question_type', 'question', 'answer']],
|
| 100 |
+
use_container_width=True
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Download button for Excel
|
| 104 |
+
csv = final_df.to_csv(index=False)
|
| 105 |
+
st.download_button(
|
| 106 |
+
label="Download Questions as CSV",
|
| 107 |
+
data=csv,
|
| 108 |
+
file_name="generated_questions.csv",
|
| 109 |
+
mime="text/csv"
|
| 110 |
+
)
|
| 111 |
+
else:
|
| 112 |
+
st.warning("No questions could be generated. Please check your inputs.")
|
| 113 |
+
|
| 114 |
+
elif not uploaded_file:
|
| 115 |
+
st.info("Please upload a PDF document to start generating questions.")
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
main()
|