-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path6_translate_UI.py
More file actions
198 lines (159 loc) · 8.07 KB
/
Copy path6_translate_UI.py
File metadata and controls
198 lines (159 loc) · 8.07 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import streamlit as st
import logging
import asyncio
import httpx
import os
import base64
from httpx import Cookies
PDF_PROCESSOR_URL = os.environ["PDF_PROCESSOR_URL"]
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
if "processed_data" not in st.session_state or st.session_state.processed_data is None:
st.session_state.processed_data = {}
if "httpx_cookies" not in st.session_state:
st.session_state.httpx_cookies = Cookies()
client = httpx.AsyncClient(cookies=st.session_state.httpx_cookies, timeout=300.0)
st.header("🌐 Translation")
async def get_translation_status(doc_id: str) -> dict:
"""Get translation status for a document."""
try:
response = await client.get(f"{PDF_PROCESSOR_URL}/translation/{doc_id}")
if response.status_code == 200:
return response.json()
elif response.status_code == 404:
return {"status": "not_found"}
else:
return {"status": "error", "error": f"HTTP {response.status_code}"}
except Exception as e:
logger.error(f"Error getting translation status: {e}")
return {"status": "error", "error": str(e)}
async def get_renderer_status(doc_id: str) -> dict:
"""Get renderer status for a document."""
try:
response = await client.get(f"{PDF_PROCESSOR_URL}/renderer/{doc_id}")
if response.status_code == 200:
return response.json()
elif response.status_code == 404:
return {"status": "not_found"}
else:
return {"status": "error", "error": f"HTTP {response.status_code}"}
except Exception as e:
logger.error(f"Error getting renderer status: {e}")
return {"status": "error", "error": str(e)}
async def trigger_renderer(doc_id: str) -> dict:
"""Trigger rendering for a translated document."""
try:
response = await client.post(f"{PDF_PROCESSOR_URL}/renderer/{doc_id}")
if response.status_code in [200, 201, 202]:
return {"status": "success", "response_code": response.status_code}
else:
return {"status": "error", "error": f"HTTP {response.status_code}"}
except Exception as e:
logger.error(f"Error triggering renderer: {e}")
return {"status": "error", "error": str(e)}
def display_translation_info(translation_data: dict):
"""Display translation metadata and statistics."""
result = translation_data.get("result", {})
source_lang = translation_data.get("source_lang", "Unknown")
target_lang = translation_data.get("target_lang", "Unknown")
st.markdown(f"**Translation:** {source_lang} → {target_lang}")
if result:
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Text Elements", len(result.get("texts", [])))
with col2:
st.metric("Tables", len(result.get("tables", [])))
with col3:
st.metric("Images", len(result.get("pictures", [])))
def display_rendered_pdf(doc_id: str, filename: str, runner: asyncio.Runner):
"""Display rendered PDF with download and preview options."""
# Server-side URL for fetching PDF (internal service communication)
server_pdf_url = f"{PDF_PROCESSOR_URL}/renderer/{doc_id}/rendered.pdf"
# Client-side URL for browser downloads (through nginx gateway)
client_pdf_url = f"/pdf_processor/renderer/{doc_id}/rendered.pdf"
try:
pdf_response = runner.run(client.get(server_pdf_url))
if pdf_response.status_code == 200:
# Download button at the top
st.download_button(
label="📥 Download Translated PDF",
data=pdf_response.content,
file_name=f"{filename.rsplit('.', 1)[0]}_translated.pdf",
mime="application/pdf",
key=f"download_{doc_id}",
use_container_width=True
)
# Preview section
with st.expander("👁️ Preview PDF", expanded=False):
base64_pdf = base64.b64encode(pdf_response.content).decode('utf-8')
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600px" type="application/pdf"></iframe>'
st.markdown(pdf_display, unsafe_allow_html=True)
else:
st.error(f"Failed to load PDF (HTTP {pdf_response.status_code})")
st.markdown(f"[Try direct download]({client_pdf_url})")
except Exception as e:
logger.error(f"Error fetching rendered PDF: {e}")
st.error("Error loading PDF. Try the download link below:")
st.markdown(f"[Download Translated PDF]({client_pdf_url})")
def display_renderer_section(doc_id: str, filename: str, renderer_data: dict, runner: asyncio.Runner):
"""Display renderer status and controls."""
status = renderer_data.get("status")
if status == "not_found":
st.info("💡 Generate a translated PDF with the translated text overlaid on the original document.")
if st.button("🎨 Generate Translated PDF", key=f"render_{doc_id}", use_container_width=True):
with st.spinner("Generating translated PDF..."):
trigger_result = runner.run(trigger_renderer(doc_id))
if trigger_result.get("status") == "success":
st.success("✅ Generation started! Refreshing...")
st.rerun()
else:
st.error(f"❌ Error: {trigger_result.get('error', 'Unknown error')}")
elif status == "processing":
st.info("⏳ Generating translated PDF...")
if st.button("🔄 Refresh", key=f"refresh_{doc_id}"):
st.rerun()
elif status == "completed":
display_rendered_pdf(doc_id, filename, runner)
elif status == "failed":
st.error("❌ PDF generation failed")
if st.button("🔄 Retry", key=f"retry_{doc_id}"):
with st.spinner("Retrying..."):
trigger_result = runner.run(trigger_renderer(doc_id))
if trigger_result.get("status") == "success":
st.success("✅ Restarted! Refreshing...")
st.rerun()
else:
st.error(f"❌ Error: {trigger_result.get('error', 'Unknown error')}")
elif status == "error":
st.error(f"❌ {renderer_data.get('error', 'Unknown error')}")
# Main content
if "processed_data" in st.session_state and st.session_state.processed_data:
response_lst = list(st.session_state.processed_data.items())
if not response_lst:
st.info("No documents have been processed yet. Upload a PDF to get started!")
else:
for doc_id, data in response_lst:
with st.expander(f"📄 {data['uploaded_filename']}", expanded=True):
runner = asyncio.Runner()
translation_data = runner.run(get_translation_status(doc_id))
status = translation_data.get("status")
if status == "not_found":
st.warning("⚠️ No translation available for this document")
st.info("Translation was not enabled when this document was uploaded, or it's still processing.")
elif status == "error":
st.error(f"❌ Error: {translation_data.get('error', 'Unknown error')}")
elif status == "processing":
st.info("⏳ Translation is currently processing...")
elif status == "completed":
display_translation_info(translation_data)
# Check and display renderer section
renderer_data = runner.run(get_renderer_status(doc_id))
display_renderer_section(doc_id, data['uploaded_filename'], renderer_data, runner)
elif status == "failed":
st.error("❌ Translation failed")
st.info("Please try uploading the document again.")
else:
st.warning(f"Unknown status: {status}")
else:
st.info("📤 No documents have been processed yet. Please upload and process a PDF first!")
st.markdown("Go to the **Upload PDF** page to get started.")