# smartdocs/ui/components/query_panel.py """ WHY THIS EXISTS: Renders the query input box and language selector dropdown. Language selector lets the user override auto-detection — useful when asking in a different language than the document (e.g. Tamil doc, English query). Per spec: "query input + language selector dropdown" """ from __future__ import annotations import streamlit as st # Languages available in the dropdown # ISO code → display name SUPPORTED_LANGUAGES = { "auto": "🔍 Auto-detect", "hi": "đŸ‡ŽđŸ‡ŗ Hindi", "en": "đŸ‡Ŧ🇧 English", "ta": "đŸ‡ŽđŸ‡ŗ Tamil", "te": "đŸ‡ŽđŸ‡ŗ Telugu", "kn": "đŸ‡ŽđŸ‡ŗ Kannada", "ml": "đŸ‡ŽđŸ‡ŗ Malayalam", "mr": "đŸ‡ŽđŸ‡ŗ Marathi", "gu": "đŸ‡ŽđŸ‡ŗ Gujarati", "pa": "đŸ‡ŽđŸ‡ŗ Punjabi", "bn": "đŸ‡ŽđŸ‡ŗ Bengali", "ur": "đŸ‡ŽđŸ‡ŗ Urdu", } # Example queries shown as quick-select chips EXAMPLE_QUERIES = { "en": [ "What is the total demand amount?", "What section of the law applies here?", "Summarize this document", "What are the key dates mentioned?", ], "hi": [ "⤇⤏ ā¤Ļ⤏āĨā¤¤ā¤žā¤ĩāĨ‡ā¤œā¤ŧ ā¤ŽāĨ‡ā¤‚ ⤕āĨā¤˛ ā¤Žā¤žā¤‚ā¤— ā¤°ā¤žā¤ļā¤ŋ ⤕āĨā¤¯ā¤ž ā¤šāĨˆ?", "ā¤¯ā¤šā¤žā¤ ⤕āĨŒā¤¨ ⤏āĨ€ ā¤§ā¤žā¤°ā¤ž ā¤˛ā¤žā¤—āĨ‚ ā¤šāĨ‹ā¤¤āĨ€ ā¤šāĨˆ?", "⤇⤏ ⤍āĨ‹ā¤Ÿā¤ŋ⤏ ā¤•ā¤ž ā¤¸ā¤žā¤°ā¤žā¤‚ā¤ļ ā¤Ŧā¤¤ā¤žā¤‡ā¤", "ā¤ŽāĨā¤–āĨā¤¯ ⤤ā¤ŋā¤Ĩā¤ŋā¤¯ā¤žā¤ ⤕āĨā¤¯ā¤ž ā¤šāĨˆā¤‚?", ], } def render_query_panel() -> tuple[str, str, bool]: """ Renders the query input section. Returns: tuple of: - query: str — the user's query text (empty string if not submitted) - language_override: str — "auto" or ISO code - submitted: bool — True if user clicked Ask or pressed Enter """ # ── Language selector ────────────────────────────────────────────────────── lang_col, _ = st.columns([1, 3]) with lang_col: language_override = st.selectbox( "Response language", options=list(SUPPORTED_LANGUAGES.keys()), format_func=lambda k: SUPPORTED_LANGUAGES[k], index=0, # Default: auto-detect help=( "Auto-detect reads your query's language automatically. " "Override if you want answers in a specific language." ), ) # ── Example queries (shown before first query) ───────────────────────────── if not st.session_state.get("last_answer"): doc_lang = st.session_state.get("doc_language", "en") examples = EXAMPLE_QUERIES.get(doc_lang, EXAMPLE_QUERIES["en"]) st.markdown("**Try an example:**") cols = st.columns(len(examples)) for col, example in zip(cols, examples): with col: if st.button( example, key=f"example_{hash(example)}", use_container_width=True, ): st.session_state.prefilled_query = example # ── Query input ──────────────────────────────────────────────────────────── prefilled = st.session_state.pop("prefilled_query", "") query = st.text_area( "Your question", value=prefilled, height=100, placeholder=( "Ask anything about your document...\n" "Hindi: ⤇⤏ ā¤Ļ⤏āĨā¤¤ā¤žā¤ĩāĨ‡ā¤œā¤ŧ ā¤ŽāĨ‡ā¤‚ ⤕āĨā¤¯ā¤ž ⤞ā¤ŋā¤–ā¤ž ā¤šāĨˆ?\n" "Tamil: āŽ‡āŽ¨ā¯āŽ¤ āŽ†āŽĩāŽŖāŽ¤ā¯āŽ¤āŽŋāŽ˛ā¯ āŽŽāŽŠā¯āŽŠ āŽ‰āŽŗā¯āŽŗāŽ¤ā¯?" ), label_visibility="collapsed", key="query_input", ) # ── Submit button ────────────────────────────────────────────────────────── ask_col, clear_col = st.columns([4, 1]) with ask_col: submitted = st.button( "🔍 Ask SmartDocs", use_container_width=True, type="primary", disabled=not st.session_state.get("doc_id"), help="Upload a document first" if not st.session_state.get("doc_id") else "", ) with clear_col: if st.button("đŸ—‘ī¸ Clear", use_container_width=True): st.session_state.pop("last_answer", None) st.session_state.pop("last_sources", None) st.session_state.pop("last_metadata", None) st.rerun() if submitted and not query.strip(): st.warning("Please enter a question before clicking Ask.") submitted = False return query.strip(), language_override, submitted