Spaces:
Running
Running
| import streamlit as st | |
| import requests | |
| from mtranslate import translate | |
| import time | |
| st.set_page_config(page_title="Sakya Library Bridge", page_icon="鈽革笍", layout="wide") | |
| st.title("鈽革笍 Sakya Library English Bridge") | |
| # Sidebar | |
| with st.sidebar: | |
| st.header("Search Settings") | |
| search_type = st.radio("Search Target:", ["Content", "Title"], index=0) | |
| display_count = st.slider("Results to fetch:", 5, 50, 15) | |
| st.info("The 'HTML Reader' link below is optimized for Chrome's 'Translate to English' feature.") | |
| query = st.text_input("Enter English keyword:", placeholder="e.g., Compassion") | |
| if st.button("Search Library", type="primary"): | |
| if query: | |
| try: | |
| with st.spinner(f'Translating "{query}"...'): | |
| tibetan_term = translate(query, 'bo', 'auto') | |
| st.success(f"Tibetan Search Term: **{tibetan_term}**") | |
| endpoint = "GetDatatableSearchContent" if search_type == "Content" else "GetDatatableSearchTitle" | |
| base_url = f"http://sakyalibrary.com/Library/{endpoint}" | |
| params = { | |
| "DataTableParam.Echo": "1", | |
| "DataTableParam.DisplayStart": "0", | |
| "DataTableParam.DisplayLength": str(display_count), | |
| "skey": tibetan_term, | |
| "_": str(int(time.time() * 1000)) | |
| } | |
| headers = { | |
| "User-Agent": "Mozilla/5.0", | |
| "X-Requested-With": "XMLHttpRequest" | |
| } | |
| response = requests.get(base_url, params=params, headers=headers, timeout=25) | |
| if response.status_code == 200: | |
| data = response.json() | |
| results = data.get('aaData', []) | |
| if not results: | |
| st.warning("No matches found.") | |
| else: | |
| for item in results: | |
| item_id = item.get('Id') | |
| name = item.get('Name', 'Untitled') | |
| content_snippet = item.get('Content', '') | |
| # PUBLIC READER URL (Bypasses the login dialogue) | |
| # Adding the page (pg=1) and skey helps the reader highlight your word | |
| public_reader_url = f"http://sakyalibrary.com/Library/Book/{item_id}?pg=1&skey={tibetan_term}" | |
| pdf_url = f"http://sakyalibrary.com{item.get('PdfUrl')}" if item.get('PdfUrl') else None | |
| with st.expander(f"馃摉 {name}", expanded=True): | |
| # AI Translation of the Title/Snippet for context | |
| with st.chat_message("assistant"): | |
| snippet_en = translate(content_snippet, 'en', 'bo') | |
| st.write(f"**English Summary:** {snippet_en}") | |
| st.caption(f"**Tibetan Source:** {content_snippet}") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| # This link opens the public 'Book' interface | |
| st.link_button("馃寪 Open in Reader (Translatable)", public_reader_url) | |
| with col2: | |
| if pdf_url: | |
| st.link_button("馃搫 Download PDF", pdf_url) | |
| else: | |
| st.error("Library server unreachable.") | |
| except Exception as e: | |
| st.error(f"Error: {e}") |