fudii0921 commited on
Commit
ae81ed4
·
verified ·
1 Parent(s): 66e012a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -4,6 +4,7 @@ from google.genai import types
4
  import os
5
  from dotenv import load_dotenv
6
  import tempfile
 
7
  #pip3 install pdfkit' と 'brew install wkhtmltopdf'を実行してください
8
 
9
  # Load environment variables
@@ -549,6 +550,34 @@ def change_input(prompt):
549
  return gr.update(visible=False)
550
 
551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
552
  # --- Python function for the Gradio app ---
553
  def generate_response(prompt):
554
  """
@@ -574,7 +603,14 @@ def generate_response(prompt):
574
 
575
  # ファイルパスを取得
576
  file_path = tmp_file.name
577
- return final_html,file_path
 
 
 
 
 
 
 
578
  except Exception as e:
579
  return f"An error occurred: {e}"
580
 
 
4
  import os
5
  from dotenv import load_dotenv
6
  import tempfile
7
+ import zipfile
8
  #pip3 install pdfkit' と 'brew install wkhtmltopdf'を実行してください
9
 
10
  # Load environment variables
 
550
  return gr.update(visible=False)
551
 
552
 
553
+ def create_zip_from_html(html_file_path, output_zip_path):
554
+ """
555
+ 指定されたHTMLファイルをZIPファイルに圧縮する関数。
556
+
557
+ Args:
558
+ html_file_path (str): 圧縮したいHTMLファイルのパス。
559
+ output_zip_path (str): 作成するZIPファイルのパス(例: 'output.zip')。
560
+ """
561
+
562
+ # HTMLファイルが存在するか確認
563
+ if not os.path.exists(html_file_path):
564
+ print(f"エラー: ファイル '{html_file_path}' が見つかりません。")
565
+ return
566
+
567
+ # 'w' (書き込み) モードでZIPファイルを開く
568
+ try:
569
+ with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
570
+ # ZIPファイルにHTMLファイルを追加する
571
+ # arcname=os.path.basename(html_file_path) で、
572
+ # ZIPファイル内のファイル名を元のファイル名(パスを含まない)にする
573
+ zipf.write(html_file_path, arcname=os.path.basename(html_file_path))
574
+
575
+ return f"✅ 成功: '{html_file_path}' が '{output_zip_path}' に圧縮されました。"
576
+
577
+ except Exception as e:
578
+ return f"❌ エラーが発生しました: {e}"
579
+
580
+
581
  # --- Python function for the Gradio app ---
582
  def generate_response(prompt):
583
  """
 
603
 
604
  # ファイルパスを取得
605
  file_path = tmp_file.name
606
+ #return final_html,file_path
607
+
608
+ zip_name = tmp_file.name.replace('.html','')
609
+ output_zip = zip_name + '.zip'
610
+ zip_file = create_zip_from_html(file_path, output_zip)
611
+
612
+ return final_html,zip_file
613
+
614
  except Exception as e:
615
  return f"An error occurred: {e}"
616