Spaces:
Running
Running
Add dynamic URL config to sidebar
Browse files- main_streamlit.py +21 -185
main_streamlit.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# D:\jan-contract\main_streamlit.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
| 5 |
import requests
|
|
@@ -10,10 +8,28 @@ load_dotenv()
|
|
| 10 |
st.set_page_config(layout="wide", page_title="Jan-Contract Unified Assistant", page_icon="⚖️")
|
| 11 |
|
| 12 |
# Backend Configuration
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Only strip trailing slash if it exists
|
| 17 |
if BACKEND_URL.endswith("/"):
|
| 18 |
BACKEND_URL = BACKEND_URL[:-1]
|
| 19 |
|
|
@@ -232,183 +248,3 @@ with tab3:
|
|
| 232 |
st.error("Failed to get answer.")
|
| 233 |
except Exception as e:
|
| 234 |
st.error(f"Error: {e}")
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
# --- 1. Initial Setup ---
|
| 238 |
-
load_dotenv()
|
| 239 |
-
st.set_page_config(layout="wide", page_title="Jan-Contract Unified Assistant", page_icon="⚖️")
|
| 240 |
-
|
| 241 |
-
# Custom CSS for a cleaner look
|
| 242 |
-
st.markdown("""
|
| 243 |
-
<style>
|
| 244 |
-
.reportview-container {
|
| 245 |
-
background: #f0f2f6;
|
| 246 |
-
}
|
| 247 |
-
.main-header {
|
| 248 |
-
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
| 249 |
-
color: #333;
|
| 250 |
-
}
|
| 251 |
-
h1 {
|
| 252 |
-
color: #1A73E8;
|
| 253 |
-
}
|
| 254 |
-
h2, h3 {
|
| 255 |
-
color: #424242;
|
| 256 |
-
}
|
| 257 |
-
.stButton>button {
|
| 258 |
-
color: #ffffff;
|
| 259 |
-
background-color: #1A73E8;
|
| 260 |
-
border-radius: 5px;
|
| 261 |
-
}
|
| 262 |
-
</style>
|
| 263 |
-
""", unsafe_allow_html=True)
|
| 264 |
-
|
| 265 |
-
st.title("Jan-Contract: Digital Workforce Assistant")
|
| 266 |
-
st.write("Empowering India's workforce with accessible legal tools and government scheme discovery.")
|
| 267 |
-
|
| 268 |
-
PDF_UPLOAD_DIR = "pdfs_demystify"
|
| 269 |
-
os.makedirs(PDF_UPLOAD_DIR, exist_ok=True)
|
| 270 |
-
|
| 271 |
-
# --- 2. Streamlit UI with Tabs ---
|
| 272 |
-
tab1, tab2, tab3 = st.tabs([
|
| 273 |
-
"Contract Generator",
|
| 274 |
-
"Scheme Finder",
|
| 275 |
-
"Document Demystifier"
|
| 276 |
-
])
|
| 277 |
-
|
| 278 |
-
# --- TAB 1: Contract Generator ---
|
| 279 |
-
with tab1:
|
| 280 |
-
st.header("Digital Agreement Generator")
|
| 281 |
-
st.write("Create a clear digital agreement from plain text and record video consent.")
|
| 282 |
-
|
| 283 |
-
col1, col2 = st.columns([1, 1])
|
| 284 |
-
|
| 285 |
-
with col1:
|
| 286 |
-
st.subheader("Agreement Details")
|
| 287 |
-
user_request = st.text_area("Describe the terms of the agreement...", height=150, key="contract_request", placeholder="E.g., I, Rajesh, agree to paint Mr. Sharma's house for 5000 rupees by next Tuesday.")
|
| 288 |
-
|
| 289 |
-
if st.button("Generate Agreement", type="primary", key="btn_generate_contract"):
|
| 290 |
-
if user_request:
|
| 291 |
-
with st.spinner("Drafting agreement..."):
|
| 292 |
-
try:
|
| 293 |
-
from agents.legal_agent import legal_agent
|
| 294 |
-
result = legal_agent.invoke({"user_request": user_request})
|
| 295 |
-
st.session_state.legal_result = result
|
| 296 |
-
# Reset video state for new contract
|
| 297 |
-
if 'video_path_from_component' in st.session_state:
|
| 298 |
-
del st.session_state['video_path_from_component']
|
| 299 |
-
except Exception as e:
|
| 300 |
-
st.error(f"An error occurred: {e}")
|
| 301 |
-
else:
|
| 302 |
-
st.warning("Please describe the agreement details.")
|
| 303 |
-
|
| 304 |
-
with col2:
|
| 305 |
-
if 'legal_result' in st.session_state:
|
| 306 |
-
result = st.session_state.legal_result
|
| 307 |
-
|
| 308 |
-
st.subheader("Drafted Agreement")
|
| 309 |
-
with st.container(border=True):
|
| 310 |
-
st.markdown(result['legal_doc'])
|
| 311 |
-
|
| 312 |
-
pdf_bytes = generate_formatted_pdf(result['legal_doc'])
|
| 313 |
-
st.download_button(label="Download PDF", data=pdf_bytes, file_name="agreement.pdf", mime="application/pdf")
|
| 314 |
-
|
| 315 |
-
if result.get('legal_trivia') and result['legal_trivia'].trivia:
|
| 316 |
-
with st.expander("Legal Insights"):
|
| 317 |
-
for item in result['legal_trivia'].trivia:
|
| 318 |
-
st.markdown(f"**{item.point}**")
|
| 319 |
-
st.caption(item.explanation)
|
| 320 |
-
st.markdown(f"[Source]({item.source_url})")
|
| 321 |
-
|
| 322 |
-
st.divider()
|
| 323 |
-
|
| 324 |
-
st.subheader("Video Consent Recording")
|
| 325 |
-
st.info("Please record a video stating your name and that you agree to the terms above.")
|
| 326 |
-
|
| 327 |
-
saved_video_path = record_consent_video()
|
| 328 |
-
|
| 329 |
-
if saved_video_path:
|
| 330 |
-
st.session_state.video_path_from_component = saved_video_path
|
| 331 |
-
|
| 332 |
-
if st.session_state.get("video_path_from_component"):
|
| 333 |
-
st.success("Consent recorded successfully.")
|
| 334 |
-
st.video(st.session_state.video_path_from_component)
|
| 335 |
-
|
| 336 |
-
# --- TAB 2: Scheme Finder ---
|
| 337 |
-
with tab2:
|
| 338 |
-
st.header("Government Scheme Finder")
|
| 339 |
-
st.write("Find relevant government schemes based on your profile.")
|
| 340 |
-
|
| 341 |
-
user_profile = st.text_input("Enter your profile description...", key="scheme_profile", placeholder="E.g., A female farmer in Maharashtra owning 2 acres of land.")
|
| 342 |
-
|
| 343 |
-
if st.button("Search Schemes", type="primary", key="btn_find_schemes"):
|
| 344 |
-
if user_profile:
|
| 345 |
-
with st.spinner("Searching for schemes..."):
|
| 346 |
-
try:
|
| 347 |
-
from agents.scheme_chatbot import scheme_chatbot
|
| 348 |
-
response = scheme_chatbot.invoke({"user_profile": user_profile})
|
| 349 |
-
st.session_state.scheme_response = response
|
| 350 |
-
except Exception as e:
|
| 351 |
-
st.error(f"An error occurred during search: {e}")
|
| 352 |
-
else:
|
| 353 |
-
st.warning("Please enter a profile description.")
|
| 354 |
-
|
| 355 |
-
if 'scheme_response' in st.session_state:
|
| 356 |
-
response = st.session_state.scheme_response
|
| 357 |
-
st.subheader(f"Schemes for: '{user_profile}'")
|
| 358 |
-
if response and response.schemes:
|
| 359 |
-
for scheme in response.schemes:
|
| 360 |
-
with st.container(border=True):
|
| 361 |
-
st.markdown(f"#### {scheme.scheme_name}")
|
| 362 |
-
st.write(scheme.description)
|
| 363 |
-
st.write(f"**Target Audience:** {scheme.target_audience}")
|
| 364 |
-
st.markdown(f"[Official Website]({scheme.official_link})")
|
| 365 |
-
else:
|
| 366 |
-
st.info("No specific schemes found. Try a more detailed description.")
|
| 367 |
-
|
| 368 |
-
# --- TAB 3: Demystifier & Chat ---
|
| 369 |
-
with tab3:
|
| 370 |
-
st.header("Document Demystifier")
|
| 371 |
-
st.write("Upload a legal document to get a simplified summary and ask questions.")
|
| 372 |
-
|
| 373 |
-
uploaded_file = st.file_uploader("Upload PDF Document", type="pdf", key="demystify_uploader")
|
| 374 |
-
|
| 375 |
-
if uploaded_file and st.button("Analyze Document", type="primary"):
|
| 376 |
-
with st.spinner("Analyzing document..."):
|
| 377 |
-
try:
|
| 378 |
-
temp_file_path = os.path.join(PDF_UPLOAD_DIR, uploaded_file.name)
|
| 379 |
-
with open(temp_file_path, "wb") as f:
|
| 380 |
-
f.write(uploaded_file.getbuffer())
|
| 381 |
-
|
| 382 |
-
analysis_result = process_document_for_demystification(temp_file_path)
|
| 383 |
-
|
| 384 |
-
st.session_state.demystifier_report = analysis_result["report"]
|
| 385 |
-
st.session_state.rag_chain = analysis_result["rag_chain"]
|
| 386 |
-
except Exception as e:
|
| 387 |
-
st.error(f"Analysis failed: {e}")
|
| 388 |
-
|
| 389 |
-
if 'demystifier_report' in st.session_state:
|
| 390 |
-
st.divider()
|
| 391 |
-
report = st.session_state.demystifier_report
|
| 392 |
-
|
| 393 |
-
tab_summary, tab_chat = st.tabs(["Summary & Analysis", "Chat with Document"])
|
| 394 |
-
|
| 395 |
-
with tab_summary:
|
| 396 |
-
st.subheader("Document Summary")
|
| 397 |
-
st.write(report.summary)
|
| 398 |
-
|
| 399 |
-
st.subheader("Key Terms Explained")
|
| 400 |
-
for term in report.key_terms:
|
| 401 |
-
with st.expander(f"{term.term}"):
|
| 402 |
-
st.write(term.explanation)
|
| 403 |
-
st.markdown(f"[Learn More]({term.resource_link})")
|
| 404 |
-
|
| 405 |
-
st.info(f"**Advice:** {report.overall_advice}")
|
| 406 |
-
|
| 407 |
-
with tab_chat:
|
| 408 |
-
st.subheader("Ask Questions")
|
| 409 |
-
chat_interface(
|
| 410 |
-
handler_function=st.session_state.rag_chain.invoke,
|
| 411 |
-
session_state_key="doc_chat_history"
|
| 412 |
-
)
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
|
|
|
| 8 |
st.set_page_config(layout="wide", page_title="Jan-Contract Unified Assistant", page_icon="⚖️")
|
| 9 |
|
| 10 |
# Backend Configuration
|
| 11 |
+
default_url = os.getenv("BACKEND_URL", "http://127.0.0.1:8000")
|
| 12 |
+
|
| 13 |
+
with st.sidebar:
|
| 14 |
+
st.header("⚙️ Configuration")
|
| 15 |
+
BACKEND_URL = st.text_input(
|
| 16 |
+
"Backend API URL",
|
| 17 |
+
value=default_url,
|
| 18 |
+
help="Paste your Vercel/Railway deployment URL here (e.g., https://your-project.vercel.app)"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Test Connection
|
| 22 |
+
if st.button("Test Connection"):
|
| 23 |
+
try:
|
| 24 |
+
resp = requests.get(f"{BACKEND_URL}/health", timeout=5)
|
| 25 |
+
if resp.status_code == 200:
|
| 26 |
+
st.success("✅ Connected!")
|
| 27 |
+
else:
|
| 28 |
+
st.error(f"❌ Error: {resp.status_code}")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"❌ Failed: {e}")
|
| 31 |
|
| 32 |
+
# Only strip trailing slash if it exists
|
| 33 |
if BACKEND_URL.endswith("/"):
|
| 34 |
BACKEND_URL = BACKEND_URL[:-1]
|
| 35 |
|
|
|
|
| 248 |
st.error("Failed to get answer.")
|
| 249 |
except Exception as e:
|
| 250 |
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|