Spaces:
Sleeping

File size: 3,739 Bytes
fa1fead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from __future__ import annotations

import os
import streamlit as st

st.set_page_config(
    page_title="SmartDocs - Multilingual PDF Q&A",
    page_icon="📚",
    layout="wide",
    initial_sidebar_state="expanded",
)

from ui.components.upload_panel import render_upload_panel
from ui.components.query_panel import render_query_panel
from ui.components.answer_panel import stream_and_render_answer, render_previous_answer
from ui.components.cost_panel import render_cost_panel

# Reads SMARTDOCS_API_URL from HF Space secrets — set in Space settings
# Locally falls back to localhost for development
API_BASE = os.getenv("SMARTDOCS_API_URL", "http://localhost:7860").rstrip("/")
DEFAULT_USER_ID = "demo_user"


def _init_session_state() -> None:
    defaults = {
        "doc_id": None,
        "doc_title": "your document",
        "doc_language": "en",
        "doc_type": "other",
        "ingestion_status": None,
        "chunks_stored": 0,
        "last_answer": None,
        "last_sources": [],
        "last_metadata": {},
        "last_crag": False,
        "session_cost_inr": 0.0,
        "query_count": 0,
        "prefilled_query": "",
        "user_id": DEFAULT_USER_ID,
    }
    for k, v in defaults.items():
        if k not in st.session_state:
            st.session_state[k] = v


_init_session_state()

# Sidebar
if st.sidebar.button("Reset Session", use_container_width=True):
    for key in list(st.session_state.keys()):
        del st.session_state[key]
    st.rerun()

st.markdown(
    """
    <div style="
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        padding: 20px 24px;
        border-radius: 12px;
        margin-bottom: 24px;
        color: white;
    ">
        <h1 style="margin:0; font-size:1.8em;">SmartDocs</h1>
        <p style="margin:6px 0 0 0; opacity:0.9; font-size:0.95em;">
            Multilingual PDF Q&amp;A for Indian professionals.
            22 languages. No translation layer.
            Powered by Sarvam-30B.
        </p>
    </div>
    """,
    unsafe_allow_html=True,
)

with st.sidebar:
    with st.expander("User Identity", expanded=False):
        uid_input = st.text_input(
            "User ID",
            value=st.session_state.user_id,
            help="Each user sees only their own documents",
        )
        if uid_input != st.session_state.user_id:
            st.session_state.user_id = uid_input
            st.session_state.doc_id = None
            st.session_state.pop("last_answer", None)
            st.rerun()

user_id = st.session_state.user_id
render_upload_panel(api_base=API_BASE, user_id=user_id)
render_cost_panel()

if not st.session_state.get("doc_id"):
    st.markdown("---")
    c1, c2, c3 = st.columns(3)
    with c1:
        st.markdown("### 1 Upload\n\nAny PDF in any Indian language.")
    with c2:
        st.markdown("### 2 Detect\n\nLanguage detected automatically.")
    with c3:
        st.markdown("### 3 Ask\n\nQuery and answer in your language.")
    st.info("Upload a PDF in the sidebar to get started.")
    st.stop()

doc_title = st.session_state.get("doc_title", "your document")
st.markdown(f"**Document:** {doc_title}")
st.markdown("---")

query, language_override, submitted = render_query_panel()
st.markdown("---")

if submitted and query:
    st.session_state.pop("last_answer", None)
    st.session_state.pop("last_sources", None)
    st.session_state.pop("last_metadata", None)
    stream_and_render_answer(
        query=query,
        doc_id=st.session_state.doc_id,
        doc_title=doc_title,
        api_base=API_BASE,
        user_id=user_id,
    )
elif st.session_state.get("last_answer"):
    render_previous_answer()
else:
    st.info("Ask a question above to get started.")