SC999 commited on
Commit
fe86877
1 Parent(s): 687ff66

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +5 -4
  2. app.py +108 -0
  3. gitattributes +35 -0
  4. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
  title: Gemini PDF
3
- emoji: 🏃
4
  colorFrom: gray
5
- colorTo: pink
6
- sdk: streamlit
7
- sdk_version: 1.40.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Gemini PDF
3
+ emoji: 🏢
4
  colorFrom: gray
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 5.4.0
8
  app_file: app.py
9
  pinned: false
10
+ license: other
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import gradio as gr
4
+ import google.generativeai as genai
5
+
6
+ # 設定 Gemini API KEY
7
+ def configure_api_key(api_key):
8
+ genai.configure(api_key=api_key)
9
+ print("API Key 已配置成功")
10
+
11
+ # 上傳 PDF 並等待處理完成
12
+ def upload_and_process_pdf(file_path, mime_type="application/pdf"):
13
+ print("上傳 PDF 中...")
14
+ pdf_file = genai.upload_file(file_path, mime_type=mime_type)
15
+ print(f"PDF '{pdf_file.display_name}' 上傳成功,URI 為: {pdf_file.uri}")
16
+
17
+ # 等待 PDF 處理完成
18
+ while pdf_file.state.name == "PROCESSING":
19
+ print("等待 PDF 處理中...")
20
+ time.sleep(10)
21
+ pdf_file = genai.get_file(pdf_file.name)
22
+
23
+ if pdf_file.state.name == "FAILED":
24
+ raise ValueError("PDF 處理失敗。")
25
+
26
+ print(f"PDF 處理完成: {pdf_file.uri}")
27
+ return pdf_file
28
+
29
+ # 使用 PDF 的 URI 來生成描述
30
+ def generate_pdf_summary(api_key, pdf_file_path, prompt="仔細讀檔,彙整重點。繁體中文"):
31
+ configure_api_key(api_key)
32
+
33
+ # 上傳並處理 PDF
34
+ try:
35
+ pdf_file = upload_and_process_pdf(pdf_file_path)
36
+ except Exception as e:
37
+ return f"PDF 上傳或處理失敗:{e}", None
38
+
39
+ # 設定模型
40
+ model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-002")
41
+
42
+ # 發送 LLM 推理請求
43
+ try:
44
+ print("Making LLM inference request...")
45
+ response = model.generate_content(
46
+ [prompt, pdf_file],
47
+ request_options={"timeout": 600}
48
+ )
49
+ # 保存模型回應到文件
50
+ output_file_path = "/tmp/model_response.txt"
51
+ with open(output_file_path, "w") as f:
52
+ f.write(response.text)
53
+ return response.text, output_file_path
54
+ except Exception as e:
55
+ return f"與模型對話時發生錯誤:{e}", None
56
+
57
+ # Gradio 介面
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("### 上傳 PDF 並生成摘要(Gemini)")
60
+
61
+ # API Key 輸入
62
+ api_key_input = gr.Textbox(
63
+ label="輸入 API Key",
64
+ placeholder="請輸入您的 Gemini API Key",
65
+ type="password"
66
+ )
67
+
68
+ # PDF 上傳
69
+ pdf_input = gr.File(
70
+ label="上傳 PDF",
71
+ type="filepath",
72
+ )
73
+
74
+ # 描述提示輸入
75
+ prompt = gr.Textbox(
76
+ label="摘要提示",
77
+ placeholder="默認為 '仔細讀檔,彙整重點。繁體中文'",
78
+ lines=5
79
+ )
80
+
81
+ # 提交按鈕
82
+ submit_button = gr.Button("提交")
83
+
84
+ # 輸出文字框和下載文件按鈕
85
+ output_text = gr.Textbox(
86
+ label="模型回應",
87
+ placeholder="模型的回應將出現在這裡",
88
+ lines=10
89
+ )
90
+ download_button = gr.File(
91
+ label="下載模型回應",
92
+ interactive=False
93
+ )
94
+
95
+ # 設置提交按鈕的函數調用
96
+ def handle_generate_summary(api_key, pdf_file, prompt):
97
+ text_response, file_path = generate_pdf_summary(api_key, pdf_file, prompt)
98
+ return text_response, file_path
99
+
100
+ # 提交按鈕的處理
101
+ submit_button.click(
102
+ handle_generate_summary,
103
+ inputs=[api_key_input, pdf_input, prompt],
104
+ outputs=[output_text, download_button]
105
+ )
106
+
107
+ # 啟動 Gradio 介面
108
+ demo.launch()
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==3.1.4 # 根據您的需求使用最新的相容版本
2
+ google-generativeai # 調整為正確的套件名稱,若有不同
3
+ httpx==0.23.0 # 指定相容版本以防止依賴衝突
4
+ httpcore==0.15.0 # 指定相容版本以防止依賴衝突