File size: 15,569 Bytes
9b4503a
 
a42cff9
e485414
b55d531
b0bbdeb
 
e501f08
9b4503a
 
ff03c35
9b4503a
e485414
b55d531
9b4503a
 
 
a8f6954
e485414
 
9b4503a
 
a8f6954
9ddb9de
b55d531
 
 
 
 
 
9ddb9de
7ed7122
 
 
b55d531
 
 
 
 
ff03c35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bba696
 
e501f08
 
 
b0bbdeb
9b4503a
 
d60ae3b
da05110
ff03c35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b4503a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da05110
c5386f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da05110
ff03c35
 
 
 
 
 
9b4503a
ff03c35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b4503a
ff03c35
da05110
ff03c35
 
 
 
 
 
e485414
 
ff03c35
 
 
 
 
 
 
 
 
 
 
 
 
e485414
a42cff9
 
 
 
 
 
 
b55d531
e485414
 
 
 
b55d531
f3231bf
20111b1
 
 
f3231bf
7377ef6
 
 
 
 
ff03c35
7377ef6
 
 
 
 
 
ff03c35
bf97479
 
 
 
ff03c35
 
 
 
 
 
 
 
 
7377ef6
 
e485414
 
7377ef6
 
 
 
 
 
 
 
 
a42cff9
 
 
 
 
 
 
9b4503a
 
 
 
ff03c35
a42cff9
9b4503a
 
7377ef6
 
b55d531
c74a768
 
 
5737001
9b4503a
 
e485414
ff03c35
e485414
 
ff03c35
 
e485414
a42cff9
 
 
 
9b4503a
 
48312c4
9b4503a
c5386f6
a42cff9
 
c5386f6
 
 
9b4503a
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -*- coding: utf-8 -*-

from typing import Container
from config.config import PASSWORD
import gradio as gr
import os
import shutil
import tempfile
from google import genai
from google.genai import types
import yt_dlp

from initializer import initialize_clients, initialize_password

# 初始化 Google Cloud Storage 服務和 GENAI 客戶端
GCS_SERVICE, GENAI_CLIENT = initialize_clients()
GCS_CLIENT = GCS_SERVICE.client

PASSWORD = initialize_password()

def toggle_visibility(toggle_value):
    return gr.update(visible=toggle_value)

def mock_question_answer(question, history):
    # 假資料模擬回答
    answers = {
        "文件的核心觀點是什麼?": "這份文件的核心觀點是關於人工智慧如何提升工作效率。",
        "有哪些關鍵詞或數據?": "關鍵詞包括:人工智慧、工作效率、數據分析。",
        "文件的摘要是什麼?": "這份文件討論了如何利用人工智慧工具,提升企業的運營效率和決策速度。"
    }
    response = answers.get(question, "抱歉,我無法回答這個問題。請嘗試其他問題!")
    history.append({"role": "user", "content": question})
    history.append({"role": "assistant", "content": response})
    return history, ""

def mock_summary():
    # 假資料模擬摘要
    return "這份文件主要討論人工智慧在工作效率提升方面的應用,並提供了實際案例來說明其價值。"

def get_youtube_title(url):
    """獲取 YouTube 影片標題"""
    try:
        # 確保 URL 格式完整
        if not url.startswith('http'):
            if 'watch?v=' in url:
                url = f'https://www.youtube.com/{url}'
            else:
                url = f'https://www.youtube.com/watch?v={url}'
        
        ydl_opts = {
            'quiet': True,
            'no_warnings': True,
            'extract_flat': True
        }
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=False)
            title = info.get('title', url)
            print(f"YouTube title: {title}")
            return title
    except Exception as e:
        print(f"Error fetching YouTube title: {str(e)}")
        return url

def add_to_file_list(file, file_list):
    if file:
        temp_dir = tempfile.gettempdir()
        temp_path = os.path.join(temp_dir, os.path.basename(file.name))
        shutil.copy(file.name, temp_path)  # 將文件存儲到臨時目錄
        file_list.append(temp_path)
    display_list = [os.path.basename(path) if os.path.basename(path) else path for path in file_list]
    return gr.update(choices=display_list), None

def add_youtube_to_list(youtube_link, file_list):
    if not youtube_link:
        return gr.update(choices=[item.split("|||")[0] if "|||" in item else os.path.basename(item) for item in file_list]), ""
    
    # 獲取標題
    title = get_youtube_title(youtube_link)
    
    # 確保 URL 格式完整
    if not youtube_link.startswith('http'):
        if 'watch?v=' in youtube_link:
            youtube_link = f'https://www.youtube.com/{youtube_link}'
        else:
            youtube_link = f'https://www.youtube.com/watch?v={youtube_link}'
    
    # 存儲格式:[title]|||[url]
    file_list.append(f"{title}|||{youtube_link}")
    display_list = [item.split("|||")[0] if "|||" in item else os.path.basename(item) for item in file_list]
    print(f"File list: {file_list}")
    print(f"Display list: {display_list}")
    return file_list, ""

def generate_transcript(youtube_link):
    print(f"\n開始生成 YouTube 逐字稿: {youtube_link}")
    try:
        print("初始化 Gemini 模型設定...")
        video = types.Part.from_uri(
            file_uri=youtube_link,
            mime_type="video/*",
        )

        model = "gemini-2.0-flash-exp"
        contents = [
            types.Content(
                role="user",
                parts=[
                    video,
                    types.Part.from_text("""請給我帶時間軸的逐字稿,請統一用 zhTW語言""")
                ]
            )
        ]
        generate_content_config = types.GenerateContentConfig(
            temperature=1,
            top_p=0.95,
            max_output_tokens=8192,
            response_modalities=["TEXT"],
            safety_settings=[
                types.SafetySetting(category="HARM_CATEGORY_HATE_SPEECH", threshold="OFF"),
                types.SafetySetting(category="HARM_CATEGORY_DANGEROUS_CONTENT", threshold="OFF"),
                types.SafetySetting(category="HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold="OFF"),
                types.SafetySetting(category="HARM_CATEGORY_HARASSMENT", threshold="OFF")
            ],
        )

        print("開始串流生成逐字稿...")
        transcript_text = ""
        for chunk in GENAI_CLIENT.models.generate_content_stream(
            model=model,
            contents=contents,
            config=generate_content_config,
        ):
            # Extract only text content from candidates
            if hasattr(chunk, 'candidates') and chunk.candidates:
                for candidate in chunk.candidates:
                    if (hasattr(candidate, 'content') and 
                        hasattr(candidate.content, 'parts')):
                        for part in candidate.content.parts:
                            if hasattr(part, 'text') and part.text:
                                transcript_text += part.text
            print(".", end="", flush=True)
        
        print("\n逐字稿生成完成!")
        return transcript_text
    except Exception as e:
        print(f"\n生成逐字稿時發生錯誤: {str(e)}")
        raise

def generate_summary(transcript):
    """Generate a summary from the transcript using Gemini."""
    try:
        print("\n開始生成摘要...")
        model = "gemini-2.0-flash-exp"
        contents = [
            types.Content(
                role="user",
                parts=[
                    types.Part.from_text(
                        f"""請根據以下逐字稿生成重點摘要,以條列方式呈現主要觀點:

{transcript}

請以下列格式輸出:
# 主要觀點:
1. [重點1]
2. [重點2]
...

# 結論:
[整體結論]
"""
                    )
                ]
            )
        ]
        
        response = GENAI_CLIENT.models.generate_content(
            model=model,
            contents=contents,
        )
        
        print("摘要生成完成!")
        return response.text
    except Exception as e:
        print(f"\n生成摘要時發生錯誤: {str(e)}")
        raise

def process_all_files(file_list):
    """處理所有選中的文件"""
    if not file_list:
        return "請選擇要處理的文件", ""

    all_text = []
    status_messages = []
    
    for item in file_list:
        try:
            if "|||" in item:
                # YouTube 連結
                title, url = item.split("|||")
                print(f"處理 YouTube: {title}")
                try:
                    transcript = generate_transcript(url)
                    if transcript:
                        all_text.append(f"=== {title} ===\n{transcript}")
                        status_messages.append(f"🟢 成功處理 YouTube 影片:{title}")
                    else:
                        status_messages.append(f"🔴 無法獲取影片逐字稿:{title}")
                except Exception as e:
                    if "無法取得影片資訊" in str(e):
                        # 可能是影片標題問題,但還是有內容
                        all_text.append(f"=== YouTube 影片 ===\n{e.transcript if hasattr(e, 'transcript') else ''}")
                        status_messages.append(f"🟡 影片資訊不完整,但已處理內容:{url}")
                    else:
                        status_messages.append(f"🔴 處理失敗:{title}{str(e)})")
            else:
                # 本地文件
                filename = os.path.basename(item)
                print(f"處理文件: {filename}")
                try:
                    with open(item, 'r', encoding='utf-8') as f:
                        content = f.read()
                        try:
                            # 嘗試解碼文件名
                            decoded_name = filename.encode('latin1').decode('utf-8')
                            all_text.append(f"=== {decoded_name} ===\n{content}")
                            status_messages.append(f"🟢 成功處理文件:{decoded_name}")
                        except:
                            # 文件名有問題,但內容可用
                            all_text.append(f"=== 文件內容 ===\n{content}")
                            status_messages.append(f"🟡 文件名稱無法正確顯示,但已處理內容:{filename}")
                except UnicodeDecodeError:
                    try:
                        # 嘗試其他編碼
                        for encoding in ['big5', 'gbk', 'shift-jis']:
                            try:
                                with open(item, 'r', encoding=encoding) as f:
                                    content = f.read()
                                    all_text.append(f"=== {filename} ===\n{content}")
                                    status_messages.append(f"🟡 使用 {encoding} 編碼成功讀取文件:{filename}")
                                    break
                            except:
                                continue
                        else:
                            status_messages.append(f"🔴 無法讀取文件內容:{filename}(編碼問題)")
                    except Exception as e:
                        status_messages.append(f"🔴 讀取文件失敗:{filename}{str(e)})")
                except Exception as e:
                    status_messages.append(f"🔴 讀取文件失敗:{filename}{str(e)})")
        except Exception as e:
            status_messages.append(f"🔴 處理失敗:{item}{str(e)})")

    if not all_text:
        return "❌ 沒有成功處理任何文件", ""

    # 合併所有文本
    combined_text = "\n\n".join(all_text)
    status_text = "\n".join(status_messages)
    
    return f"處理完成\n{status_text}", combined_text

def process_with_auth(password, file_list, file_display):
    """帶密碼驗證的文件處理"""
    if not file_display:  # 使用 file_display 而不是 file_list
        return "請選擇要處理的文件", "", gr.update(visible=False)
        
    if password != PASSWORD:
        return "請輸入正確的密碼", "", gr.update(visible=False)
    
    # 根據顯示的選項找到對應的完整項目
    selected_files = []
    for item in file_list:
        if "|||" in item:
            title = item.split("|||")[0]
            if title in file_display:
                selected_files.append(item)
        else:
            if os.path.basename(item) in file_display:
                selected_files.append(item)
    
    result_text, transcript_text = process_all_files(selected_files)
    return result_text, transcript_text, gr.update(visible=True)

def on_summary_click(transcript):
    if not transcript:
        return "請先上傳文件或輸入 YouTube 連結並處理完成後再生成摘要。"
    
    summary = generate_summary(transcript)
    return summary

with gr.Blocks() as demo:

    with gr.Row():
        gr.Markdown("# AI Notes Assistant")
        password_input = gr.Textbox(label="password")

    with gr.Row():
        source_toggle = gr.Checkbox(label="顯示來源選單", value=True)
        chat_toggle = gr.Checkbox(label="顯示對話區域", value=True)
        feature_toggle = gr.Checkbox(label="顯示功能卡片", value=True)

    with gr.Row():
        with gr.Column(visible=True) as source_column:
            gr.Markdown("### 來源選單")
            
            file_list = gr.State([])
            file_display = gr.State([])
            
            with gr.Tab("YouTube 連結"):
                youtube_link = gr.Textbox(label="輸入 YouTube 連結")
                add_youtube_button = gr.Button("添加到來源列表")
                add_youtube_button.click(add_youtube_to_list, inputs=[youtube_link, file_list], outputs=[file_list, youtube_link])

            with gr.Tab("上傳檔案(TODO)"):
                upload_file = gr.File(label="從電腦添加文件", file_types=[".txt", ".pdf", ".docx"])
                add_file_button = gr.Button("添加到來源列表")
                add_file_button.click(add_to_file_list, inputs=[upload_file, file_list], outputs=[file_list, upload_file])        
            
            file_display_input = gr.CheckboxGroup(label="已上傳的文件", interactive=True)

            # 更新顯示邏輯
            def update_display(file_list):
                display_list = [item.split("|||")[0] if "|||" in item else os.path.basename(item) for item in file_list]
                print(f"Updating display with: {display_list}")
                return gr.update(choices=display_list, value=[])
            
            file_list.change(update_display, inputs=file_list, outputs=file_display_input)

            process_files_button = gr.Button("處理檔案")
            rag_result = gr.Textbox(label="處理狀態", interactive=False)
            
        with gr.Column(visible=True) as chat_column:
            gr.Markdown("### 對話區域")
            chatbot = gr.Chatbot(label="聊天記錄", type="messages")
            question = gr.Textbox(label="輸入問題,例如:文件的核心觀點是什麼?")
            ask_button = gr.Button("提問")

        with gr.Column(visible=True) as feature_column:
            gr.Markdown("### 功能卡片")
            with gr.Tab("摘要生成"):
                summary_button = gr.Button("生成摘要", visible=False)
                summary_output = gr.Markdown(
                    label="摘要",
                    show_label=True,
                    show_copy_button=True,
                    container=True
                )
            with gr.Tab("逐字稿"):
                transcript_display = gr.Textbox(
                    label="YouTube 逐字稿", 
                    interactive=False, 
                    lines=20,
                    show_copy_button=True,
                    placeholder="處理 YouTube 影片後,逐字稿將顯示在這裡..."
                )
            with gr.Tab("其他功能"):
                gr.Markdown("此處可以添加更多功能卡片")

    source_toggle.change(toggle_visibility, inputs=source_toggle, outputs=source_column)
    chat_toggle.change(toggle_visibility, inputs=chat_toggle, outputs=chat_column)
    feature_toggle.change(toggle_visibility, inputs=feature_toggle, outputs=feature_column)

    # 更新處理檔案按鈕的事件處理
    process_files_button.click(
        fn=process_with_auth,
        inputs=[password_input, file_list, file_display_input],
        outputs=[
            rag_result, 
            transcript_display,
            summary_button
        ]
    ).then(
        fn=on_summary_click,
        inputs=[transcript_display],
        outputs=[summary_output]
    )

    history = gr.State([])
    ask_button.click(mock_question_answer, inputs=[question, history], outputs=[chatbot, question])
    summary_button.click(
        fn=on_summary_click, 
        inputs=[transcript_display], 
        outputs=[summary_output]
    )

demo.launch(share=True)