Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,9 +56,10 @@ class NotebookLMTool:
|
|
| 56 |
progress(0.1 + (0.8 * (i / len(images))), desc=f"AI 正在處理第 {i+1}/{len(images)} 頁...")
|
| 57 |
|
| 58 |
# --- 步驟 A: 提取文字 (OCR) ---
|
|
|
|
| 59 |
try:
|
| 60 |
resp_text = self.client.models.generate_content(
|
| 61 |
-
model="gemini-2.5-flash",
|
| 62 |
contents=["Extract all text content from this slide strictly.", img]
|
| 63 |
)
|
| 64 |
page_content = resp_text.text if resp_text.text else "[No Text Found]"
|
|
@@ -67,34 +68,34 @@ class NotebookLMTool:
|
|
| 67 |
|
| 68 |
full_text += f"=== Page {i+1} ===\n{page_content}\n\n"
|
| 69 |
|
| 70 |
-
# --- 步驟 B: 圖片去字 (
|
|
|
|
| 71 |
save_name = f"slide_{i+1:02d}.png"
|
| 72 |
final_path = os.path.join(img_output_dir, save_name)
|
| 73 |
|
| 74 |
try:
|
| 75 |
-
# 參考你的 JS:使用 responseModalities = ["IMAGE"]
|
| 76 |
resp_img = self.client.models.generate_content(
|
| 77 |
-
model="gemini-2.
|
| 78 |
contents=[
|
| 79 |
"Remove all text from this image. Fill the gaps using the surrounding background texture to make it look clean and natural. Output ONLY the image.",
|
| 80 |
img
|
| 81 |
],
|
| 82 |
config=types.GenerateContentConfig(
|
| 83 |
-
response_modalities=["IMAGE"]
|
| 84 |
)
|
| 85 |
)
|
| 86 |
|
| 87 |
-
# 處理圖片回傳 (SDK
|
| 88 |
image_data = None
|
| 89 |
|
| 90 |
-
#
|
| 91 |
if hasattr(resp_img, 'parts') and resp_img.parts:
|
| 92 |
for part in resp_img.parts:
|
| 93 |
if part.inline_data:
|
| 94 |
image_data = part.inline_data.data
|
| 95 |
break
|
| 96 |
|
| 97 |
-
#
|
| 98 |
if image_data is None and hasattr(resp_img, 'bytes') and resp_img.bytes:
|
| 99 |
image_data = resp_img.bytes
|
| 100 |
|
|
@@ -110,15 +111,15 @@ class NotebookLMTool:
|
|
| 110 |
gallery_preview.append((final_path, f"Page {i+1} (Cleaned)"))
|
| 111 |
print(f"Page {i+1}: Image generated successfully.")
|
| 112 |
else:
|
| 113 |
-
#
|
| 114 |
-
print(f"Page {i+1} Failed: No image data
|
| 115 |
img.save(final_path)
|
| 116 |
-
gallery_preview.append((final_path, f"Page {i+1} (
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
print(f"Page {i+1} Error: {str(e)}")
|
| 120 |
img.save(final_path)
|
| 121 |
-
gallery_preview.append((final_path, f"Page {i+1} (
|
| 122 |
|
| 123 |
# 4. 打包結果
|
| 124 |
progress(0.9, desc="正在打包 ZIP...")
|
|
@@ -140,7 +141,7 @@ tool = NotebookLMTool()
|
|
| 140 |
|
| 141 |
# --- Gradio UI ---
|
| 142 |
with gr.Blocks(title="NotebookLM Slide Decomposer", theme=gr.themes.Soft()) as demo:
|
| 143 |
-
gr.Markdown("# 🛠️ NotebookLM 投影片拆解助手 (
|
| 144 |
gr.Markdown("上傳 PDF,AI 自動幫你:**1. 抓出所有文字** | **2. 重繪乾淨背景圖**")
|
| 145 |
|
| 146 |
with gr.Row():
|
|
|
|
| 56 |
progress(0.1 + (0.8 * (i / len(images))), desc=f"AI 正在處理第 {i+1}/{len(images)} 頁...")
|
| 57 |
|
| 58 |
# --- 步驟 A: 提取文字 (OCR) ---
|
| 59 |
+
# 使用標準 Flash 模型處理文字,速度最快
|
| 60 |
try:
|
| 61 |
resp_text = self.client.models.generate_content(
|
| 62 |
+
model="gemini-2.5-flash",
|
| 63 |
contents=["Extract all text content from this slide strictly.", img]
|
| 64 |
)
|
| 65 |
page_content = resp_text.text if resp_text.text else "[No Text Found]"
|
|
|
|
| 68 |
|
| 69 |
full_text += f"=== Page {i+1} ===\n{page_content}\n\n"
|
| 70 |
|
| 71 |
+
# --- 步驟 B: 圖片去字 (Image Generation) ---
|
| 72 |
+
# 關鍵修改:必須使用 'gemini-2.0-flash-exp' 且該模型目前才支援 IMAGE 輸出
|
| 73 |
save_name = f"slide_{i+1:02d}.png"
|
| 74 |
final_path = os.path.join(img_output_dir, save_name)
|
| 75 |
|
| 76 |
try:
|
|
|
|
| 77 |
resp_img = self.client.models.generate_content(
|
| 78 |
+
model="gemini-2.0-flash-exp", # ✅ 修正:使用支援圖片輸出的實驗模型
|
| 79 |
contents=[
|
| 80 |
"Remove all text from this image. Fill the gaps using the surrounding background texture to make it look clean and natural. Output ONLY the image.",
|
| 81 |
img
|
| 82 |
],
|
| 83 |
config=types.GenerateContentConfig(
|
| 84 |
+
response_modalities=["IMAGE"] # ✅ 修正:明確告知需要圖片模態
|
| 85 |
)
|
| 86 |
)
|
| 87 |
|
| 88 |
+
# 處理圖片回傳 (解析 SDK 回應)
|
| 89 |
image_data = None
|
| 90 |
|
| 91 |
+
# 檢查 inline_data (Base64)
|
| 92 |
if hasattr(resp_img, 'parts') and resp_img.parts:
|
| 93 |
for part in resp_img.parts:
|
| 94 |
if part.inline_data:
|
| 95 |
image_data = part.inline_data.data
|
| 96 |
break
|
| 97 |
|
| 98 |
+
# 部分 SDK 版本可能直接放在 bytes
|
| 99 |
if image_data is None and hasattr(resp_img, 'bytes') and resp_img.bytes:
|
| 100 |
image_data = resp_img.bytes
|
| 101 |
|
|
|
|
| 111 |
gallery_preview.append((final_path, f"Page {i+1} (Cleaned)"))
|
| 112 |
print(f"Page {i+1}: Image generated successfully.")
|
| 113 |
else:
|
| 114 |
+
# 失敗回退:保留原圖並標記
|
| 115 |
+
print(f"Page {i+1} Failed: No image data. Text: {resp_img.text if hasattr(resp_img, 'text') else 'Unknown'}")
|
| 116 |
img.save(final_path)
|
| 117 |
+
gallery_preview.append((final_path, f"Page {i+1} (Original - Gen Failed)"))
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
print(f"Page {i+1} Error: {str(e)}")
|
| 121 |
img.save(final_path)
|
| 122 |
+
gallery_preview.append((final_path, f"Page {i+1} (Original - Error)"))
|
| 123 |
|
| 124 |
# 4. 打包結果
|
| 125 |
progress(0.9, desc="正在打包 ZIP...")
|
|
|
|
| 141 |
|
| 142 |
# --- Gradio UI ---
|
| 143 |
with gr.Blocks(title="NotebookLM Slide Decomposer", theme=gr.themes.Soft()) as demo:
|
| 144 |
+
gr.Markdown("# 🛠️ NotebookLM 投影片拆解助手 (V4 最終修復版)")
|
| 145 |
gr.Markdown("上傳 PDF,AI 自動幫你:**1. 抓出所有文字** | **2. 重繪乾淨背景圖**")
|
| 146 |
|
| 147 |
with gr.Row():
|