Spaces:
Sleeping
Sleeping
import os | |
import configparser | |
from docx import Document | |
import gradio as gr | |
from langchain_openai import AzureChatOpenAI | |
print(gr.__version__) | |
# 讀取 config.ini 文件 | |
def load_config(config_file_path): | |
config = configparser.ConfigParser() | |
try: | |
config.read(config_file_path) | |
if not config.sections(): | |
raise ValueError("config.ini 檔案缺少區段標題,請確認檔案格式正確。") | |
except Exception as e: | |
raise ValueError(f"讀取 config.ini 檔案時發生錯誤:{e}") | |
return config | |
# 設定 Azure OpenAI 配置 | |
def create_openai_client(config): | |
try: | |
return AzureChatOpenAI( | |
openai_api_version=config["AzureOpenAI"]["VERSION"], | |
azure_deployment=config["AzureOpenAI"]["DEPLOYMENT_NAME_GPT4o"], | |
azure_endpoint=config["AzureOpenAI"]["BASE"], | |
api_key=config["AzureOpenAI"]["KEY"], | |
temperature=0.2, | |
) | |
except KeyError as e: | |
raise ValueError(f"config.ini 檔案缺少必要的欄位:{e}") | |
def read_docx(file): | |
"""Reads the content of a .docx file and returns the text.""" | |
doc = Document(file) | |
return "\n".join([para.text for para in doc.paragraphs]) | |
def generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gpt4o): | |
"""Generates a monthly report based on weekly reports and the last month's report.""" | |
weekly_reports = [read_docx(file) for file in weekly_reports_files] | |
last_month_report = read_docx(last_month_report_file) if last_month_report_file else "" | |
prompt = f"""請根據以下資訊生成本月月報: | |
上個月月報: | |
{last_month_report} | |
本月各週報: | |
""" | |
for report in weekly_reports: | |
prompt += f"{report}\n---\n" | |
prompt += """ | |
你是一個文件總結的專業秘書, | |
根據本月各週報中的每個專案進行詳細的描述, | |
進度內容需要描述發生了甚麼問題,以及後續處理方式與結果, | |
並參考上個月月報的格式,製作出本月的月報,並且描述下個月預計事項。 | |
""" | |
try: | |
response = llm_gpt4o.invoke([("system", "You are a professional assistant."), ("human", prompt)]) | |
return response.content | |
except Exception as e: | |
raise ValueError(f"生成月報時發生錯誤:{str(e)}") | |
def process_reports(weekly_reports_files, last_month_report_file, config_file): | |
try: | |
config_file_path = config_file.name | |
config = load_config(config_file_path) | |
llm_gpt4o = create_openai_client(config) | |
if not weekly_reports_files or not last_month_report_file: | |
return "請上傳所有必需的檔案(週報和上個月月報)。" | |
return generate_monthly_report(weekly_reports_files, last_month_report_file, llm_gpt4o) | |
except Exception as e: | |
return f"生成月報時發生錯誤:{str(e)}" | |
# Gradio Blocks interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# 月報生成工具") | |
gr.Markdown("上傳週報和上個月的月報,並生成本月的月報。") | |
with gr.Row(): | |
weekly_reports_input = gr.File(label="上傳本月週報(多個 .docx 檔案)", file_types=[".docx"], file_count="multiple") | |
last_month_report_input = gr.File(label="上傳上個月月報(單個 .docx 檔案)", file_types=[".docx"]) | |
config_file_input = gr.File(label="上傳 config.ini 檔案") | |
output = gr.Textbox(label="生成的月報") | |
submit_button = gr.Button("生成月報") | |
submit_button.click(process_reports, inputs=[weekly_reports_input, last_month_report_input, config_file_input], outputs=output) | |
demo.launch() | |