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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
import time # For simulating delays
|
| 5 |
+
|
| 6 |
+
# --- Configuration and Initial Setup ---
|
| 7 |
+
st.set_page_config(layout="wide", page_title="Dummy URS Doc Processor UI Test")
|
| 8 |
+
st.title("Dummy URS Doc Processor - UI Test Version")
|
| 9 |
+
|
| 10 |
+
# Initialize session states
|
| 11 |
+
if "show_step2" not in st.session_state:
|
| 12 |
+
st.session_state.show_step2 = False
|
| 13 |
+
if "show_step3" not in st.session_state:
|
| 14 |
+
st.session_state.show_step3 = False
|
| 15 |
+
if "protocol_generated" not in st.session_state:
|
| 16 |
+
st.session_state.protocol_generated = False
|
| 17 |
+
if "extracted_data" not in st.session_state:
|
| 18 |
+
st.session_state.extracted_data = None
|
| 19 |
+
if "dataframe" not in st.session_state:
|
| 20 |
+
st.session_state.dataframe = None
|
| 21 |
+
if "processing" not in st.session_state:
|
| 22 |
+
st.session_state.processing = False
|
| 23 |
+
if "generated_pdf" not in st.session_state:
|
| 24 |
+
st.session_state.generated_pdf = None
|
| 25 |
+
if "generated_pdf_name" not in st.session_state:
|
| 26 |
+
st.session_state.generated_pdf_name = None
|
| 27 |
+
|
| 28 |
+
# Dummy LLM setup (no actual LLM calls)
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def get_llms():
|
| 31 |
+
return None, None # No LLMs needed for UI test
|
| 32 |
+
|
| 33 |
+
# --------------------- Dummy Backend Functions ---------------------
|
| 34 |
+
|
| 35 |
+
def dummy_run_rag_pipeline(pdf_file, template_file):
|
| 36 |
+
"""
|
| 37 |
+
Simulates the RAG pipeline for UI testing.
|
| 38 |
+
Returns dummy extracted data.
|
| 39 |
+
"""
|
| 40 |
+
st.toast("Simulating RAG Pipeline...")
|
| 41 |
+
time.sleep(2) # Simulate processing time
|
| 42 |
+
|
| 43 |
+
# Dummy extracted data
|
| 44 |
+
dummy_data = {
|
| 45 |
+
"ProjectName": "Dummy Project UI",
|
| 46 |
+
"DocumentNumber": "DUMMY-001",
|
| 47 |
+
"SystemName": "Test System Beta",
|
| 48 |
+
"Version": "1.0.0",
|
| 49 |
+
"Author": "UI Tester",
|
| 50 |
+
"Date": "2025-07-21",
|
| 51 |
+
"FunctionalRequirement_1": "The system shall display data responsively on mobile devices.",
|
| 52 |
+
"FunctionalRequirement_2": "The system shall allow editing of extracted values.",
|
| 53 |
+
"Utility#1": "Power Supply (240V)",
|
| 54 |
+
"Utility#2": "Ethernet Connection",
|
| 55 |
+
"SoftwareRequirements": "Python 3.9+, Streamlit 1.x",
|
| 56 |
+
"HardwareRequirements": "Any modern smartphone or tablet"
|
| 57 |
+
}
|
| 58 |
+
st.toast("RAG Pipeline simulation complete!")
|
| 59 |
+
return dummy_data
|
| 60 |
+
|
| 61 |
+
def dummy_json_to_dataframe(json_data):
|
| 62 |
+
"""
|
| 63 |
+
Converts dummy JSON data to DataFrame for editing.
|
| 64 |
+
"""
|
| 65 |
+
if not json_data:
|
| 66 |
+
return pd.DataFrame(columns=['Placeholder', 'Value'])
|
| 67 |
+
|
| 68 |
+
data_list = []
|
| 69 |
+
for key, value in json_data.items():
|
| 70 |
+
data_list.append((str(key), str(value) if value is not None else ""))
|
| 71 |
+
|
| 72 |
+
df = pd.DataFrame(data_list, columns=['Placeholder', 'Value'])
|
| 73 |
+
return df
|
| 74 |
+
|
| 75 |
+
def create_dummy_pdf():
|
| 76 |
+
"""Generates a dummy PDF byte stream for download."""
|
| 77 |
+
from reportlab.lib.pagesizes import letter
|
| 78 |
+
from reportlab.pdfgen import canvas
|
| 79 |
+
from io import BytesIO
|
| 80 |
+
|
| 81 |
+
buffer = BytesIO()
|
| 82 |
+
p = canvas.Canvas(buffer, pagesize=letter)
|
| 83 |
+
p.drawString(100, 750, "This is a Dummy PDF for UI Testing!")
|
| 84 |
+
p.drawString(100, 730, "It contains no real data from your URS or protocol.")
|
| 85 |
+
p.drawString(100, 710, "Generated on: " + time.ctime())
|
| 86 |
+
p.showPage()
|
| 87 |
+
p.save()
|
| 88 |
+
buffer.seek(0)
|
| 89 |
+
return buffer.getvalue()
|
| 90 |
+
|
| 91 |
+
# --- Status & Progress Tracking ---
|
| 92 |
+
# This section remains largely the same to simulate your original progress logic
|
| 93 |
+
progress = 0
|
| 94 |
+
status_text = "🟦 Waiting for input files"
|
| 95 |
+
|
| 96 |
+
if st.session_state.processing:
|
| 97 |
+
progress = 20
|
| 98 |
+
status_text = "Extracting values from uploaded files (simulated)..."
|
| 99 |
+
|
| 100 |
+
if st.session_state.show_step2:
|
| 101 |
+
progress = 40
|
| 102 |
+
status_text = "Input form is being generated (simulated)..."
|
| 103 |
+
|
| 104 |
+
if st.session_state.show_step3:
|
| 105 |
+
progress = 60
|
| 106 |
+
status_text = "Saving changes to input form (simulated)..."
|
| 107 |
+
|
| 108 |
+
if st.session_state.protocol_generated:
|
| 109 |
+
progress = 100
|
| 110 |
+
status_text = "Final protocol document is being generated (simulated)..."
|
| 111 |
+
|
| 112 |
+
st.markdown(f"### Status: {status_text}")
|
| 113 |
+
st.progress(progress / 100.0)
|
| 114 |
+
|
| 115 |
+
# --- Layout: Horizontal columns for each step ---
|
| 116 |
+
st.write("---") # Horizontal line for separation
|
| 117 |
+
col1, col2, col3 = st.columns(3)
|
| 118 |
+
|
| 119 |
+
# --- STEP 1 ---
|
| 120 |
+
with col1:
|
| 121 |
+
st.markdown("### 🔹 Step 1")
|
| 122 |
+
st.write("📄 **Upload input URS (PDF)**")
|
| 123 |
+
urs_pdf = st.file_uploader("Upload PDF", type=["pdf"], key="pdf_upload_step1")
|
| 124 |
+
|
| 125 |
+
st.write("📊 **Upload template (XLSX)**")
|
| 126 |
+
template_xlsx = st.file_uploader("Upload XLSX", type=["xlsx"], key="xlsx_upload_step1")
|
| 127 |
+
|
| 128 |
+
if st.button("➡️ Generate Input Form", key="generate_form_button"):
|
| 129 |
+
if urs_pdf and template_xlsx:
|
| 130 |
+
st.session_state.processing = True
|
| 131 |
+
st.rerun()
|
| 132 |
+
else:
|
| 133 |
+
st.warning("Please upload both PDF and XLSX files for the simulation.")
|
| 134 |
+
|
| 135 |
+
# Process RAG pipeline if processing is True
|
| 136 |
+
if st.session_state.processing:
|
| 137 |
+
with st.spinner("🔄 Running RAG Pipeline (Simulated)..."):
|
| 138 |
+
# Call the dummy RAG pipeline
|
| 139 |
+
result = dummy_run_rag_pipeline(urs_pdf, template_xlsx)
|
| 140 |
+
|
| 141 |
+
if result:
|
| 142 |
+
st.session_state.extracted_data = result
|
| 143 |
+
st.session_state.dataframe = dummy_json_to_dataframe(result)
|
| 144 |
+
st.session_state.show_step2 = True
|
| 145 |
+
st.session_state.processing = False
|
| 146 |
+
st.success("✅ RAG Pipeline simulation completed!")
|
| 147 |
+
st.rerun()
|
| 148 |
+
else:
|
| 149 |
+
st.error("❌ Failed to simulate document processing. Try again.")
|
| 150 |
+
st.session_state.processing = False
|
| 151 |
+
st.rerun()
|
| 152 |
+
|
| 153 |
+
st.write("---") # Horizontal line for separation
|
| 154 |
+
|
| 155 |
+
# --- STEP 2 ---
|
| 156 |
+
with col2:
|
| 157 |
+
if st.session_state.show_step2:
|
| 158 |
+
st.markdown("### 🔹 Step 2")
|
| 159 |
+
|
| 160 |
+
if st.session_state.dataframe is not None:
|
| 161 |
+
st.markdown("✏️ **Edit Input Form (DataFrame)**")
|
| 162 |
+
|
| 163 |
+
# Display editable dataframe
|
| 164 |
+
edited_df = st.data_editor(
|
| 165 |
+
st.session_state.dataframe,
|
| 166 |
+
num_rows="dynamic",
|
| 167 |
+
key="data_editor",
|
| 168 |
+
height=400 # Fixed height for better mobile consistency
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# Update session state with edited data (for dummy purposes)
|
| 172 |
+
st.session_state.dataframe = edited_df
|
| 173 |
+
|
| 174 |
+
if st.button("💾 Save Changes", key="save_changes_button"):
|
| 175 |
+
# Simulate saving changes
|
| 176 |
+
st.session_state.extracted_data = {
|
| 177 |
+
row['Placeholder']: row['Value']
|
| 178 |
+
for _, row in edited_df.iterrows()
|
| 179 |
+
if pd.notna(row['Value']) and row['Value'] != ''
|
| 180 |
+
}
|
| 181 |
+
st.success("✅ Changes saved (simulated)!")
|
| 182 |
+
st.session_state.show_step3 = True
|
| 183 |
+
st.rerun()
|
| 184 |
+
else:
|
| 185 |
+
st.markdown("### 🔹 Step 2")
|
| 186 |
+
st.info("Complete Step 1 to unlock.")
|
| 187 |
+
|
| 188 |
+
st.write("---") # Horizontal line for separation
|
| 189 |
+
|
| 190 |
+
# --- STEP 3 ---
|
| 191 |
+
with col3:
|
| 192 |
+
if st.session_state.show_step3:
|
| 193 |
+
st.markdown("### 🔹 Step 3")
|
| 194 |
+
st.write("📤 **Upload Protocol Template (DOCX)**")
|
| 195 |
+
output_docx = st.file_uploader("Upload DOCX", type=["docx"], key="docx_upload_step3")
|
| 196 |
+
|
| 197 |
+
# Dummy progress bar for protocol generation steps
|
| 198 |
+
protocol_status_text = st.empty()
|
| 199 |
+
protocol_progress_bar_inner = st.progress(0.0)
|
| 200 |
+
|
| 201 |
+
if st.button("📝 Generate Protocol Document", key="generate_protocol_button"):
|
| 202 |
+
if output_docx is None:
|
| 203 |
+
st.warning("Please upload a DOCX template file for the simulation.")
|
| 204 |
+
else:
|
| 205 |
+
protocol_status_text.text("💾 Saving completed DOCX (simulated)...")
|
| 206 |
+
protocol_progress_bar_inner.progress(0.5)
|
| 207 |
+
time.sleep(1) # Simulate delay
|
| 208 |
+
|
| 209 |
+
protocol_status_text.text("⏳ Converting DOCX to PDF (simulated)...")
|
| 210 |
+
protocol_progress_bar_inner.progress(0.7)
|
| 211 |
+
time.sleep(1) # Simulate delay
|
| 212 |
+
|
| 213 |
+
# Generate a dummy PDF
|
| 214 |
+
st.session_state.generated_pdf = create_dummy_pdf()
|
| 215 |
+
st.session_state.generated_pdf_name = "dummy_final_protocol.pdf"
|
| 216 |
+
st.session_state.protocol_generated = True
|
| 217 |
+
|
| 218 |
+
protocol_status_text.text("✅ Final protocol document generated (simulated)!")
|
| 219 |
+
protocol_progress_bar_inner.progress(1.0)
|
| 220 |
+
st.success("✅ Protocol document generated! You can now view or download the dummy PDF.")
|
| 221 |
+
st.rerun()
|
| 222 |
+
|
| 223 |
+
if st.session_state.protocol_generated and hasattr(st.session_state, 'generated_pdf'):
|
| 224 |
+
st.markdown("#### 📥 View / Download")
|
| 225 |
+
col_a, col_b = st.columns(2) # Removed 'Save' column
|
| 226 |
+
with col_a:
|
| 227 |
+
# The 'View PDF' button essentially just triggers a download with preview capabilities
|
| 228 |
+
st.download_button(
|
| 229 |
+
label="👁️ View PDF",
|
| 230 |
+
data=st.session_state.generated_pdf,
|
| 231 |
+
file_name=st.session_state.generated_pdf_name,
|
| 232 |
+
mime="application/pdf",
|
| 233 |
+
key="view_pdf_button"
|
| 234 |
+
)
|
| 235 |
+
with col_b:
|
| 236 |
+
st.download_button(
|
| 237 |
+
label="⬇️ Download PDF",
|
| 238 |
+
data=st.session_state.generated_pdf,
|
| 239 |
+
file_name=st.session_state.generated_pdf_name,
|
| 240 |
+
mime="application/pdf",
|
| 241 |
+
key="download_pdf_button"
|
| 242 |
+
)
|
| 243 |
+
else:
|
| 244 |
+
st.markdown("### 🔹 Step 3")
|
| 245 |
+
st.info("Complete Step 2 to unlock.")
|
| 246 |
+
|
| 247 |
+
st.write("---") # Final horizontal line
|