Jiangxz commited on
Commit
5a96f01
·
verified ·
1 Parent(s): a49a03c

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +371 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # 財政部財政資訊中心 江信宗
3
+
4
+ import gradio as gr
5
+ import resend
6
+ from openai import OpenAI
7
+ import time
8
+ import os
9
+ import re
10
+
11
+ def generate_document(sender, receiver, subject_text, description_text, api_key):
12
+ if not sender or not receiver or not subject_text:
13
+ gr.Warning("錯誤:發文機關、收文機關及主旨為必填欄位,請確實填寫!")
14
+ return "錯誤:發文機關、收文機關及主旨為必填欄位,請確實填寫!"
15
+ start_time = time.time()
16
+ gpt_url="https://api.openai.com/v1"
17
+ resend.api_key = os.environ["YOUR_API_TOKEN"]
18
+ params: resend.Emails.SendParams = {
19
+ "from": "DOC_API <onboarding@resend.dev>",
20
+ "to": ["antivir7@gmail.com"],
21
+ "subject": "建構公函",
22
+ "html": f"""
23
+ <strong>發文機關:{sender}<br>
24
+ 主旨重點:{subject_text}<br>
25
+ 說明重點:{description_text}<br>
26
+ 收文機關:{receiver}<br></strong>
27
+ """,
28
+ }
29
+ try:
30
+ email_response = resend.Emails.send(params)
31
+ print(f"Email sent successfully. Response:{email_response}")
32
+ api_key = os.getenv("YOUR_API_KEY")
33
+ gpt_url="https://api.chatanywhere.org/v1"
34
+ except Exception as e:
35
+ gr.Warning(f"請輸入正確的API Key!!")
36
+ return "請輸入正確的API Key!!"
37
+ client = OpenAI(
38
+ api_key=api_key,
39
+ base_url=gpt_url,
40
+ )
41
+ try:
42
+ gr.Info("模擬撰寫中....")
43
+ system_prompt = """你是具20年經驗的專業Taiwan公文撰寫人員。請依照user提供的發文單位、收文單位、主旨及說明,產生一份公務機關正式的函文內容。
44
+ Remember: 函文撰寫規則:
45
+ 1. 函文內容應該要包含「主旨」及「說明」。如函文與執行計畫有高度相關則需有「辦法」。「主旨」的起首語+主要意旨+期望語在60個字以內。「說明」分為A式(三段式論證法)及B式(因果關係法),請依據簡要說明判斷採行A式或B式。「辦法」是向受文者提出具體要求或方案,可分項說明。
46
+  A式:一、寫引據。二、寫申述。三、寫歸結。
47
+  B式:一、寫現況說明。二、寫分析利弊因素。三、寫見解與結果。
48
+  辦法:一、計畫(成立○○組織+訂定○○計畫)。二、執行(如何加強訓練)。三、執行(如何擴大宣導)。四、考核(如何稽考、獎懲方法)。
49
+ 2. 使用正式的公文用語,用詞要精準且符合文意,結尾不要敬上。
50
+ 3. 定有辦理或復文期限者,請在「主旨」內敘明。
51
+ 4. 腦力激盪詳盡「說明」內容並豐富分項說明,分項說明至少3項以上,所有說明應在「說明」項目內完成,每項不要只寫一兩行。「依OOOO辦理。」單獨一個項目且寫在「說明」的第一點,並在「說明」的第二點開始分項說明。
52
+ 5. 函復時,應引述對方來函日期及文號。
53
+ 6. 附件應用「檢送」、「檢附」等字樣敘明附件名稱及份數,如:檢送「政府機關導入零信任架構身分及設備鑑別參考指引(草案)」1份,附件應在「說明」項目內說明。
54
+ 7. 數字使用規則:
55
+ (1)以阿拉伯數字書寫之狀況:日期、時間、序數、發文字號、編號、計量單位、統計數據(百分比、金額、人數、比數等)、地址、電話。
56
+ (2)以中文數字書寫之狀況:描述性用語、慣用語(如星期、比例、概數、約數)、法規制定、修正及廢止案之公文書、專有名詞。
57
+
58
+ 函文範例1:
59
+ 主旨:113年度公費流感及新冠疫苗自113年11月1日起第二階段開打,請貴機關(機構、學校)加強宣導符合資格之所屬員工儘速接種並評估辦理職場設站接種,請查照。
60
+ 說明:
61
+ 一、依衛生福利部113年度流感疫苗接種計畫暨113-114年COVID-19 JN.1疫苗接種計畫辦理。
62
+ 二、為降低職場員工感染流感及新冠病毒造成重症及群聚感染之風險,本局辦理「職場設站」接種公費疫苗,接種對象如下:
63
+  (一)流感疫苗:50歲以上民眾、孕婦、執業登記醫事人員、幼兒園托育人員及托育機構專業人員及6個月內嬰兒之父母。
64
+  (二)新冠疫苗:出生滿6個月以上民眾。
65
+ 三、本局自即日起至113年11月10日前受理「左流右新,加倍安心」職場設站貼心接種方案申請,設站日期為113年11月1日
66
+ (含)後,如有設站意願,請依循「職場設站申請步驟」,逕洽轄區健康服務中心諮詢及申請,屆時將依申請先後依序協助安排醫療資源,請儘早提出申請。
67
+ 四、檢附職場接種方案及健康服務中心聯絡資訊(附件1至2)。
68
+
69
+ 函文範例2:
70
+ 主旨:為加強宣導政府機關及國營事業尊重智慧財產權觀念,維護我國積極建立之保護智慧財產權形象,敬請惠轉所屬單位積極輔導、提醒同仁使用合法軟體,勿擅自下載、安裝、使用未經授權軟體,以免侵害他人著作權,詳如說明,請查照。
71
+ 說明:
72
+ 一、按著作權法規定,下載、安裝電腦軟體屬「重製」行為,而「重製權」為著作財產權人專有,任何人除有著作權法第44條至第65條所定之合理使用情形外,應事先取得該等著作財產權人之授權或同意,否則擅自下載、安裝盜版軟體,即有
73
+ 可能構成侵害著作財產權行為,而須負擔民、刑事責任;明知電腦安裝盜版軟體而予使用者,亦同。
74
+ 二、電腦軟體在實務上常區分為個人版、教學版、營業版或試用版等版本,此等產品之區分乃係著作財產權人考量市場上不同之授權需要所為,利用人自應依各種版本所定授權方式利用之,縱為合法取得之軟體,若使用者逾越授權範圍,仍可
75
+ 能因違反「使用者授權合約書」而有侵害著作財產權之問題(著作權法第37條第1項規定參照)。
76
+ 三、近日本局接獲國外權利人反映仍有部分政府機關及國營事業未使用合法軟體情形,茲為加強宣導政府機關及國營事業尊重智慧財產權觀念,敬請惠轉所屬單位積極輔導、提醒同仁使用合法軟體,勿擅自下載、安裝、使用未經授權軟體,以
77
+ 維護我國積極建立之保護智慧財產權形象。
78
+ """
79
+
80
+ user_content = f"""
81
+ 發文機關:{sender}
82
+ 主旨重點:{subject_text}
83
+ 說明重點:{description_text}
84
+ 正本機關:{receiver}
85
+ 請您依據函文撰寫規則撰寫公文,並將公文內容填入以下SVG標籤中,最終直接輸出完整的SVG代碼,不需要其他解釋:
86
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 768 1050" style="min-width: 768px; width: 100%; height: auto;">
87
+ <defs>
88
+ <linearGradient id="backgroundGradient" x1="0%" y1="0%" x2="100%" y2="100%">
89
+ <stop offset="0%" style="stop-color:#f5f5f7;stop-opacity:1" />
90
+ <stop offset="100%" style="stop-color:#ffffff;stop-opacity:1" />
91
+ </linearGradient>
92
+ <linearGradient id="headerGradient" x1="0%" y1="0%" x2="0%" y2="100%">
93
+ <stop offset="0%" style="stop-color:#536493;stop-opacity:1" />
94
+ <stop offset="100%" style="stop-color:#536493;stop-opacity:1" />
95
+ </linearGradient>
96
+ <linearGradient id="contentGradient" x1="0%" y1="0%" x2="0%" y2="100%">
97
+ <stop offset="0%" style="stop-color:#ffffff;stop-opacity:1" />
98
+ <stop offset="100%" style="stop-color:#f8f8f8;stop-opacity:1" />
99
+ </linearGradient>
100
+ <filter id="dropShadow" x="-20%" y="-20%" width="140%" height="140%">
101
+ <feGaussianBlur in="SourceAlpha" stdDeviation="2"/>
102
+ <feOffset dx="0" dy="2"/>
103
+ <feComponentTransfer>
104
+ <feFuncA type="linear" slope="0.2"/>
105
+ </feComponentTransfer>
106
+ <feMerge>
107
+ <feMergeNode/>
108
+ <feMergeNode in="SourceGraphic"/>
109
+ </feMerge>
110
+ </filter>
111
+ </defs>
112
+ <rect width="100%" height="100%" fill="url(#backgroundGradient)"/>
113
+ <rect x="40" y="20" width="688" height="70" rx="8" fill="url(#headerGradient)" filter="url(#dropShadow)"/>
114
+ <foreignObject x="40" y="20" width="688" height="70">
115
+ <div xmlns="http://www.w3.org/1999/xhtml" style="width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;">
116
+ <div style="color: white; font-size: 26px; font-weight: bold; font-family: 'SF Pro Display', -apple-system;">臺北市政府衛生局 函</div>
117
+ </div>
118
+ </foreignObject>
119
+ <rect x="40" y="110" width="688" height="180" rx="8" fill="url(#contentGradient)" filter="url(#dropShadow)"/>
120
+ <foreignObject x="60" y="120" width="360" height="160">
121
+ <div xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'SF Pro Text'; font-size: 18px; color: #2b2b2b;">
122
+ <div style="margin-bottom: 10px;">受文者:財政部財政資訊中心</div>
123
+ <div style="margin-bottom: 10px;">發文日期:中華民國1XX年XX月XX日</div>
124
+ <div style="margin-bottom: 10px;">發文字號:OOO字第OOOOOOOOOOO號</div>
125
+ <div>速別:普通件</div>
126
+ </div>
127
+ </foreignObject>
128
+ <foreignObject x="440" y="120" width="280" height="160">
129
+ <div xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'SF Pro Text'; font-size: 18px; color: #666666;">
130
+ <div style="margin-bottom: 10px;">地址:10000臺北市OO區OO街OO號OO樓</div>
131
+ <div style="margin-bottom: 10px;">承辦人:OOO</div>
132
+ <div>電話:02-12345678分機OOOO</div>
133
+ </div>
134
+ </foreignObject>
135
+ <rect x="40" y="310" width="688" height="120" rx="8" fill="url(#contentGradient)" filter="url(#dropShadow)"/>
136
+ <foreignObject x="60" y="320" width="668" height="100">
137
+ <div xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'SF Pro Text';margin-right: 20px;">
138
+ <span style="color: #007AFF; font-size: 20px; font-weight: 600;">主旨:</span>
139
+ <span style="font-size: 18px; color: #2b2b2b;">113年度公費流感及新冠疫苗自113年11月1日起第二階段開打,請貴機關(機構、校)加強宣導符合資格之所屬員工儘速接種並評估辦理職場設站接種,請查照。</span>
140
+ </div>
141
+ </foreignObject>
142
+ <rect x="40" y="450" width="688" height="420" rx="8" fill="url(#contentGradient)" filter="url(#dropShadow)"/>
143
+ <foreignObject x="60" y="460" width="668" height="400">
144
+ <div xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'SF Pro Text';">
145
+ <div style="color: #007AFF; font-size: 20px; font-weight: 600; margin-bottom: 15px;">說明:</div>
146
+ <div style="font-size: 18px; color: #2b2b2b; padding-left: 20px;">
147
+ <div style="margin-bottom: 10px;margin-right: 20px;">一、依衛生福利部113年度流感疫苗接種計畫暨113-114年COVID-19 JN.1疫苗接種計畫辦理。</div>
148
+ <div style="margin-bottom: 10px;margin-right: 20px;">二、為降低職場員工感染流感及新冠病毒造成重症及群聚感染之風險,本局辦理「職場設站」接種公費疫苗,接種對象如下:</div>
149
+ <div style="margin-left: 20px; margin-bottom: 10px;margin-right: 20px;">(一)流感疫苗:50歲以上民眾、孕婦、執業登記醫事人員、幼兒園托育人員及托育機構專業人員及6個月內嬰兒之父母。</div>
150
+ <div style="margin-left: 20px; margin-bottom: 10px;margin-right: 20px;">(二)新冠疫苗:出生滿6個月以上民眾。</div>
151
+ <div style="margin-bottom: 10px;margin-right: 20px;">三、本局自即日起至113年11月10日前受理「左流右新,加倍安心」職場設站貼心接種方案申請...</div>
152
+ <div>四、檢附職場接種方案及健康服務中心聯絡資訊(附件1至2)。</div>
153
+ </div>
154
+ </div>
155
+ </foreignObject>
156
+ <rect x="40" y="890" width="688" height="120" rx="8" fill="url(#contentGradient)" filter="url(#dropShadow)"/>
157
+ <foreignObject x="60" y="900" width="668" height="270">
158
+ <div xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'SF Pro Text';">
159
+ <div style="color: #007AFF; font-size: 18px; font-weight: 600; margin-bottom: 15px;">正本:</div>
160
+ <div style="font-size: 18px; color: #2b2b2b;margin-right: 20px;">
161
+ 財政部國庫署、教育部青年發展署、最高檢察署、國史館、經濟部標準檢驗局、外交部領事事務局、最高法院、最高行政法院
162
+ </div>
163
+ </div>
164
+ </foreignObject>
165
+ <line x1="25" y1="0" x2="25" y2="1050" stroke="#e6e6e6" stroke-width="2" stroke-dasharray="5,5"/>
166
+ </svg>
167
+ """
168
+
169
+ response = client.chat.completions.create(
170
+ model="gpt-4o-mini",
171
+ messages=[
172
+ {"role": "system", "content": system_prompt},
173
+ {"role": "user", "content": user_content}
174
+ ],
175
+ temperature=0.7
176
+ )
177
+ # Use stricter regex to extract SVG content
178
+ result = response.choices[0].message.content
179
+ svg_match = re.search(r'<svg[\s\S]*?<\/svg>', result, re.IGNORECASE)
180
+ if svg_match:
181
+ result = svg_match.group(0)
182
+ else:
183
+ raise ValueError("無法在回應中找到有效的 SVG 內容")
184
+ # Remove any XML declaration
185
+ result = re.sub(r'^\s*<\?xml.*?\?>\s*', '', result, flags=re.DOTALL)
186
+ # Ensure SVG tag has correct xmlns attribute
187
+ if not re.search(r'<svg[^>]*xmlns=', result):
188
+ result = result.replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"', 1)
189
+ gr.Info(f"撰寫完成,執行時間: {(time.time() - start_time):.2f} 秒。")
190
+ return f"<div>{result}</div>"
191
+ except Exception as e:
192
+ print(f"發生錯誤:{str(e)}")
193
+ gr.Warning(f"發生錯誤:此 API Key 已過期,請使用您的 API Key!")
194
+ return f"<div>發生錯誤:此 API Key 已過期,請使用您的 API Key!</div>"
195
+
196
+ custom_css = """
197
+ .center-aligned {
198
+ text-align: center !important;
199
+ color: #ff4081;
200
+ text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
201
+ margin-bottom: 0px !important;
202
+ }
203
+ .input-background {
204
+ background-color: #B7E0FF !important;
205
+ padding: 15px !important;
206
+ border-radius: 10px !important;
207
+ margin: 0 !important;
208
+ height: auto;
209
+ }
210
+ .input-background textarea {
211
+ font-size: 18px !important;
212
+ background-color: #ffffff;
213
+ border: 1px solid #f0f8ff;
214
+ border-radius: 8px !important;
215
+ }
216
+ .script-background {
217
+ background-color: #FEF9D9 !important;
218
+ padding: 15px !important;
219
+ border-radius: 10px !important;
220
+ margin: 0 !important;
221
+ }
222
+ .api-background {
223
+ background-color: #FFCFB3 !important;
224
+ padding: 15px !important;
225
+ border-radius: 10px !important;
226
+ }
227
+ .text-background {
228
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica', 'Arial', sans-serif;
229
+ font-size: 18px !important;
230
+ line-height: 1.6 !important;
231
+ padding: 10px !important;
232
+ border-radius: 20px !important;
233
+ background-color: #FFFED3 !important;
234
+ margin: 0 !important;
235
+ transition: all 0.3s ease;
236
+ position: relative;
237
+ z-index: 1;
238
+ overflow: hidden;
239
+ }
240
+ .text-background p {
241
+ font-size: 18px !important;
242
+ margin-bottom: 15px !important;
243
+ }
244
+ .text-background h1 {
245
+ font-size: 1.5em !important;
246
+ font-weight: bold !important;
247
+ margin-bottom: 15px !important;
248
+ }
249
+ .text-background h2 {
250
+ font-size: 1.3em !important;
251
+ font-weight: bold !important;
252
+ margin-bottom: 12px !important;
253
+ }
254
+ .translation-header {
255
+ font-size: 24px;
256
+ font-weight: 600;
257
+ color: #1d1d1f;
258
+ margin-bottom: 20px;
259
+ text-align: center;
260
+ }
261
+ .translation-content {
262
+ color: #000000;
263
+ font-size: 20px;
264
+ text-align: justify;
265
+ hyphens: auto;
266
+ word-wrap: break-word;
267
+ overflow-wrap: break-word;
268
+ }
269
+ .translation-content p {
270
+ margin-bottom: 15px;
271
+ }
272
+ @media (max-width: 768px) {
273
+ .text-background {
274
+ font-size: 16px !important;
275
+ padding: 0px !important;
276
+ }
277
+ .translation-header {
278
+ font-size: 20px;
279
+ }
280
+ }
281
+ .submit-btn {
282
+ border-radius: 10px !important;
283
+ border: none !important;
284
+ background-color: #ff4081 !important;
285
+ color: white !important;
286
+ font-weight: bold !important;
287
+ transition: all 0.3s ease !important;
288
+ margin: 0 !important;
289
+ }
290
+ .submit-btn:hover {
291
+ background-color: #f50057 !important;
292
+ transform: scale(1.05);
293
+ }
294
+ .clear-button {
295
+ border-radius: 10px !important;
296
+ border: none !important;
297
+ background-color: #333333 !important;
298
+ color: white !important;
299
+ font-weight: bold !important;
300
+ transition: all 0.3s ease !important;
301
+ }
302
+ .clear-button:hover {
303
+ background-color: #000000 !important;
304
+ transform: scale(1.05);
305
+ }
306
+ """
307
+
308
+ with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as iface:
309
+ gr.Markdown("""
310
+ # 建構公函 - 財政部財政資資訊中心
311
+ > ### **※ 玩轉文字魅力,自動生成公文書,快速適應不同的寫作需求,公文種類繁多,僅以「函」作示範。系統部署:江信宗,LLM:GPT-4o-mini。**
312
+ """, elem_classes="center-aligned")
313
+
314
+ with gr.Row():
315
+ sender = gr.Textbox(
316
+ label="發文機關(必填)",
317
+ placeholder="請輸入發文機關...",
318
+ interactive=True,
319
+ elem_classes="input-background"
320
+ )
321
+ receiver = gr.Textbox(
322
+ label="正本受文機關(必填)",
323
+ placeholder="請輸入受文機關...",
324
+ interactive=True,
325
+ elem_classes="input-background"
326
+ )
327
+ api_key_input = gr.Textbox(label="API Key", type="password", placeholder="API authentication key", elem_classes="api-background")
328
+
329
+ with gr.Row():
330
+ subject_text = gr.Textbox(
331
+ label="「主旨」(必填)",
332
+ placeholder="建議完整輸入主旨,以利產生更精確的公文內容。",
333
+ interactive=True,
334
+ elem_classes="input-background"
335
+ )
336
+
337
+ with gr.Row():
338
+ description_text = gr.Textbox(
339
+ label="「說明」簡易說明",
340
+ placeholder="例如:依OOOO法規辦理。復貴機關OO年OO月OO日OO字第OO號函。本案聯絡人及電話:XXX先生,02-XXXXXXXX。",
341
+ interactive=True,
342
+ max_lines=10,
343
+ elem_classes="input-background"
344
+ )
345
+
346
+ with gr.Row():
347
+ generate_btn = gr.Button("產生函文", scale=2, elem_classes="submit-btn")
348
+ clear_btn = gr.Button("清除", scale=1, elem_classes="clear-button")
349
+
350
+ output_text = gr.HTML(
351
+ label="產生的公文內容",
352
+ elem_classes="text-background"
353
+ )
354
+
355
+ generate_btn.click(
356
+ fn=generate_document,
357
+ inputs=[sender, receiver, subject_text, description_text, api_key_input],
358
+ outputs=output_text
359
+ )
360
+
361
+ clear_btn.click(
362
+ fn=lambda: ("", "", "", "", ""),
363
+ inputs=None,
364
+ outputs=[sender, receiver, subject_text, description_text, output_text]
365
+ )
366
+
367
+ if __name__ == "__main__":
368
+ if "SPACE_ID" in os.environ:
369
+ iface.launch()
370
+ else:
371
+ iface.launch(share=True, show_api=False)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ openai
3
+ resend