youngtsai commited on
Commit
d43c4df
·
1 Parent(s): 7e3c5ed
Files changed (1) hide show
  1. app.py +71 -15
app.py CHANGED
@@ -114,7 +114,7 @@ def upload_file_to_gcs_with_json_string(gcs_client, bucket_name, destination_blo
114
  bucket = gcs_client.bucket(bucket_name)
115
  blob = bucket.blob(destination_blob_name)
116
  blob.upload_from_string(json_string)
117
- print(f"JSON string uploaded to {destination_blob_name} in GCS.")
118
 
119
  def download_blob_to_string(gcs_client, bucket_name, source_blob_name):
120
  """从 GCS 下载文件内容到字符串"""
@@ -175,6 +175,13 @@ def copy_file_from_drive_to_gcs(drive_service, gcs_client, file_id, bucket_name,
175
  blob.upload_from_string(file_content)
176
  print(f"File {file_id} copied to GCS at {gcs_destination_path}.")
177
 
 
 
 
 
 
 
 
178
  # # ====drive====初始化
179
  def init_drive_service():
180
  credentials_json_string = DRIVE_KEY
@@ -763,16 +770,39 @@ def generate_reading_passage(df_string):
763
 
764
  return reading_passage
765
 
766
- def edit_generate_reading():
767
- # 將 reading_passage 設置為可編輯模式並顯示更新按鈕
768
- return gr.update(interactive=True), gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769
 
770
- def update_reading_passage(new_content, video_id):
771
- # 更新 GCS 上的 reading_passage 內容
772
- # 此處假設已有函數 update_reading_passage_to_gcs 用於更新 GCS 內容
773
- # update_reading_passage_to_gcs(video_id, new_content)
774
- # reading_passage 設置回唯讀模式並隱藏更新按鈕
775
- return gr.update(value=new_content, interactive=False), gr.update(visible=False)
776
 
777
 
778
  def text_to_speech(video_id, text):
@@ -1545,12 +1575,16 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
1545
  ai_msg = gr.Textbox(label="Message")
1546
  ai_send_button = gr.Button("Send", variant="primary")
1547
  with gr.Tab("文章模式"):
1548
- reading_passage = gr.Textbox(label="Reading Passage", lines=40, show_label=False)
1549
- reading_passage_speak_button = gr.Button("Speak", visible=False)
1550
- reading_passage_audio_output = gr.Audio(label="Audio Output", visible=False)
1551
  with gr.Row() as reading_passage_admin:
1552
- reading_passage_edit_button = gr.Button("編輯")
1553
- reading_passage_update_button = gr.Button("更新")
 
 
 
 
 
 
 
1554
  with gr.Tab("重點摘要"):
1555
  df_summarise = gr.Textbox(container=True, show_copy_button=True, lines=40, show_label=False)
1556
  with gr.Tab("教學備課"):
@@ -1759,6 +1793,28 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
1759
  # 当输入网页链接时触发
1760
  # web_link.change(process_web_link, inputs=web_link, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
1761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1762
  # 教師版
1763
  worksheet_content_btn.click(
1764
  get_ai_content,
 
114
  bucket = gcs_client.bucket(bucket_name)
115
  blob = bucket.blob(destination_blob_name)
116
  blob.upload_from_string(json_string)
117
+ print(f"JSON string uploaded to {destination_blob_name} in GCS.")
118
 
119
  def download_blob_to_string(gcs_client, bucket_name, source_blob_name):
120
  """从 GCS 下载文件内容到字符串"""
 
175
  blob.upload_from_string(file_content)
176
  print(f"File {file_id} copied to GCS at {gcs_destination_path}.")
177
 
178
+ def delete_blob(gcs_client, bucket_name, blob_name):
179
+ """删除指定的 GCS 对象"""
180
+ bucket = gcs_client.bucket(bucket_name)
181
+ blob = bucket.blob(blob_name)
182
+ blob.delete()
183
+ print(f"Blob {blob_name} deleted from GCS.")
184
+
185
  # # ====drive====初始化
186
  def init_drive_service():
187
  credentials_json_string = DRIVE_KEY
 
770
 
771
  return reading_passage
772
 
773
+ def enable_edit_mode():
774
+ return gr.update(interactive=True)
775
+
776
+ def delete_reading_passage(video_id):
777
+ print("===delete_reading_passage===")
778
+ gcs_client = GCS_CLIENT
779
+ bucket_name = 'video_ai_assistant'
780
+ file_name = f'{video_id}_reading_passage.json'
781
+ blob_name = f"{video_id}/{file_name}"
782
+ # 检查 reading_passage 是否存在
783
+ is_file_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
784
+ if is_file_exists:
785
+ delete_blob(gcs_client, bucket_name, blob_name)
786
+ print("reading_passage已从GCS中删除")
787
+ return gr.update(value="", interactive=False)
788
+
789
+ def update_reading_passage(video_id, new_content):
790
+ print("===upfdate_reading_passage on gcs===")
791
+ gcs_client = GCS_CLIENT
792
+ bucket_name = 'video_ai_assistant'
793
+ file_name = f'{video_id}_reading_passage.json'
794
+ blob_name = f"{video_id}/{file_name}"
795
+ reading_passage_json = {"reading_passage": str(new_content)}
796
+ reading_passage_text = json.dumps(reading_passage_json, ensure_ascii=False, indent=2)
797
+ upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, reading_passage_text)
798
+ print("reading_passage已更新到GCS")
799
+ return gr.update(value=new_content, interactive=False)
800
 
801
+ def create_reading_passage(video_id, df_string):
802
+ print("===create_reading_passage===")
803
+ content = generate_reading_passage(df_string)
804
+ update_reading_passage(video_id, content)
805
+ return gr.update(value=content, interactive=False)
 
806
 
807
 
808
  def text_to_speech(video_id, text):
 
1575
  ai_msg = gr.Textbox(label="Message")
1576
  ai_send_button = gr.Button("Send", variant="primary")
1577
  with gr.Tab("文章模式"):
 
 
 
1578
  with gr.Row() as reading_passage_admin:
1579
+ reading_passage_edit_button = gr.Button("編輯", size="sm", variant="primary")
1580
+ reading_passage_update_button = gr.Button("更新", size="sm", variant="primary")
1581
+ reading_passage_delete_button = gr.Button("刪除", size="sm", variant="primary")
1582
+ reading_passage_create_button = gr.Button("建立", size="sm", variant="primary")
1583
+ with gr.Row():
1584
+ reading_passage = gr.Textbox(label="Reading Passage", lines=40, show_label=False)
1585
+ reading_passage_speak_button = gr.Button("Speak", visible=False)
1586
+ reading_passage_audio_output = gr.Audio(label="Audio Output", visible=False)
1587
+
1588
  with gr.Tab("重點摘要"):
1589
  df_summarise = gr.Textbox(container=True, show_copy_button=True, lines=40, show_label=False)
1590
  with gr.Tab("教學備課"):
 
1793
  # 当输入网页链接时触发
1794
  # web_link.change(process_web_link, inputs=web_link, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
1795
 
1796
+ # reading_passage event
1797
+ reading_passage_create_button.click(
1798
+ create_reading_passage,
1799
+ inputs=[video_id, df_string_output],
1800
+ outputs=[reading_passage]
1801
+ )
1802
+ reading_passage_delete_button.click(
1803
+ delete_reading_passage,
1804
+ inputs=[video_id],
1805
+ outputs=[reading_passage]
1806
+ )
1807
+ reading_passage_edit_button.click(
1808
+ enable_edit_mode,
1809
+ inputs=[],
1810
+ outputs=[reading_passage]
1811
+ )
1812
+ reading_passage_update_button.click(
1813
+ update_reading_passage,
1814
+ inputs=[video_id, reading_passage],
1815
+ outputs=[reading_passage]
1816
+ )
1817
+
1818
  # 教師版
1819
  worksheet_content_btn.click(
1820
  get_ai_content,