Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -41
src/streamlit_app.py
CHANGED
|
@@ -10,38 +10,6 @@ from io import BytesIO
|
|
| 10 |
import tempfile
|
| 11 |
from pathlib import Path
|
| 12 |
|
| 13 |
-
# 檢查是否有openpyxl套件,用於Excel匯出
|
| 14 |
-
try:
|
| 15 |
-
import openpyxl
|
| 16 |
-
EXCEL_SUPPORT = True
|
| 17 |
-
except ImportError:
|
| 18 |
-
EXCEL_SUPPORT = False
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# 如果支援Excel格式,也提供Excel下載選項
|
| 22 |
-
if EXCEL_SUPPORT:
|
| 23 |
-
excel_buffer = BytesIO()
|
| 24 |
-
result_df.to_excel(excel_buffer, index=False, engine='openpyxl')
|
| 25 |
-
excel_data = excel_buffer.getvalue()
|
| 26 |
-
|
| 27 |
-
st.download_button(
|
| 28 |
-
label="下載結果為Excel",
|
| 29 |
-
data=excel_data,
|
| 30 |
-
file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",
|
| 31 |
-
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
| 32 |
-
key="excel_download" # 添加唯一key避免與CSV下載按鈕衝突
|
| 33 |
-
)
|
| 34 |
-
# 需要安裝:pip install openpyxl
|
| 35 |
-
excel_buffer = BytesIO()
|
| 36 |
-
result_df.to_excel(excel_buffer, index=False, engine='openpyxl')
|
| 37 |
-
excel_data = excel_buffer.getvalue()
|
| 38 |
-
|
| 39 |
-
st.download_button(
|
| 40 |
-
label="下載結果為Excel",
|
| 41 |
-
data=excel_data,
|
| 42 |
-
file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",
|
| 43 |
-
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
| 44 |
-
)
|
| 45 |
# 設定頁面配置
|
| 46 |
st.set_page_config(
|
| 47 |
page_title="PDF 處理與翻譯工具",
|
|
@@ -59,6 +27,13 @@ logging.basicConfig(
|
|
| 59 |
)
|
| 60 |
logger = logging.getLogger(__name__)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# 應用標題
|
| 63 |
st.title("PDF 內容處理與翻譯工具")
|
| 64 |
st.markdown("使用 Gemini API 來處理和翻譯 PDF 文件內容")
|
|
@@ -145,6 +120,31 @@ def process_with_gemini(model, text, instruction="請解釋以下內容"):
|
|
| 145 |
logger.error(f"Gemini處理失敗: {e}")
|
| 146 |
return f"處理失敗: {str(e)}"
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
# 上傳PDF文件
|
| 149 |
uploaded_file = st.file_uploader("上傳PDF文件", type="pdf")
|
| 150 |
|
|
@@ -206,6 +206,9 @@ if uploaded_file is not None:
|
|
| 206 |
"原始內容": [selected_page_content],
|
| 207 |
f"{target_language}翻譯": [translation]
|
| 208 |
})
|
|
|
|
|
|
|
|
|
|
| 209 |
else:
|
| 210 |
# 先解釋後翻譯
|
| 211 |
with st.spinner("解釋內容..."):
|
|
@@ -232,14 +235,8 @@ if uploaded_file is not None:
|
|
| 232 |
"內容解釋": [explanation],
|
| 233 |
f"{target_language}翻譯": [translation]
|
| 234 |
})
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
st.download_button(
|
| 239 |
-
label="下載結果為CSV",
|
| 240 |
-
data=csv,
|
| 241 |
-
file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
|
| 242 |
-
mime="text/csv",
|
| 243 |
-
)
|
| 244 |
else:
|
| 245 |
st.error("無法提取PDF內容,請檢查文件格式是否正確。")
|
|
|
|
| 10 |
import tempfile
|
| 11 |
from pathlib import Path
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# 設定頁面配置
|
| 14 |
st.set_page_config(
|
| 15 |
page_title="PDF 處理與翻譯工具",
|
|
|
|
| 27 |
)
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
+
# 檢查是否有openpyxl套件,用於Excel匯出
|
| 31 |
+
try:
|
| 32 |
+
import openpyxl
|
| 33 |
+
EXCEL_SUPPORT = True
|
| 34 |
+
except ImportError:
|
| 35 |
+
EXCEL_SUPPORT = False
|
| 36 |
+
|
| 37 |
# 應用標題
|
| 38 |
st.title("PDF 內容處理與翻譯工具")
|
| 39 |
st.markdown("使用 Gemini API 來處理和翻譯 PDF 文件內容")
|
|
|
|
| 120 |
logger.error(f"Gemini處理失敗: {e}")
|
| 121 |
return f"處理失敗: {str(e)}"
|
| 122 |
|
| 123 |
+
def provide_download_options(result_df):
|
| 124 |
+
"""提供下載選項"""
|
| 125 |
+
# 提供CSV下載選項
|
| 126 |
+
csv = result_df.to_csv(index=False)
|
| 127 |
+
st.download_button(
|
| 128 |
+
label="下載結果為CSV",
|
| 129 |
+
data=csv,
|
| 130 |
+
file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
|
| 131 |
+
mime="text/csv",
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# 如果支援Excel格式,也提供Excel下載選項
|
| 135 |
+
if EXCEL_SUPPORT:
|
| 136 |
+
excel_buffer = BytesIO()
|
| 137 |
+
result_df.to_excel(excel_buffer, index=False, engine='openpyxl')
|
| 138 |
+
excel_data = excel_buffer.getvalue()
|
| 139 |
+
|
| 140 |
+
st.download_button(
|
| 141 |
+
label="下載結果為Excel",
|
| 142 |
+
data=excel_data,
|
| 143 |
+
file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",
|
| 144 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
| 145 |
+
key="excel_download" # 添加唯一key避免與CSV下載按鈕衝突
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
# 上傳PDF文件
|
| 149 |
uploaded_file = st.file_uploader("上傳PDF文件", type="pdf")
|
| 150 |
|
|
|
|
| 206 |
"原始內容": [selected_page_content],
|
| 207 |
f"{target_language}翻譯": [translation]
|
| 208 |
})
|
| 209 |
+
|
| 210 |
+
# 提供下載選項
|
| 211 |
+
provide_download_options(result_df)
|
| 212 |
else:
|
| 213 |
# 先解釋後翻譯
|
| 214 |
with st.spinner("解釋內容..."):
|
|
|
|
| 235 |
"內容解釋": [explanation],
|
| 236 |
f"{target_language}翻譯": [translation]
|
| 237 |
})
|
| 238 |
+
|
| 239 |
+
# 提供下載選項
|
| 240 |
+
provide_download_options(result_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
else:
|
| 242 |
st.error("無法提取PDF內容,請檢查文件格式是否正確。")
|