unausagi commited on
Commit
838c33c
·
verified ·
1 Parent(s): d8f3a0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import chardet
3
  import configparser
4
  from docx import Document
5
  import gradio as gr
@@ -8,39 +7,35 @@ from langchain_openai import AzureChatOpenAI
8
  # 讀取 config.ini 文件
9
  def load_config(config_file_path):
10
  config = configparser.ConfigParser()
11
-
12
- # 使用配置檔案路徑直接讀取文件
13
  try:
14
- config.read(config_file_path) # 使用檔案路徑來讀取
15
- if not config.sections(): # 檢查是否包含區段標題
16
  raise ValueError("config.ini 檔案缺少區段標題,請確認檔案格式正確。")
17
  except Exception as e:
18
  raise ValueError(f"讀取 config.ini 檔案時發生錯誤:{e}")
19
-
20
  return config
21
 
22
  # 設定 Azure OpenAI 配置
23
  def create_openai_client(config):
24
- return AzureChatOpenAI(
25
- openai_api_version=config["AzureOpenAI"]["VERSION"],
26
- azure_deployment=config["AzureOpenAI"]["DEPLOYMENT_NAME_GPT4o"],
27
- azure_endpoint=config["AzureOpenAI"]["BASE"],
28
- api_key=config["AzureOpenAI"]["KEY"],
29
- temperature=0.2,
30
- )
 
 
 
31
 
32
  def read_docx(file):
33
  """Reads the content of a .docx file and returns the text."""
34
  doc = Document(file)
35
- text = "\n".join([para.text for para in doc.paragraphs])
36
- return text
37
 
38
  def generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gpt4o):
39
  """Generates a monthly report based on weekly reports and the last month's report."""
40
- weekly_reports = []
41
- for file in weekly_reports_files:
42
- weekly_reports.append(read_docx(file))
43
-
44
  last_month_report = read_docx(last_month_report_file) if last_month_report_file else ""
45
 
46
  prompt = f"""請根據以下資訊生成本月月報:
@@ -54,23 +49,22 @@ def generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gp
54
  prompt += """
55
  請將每個專案的進度內容進行詳細的描述,並參考上個月月報的格式,製作出本月的月報,並且描述下個月預計事項。
56
  """
57
-
58
- # 使用 langchain AzureChatOpenAI 生成月報
59
- response = llm_gpt4o.invoke([("system", "You are a professional assistant."), ("human", prompt)])
60
- return response.content
 
61
 
62
  def process_reports(weekly_reports_files, last_month_report_file, config_file):
63
  try:
64
- # 將 config_file 參數傳遞為檔案路徑
65
  config_file_path = config_file.name
66
- config = load_config(config_file_path) # 讀取上傳的 config.ini
67
- llm_gpt4o = create_openai_client(config) # 創建 AzureOpenAI 客戶端
68
 
69
  if not weekly_reports_files or not last_month_report_file:
70
  return "請上傳所有必需的檔案(週報和上個月月報)。"
71
 
72
- monthly_report = generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gpt4o)
73
- return monthly_report
74
  except Exception as e:
75
  return f"生成月報時發生錯誤:{str(e)}"
76
 
@@ -82,11 +76,9 @@ with gr.Blocks() as demo:
82
  with gr.Row():
83
  weekly_reports_input = gr.File(label="上傳本月週報(多個 .docx 檔案)", file_types=[".docx"], file_count="multiple")
84
  last_month_report_input = gr.File(label="上傳上個月月報(單個 .docx 檔案)", file_types=[".docx"])
85
-
86
- config_file_input = gr.File(label="上傳 config.ini 檔案")
87
 
88
  output = gr.Textbox(label="生成的月報")
89
-
90
  submit_button = gr.Button("生成月報")
91
  submit_button.click(process_reports, inputs=[weekly_reports_input, last_month_report_input, config_file_input], outputs=output)
92
 
 
1
  import os
 
2
  import configparser
3
  from docx import Document
4
  import gradio as gr
 
7
  # 讀取 config.ini 文件
8
  def load_config(config_file_path):
9
  config = configparser.ConfigParser()
 
 
10
  try:
11
+ config.read(config_file_path)
12
+ if not config.sections():
13
  raise ValueError("config.ini 檔案缺少區段標題,請確認檔案格式正確。")
14
  except Exception as e:
15
  raise ValueError(f"讀取 config.ini 檔案時發生錯誤:{e}")
 
16
  return config
17
 
18
  # 設定 Azure OpenAI 配置
19
  def create_openai_client(config):
20
+ try:
21
+ return AzureChatOpenAI(
22
+ openai_api_version=config["AzureOpenAI"]["VERSION"],
23
+ azure_deployment=config["AzureOpenAI"]["DEPLOYMENT_NAME_GPT4o"],
24
+ azure_endpoint=config["AzureOpenAI"]["BASE"],
25
+ api_key=config["AzureOpenAI"]["KEY"],
26
+ temperature=0.2,
27
+ )
28
+ except KeyError as e:
29
+ raise ValueError(f"config.ini 檔案缺少必要的欄位:{e}")
30
 
31
  def read_docx(file):
32
  """Reads the content of a .docx file and returns the text."""
33
  doc = Document(file)
34
+ return "\n".join([para.text for para in doc.paragraphs])
 
35
 
36
  def generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gpt4o):
37
  """Generates a monthly report based on weekly reports and the last month's report."""
38
+ weekly_reports = [read_docx(file) for file in weekly_reports_files]
 
 
 
39
  last_month_report = read_docx(last_month_report_file) if last_month_report_file else ""
40
 
41
  prompt = f"""請根據以下資訊生成本月月報:
 
49
  prompt += """
50
  請將每個專案的進度內容進行詳細的描述,並參考上個月月報的格式,製作出本月的月報,並且描述下個月預計事項。
51
  """
52
+ try:
53
+ response = llm_gpt4o.invoke([("system", "You are a professional assistant."), ("human", prompt)])
54
+ return response.content
55
+ except Exception as e:
56
+ raise ValueError(f"生成月報時發生錯誤:{str(e)}")
57
 
58
  def process_reports(weekly_reports_files, last_month_report_file, config_file):
59
  try:
 
60
  config_file_path = config_file.name
61
+ config = load_config(config_file_path)
62
+ llm_gpt4o = create_openai_client(config)
63
 
64
  if not weekly_reports_files or not last_month_report_file:
65
  return "請上傳所有必需的檔案(週報和上個月月報)。"
66
 
67
+ return generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gpt4o)
 
68
  except Exception as e:
69
  return f"生成月報時發生錯誤:{str(e)}"
70
 
 
76
  with gr.Row():
77
  weekly_reports_input = gr.File(label="上傳本月週報(多個 .docx 檔案)", file_types=[".docx"], file_count="multiple")
78
  last_month_report_input = gr.File(label="上傳上個月月報(單個 .docx 檔案)", file_types=[".docx"])
79
+ config_file_input = gr.File(label="上傳 config.ini 檔案")
 
80
 
81
  output = gr.Textbox(label="生成的月報")
 
82
  submit_button = gr.Button("生成月報")
83
  submit_button.click(process_reports, inputs=[weekly_reports_input, last_month_report_input, config_file_input], outputs=output)
84