曾經在一次重要的線上會議上分配了任務並討論了想法,但不太記得誰說了什麼?幾乎感覺您需要一個專門的記錄員來追蹤一切並產生報告。更好的解決方案是使用腳本自動執行此操作,而這正是我們要做的。
在本教程中,我將向您展示如何建立一個使用 BotHub API (Whisper-1 Claude 3.5 Sonnet) 自動分析會議並產生報告的應用程式。該應用程式將轉錄錄音,識別關鍵資訊(誰說了什麼以及討論了哪些任務)並編譯報告,包括 PDF 版本。
在開始之前,讓我們確保已安裝所有必要的元件,包括 Python 以及處理 API 和音訊檔案所需的程式庫。我們將安裝以下內容:
使用 pip 安裝這些套件:
pip install openai python-dotenv fpdf
我們還將使用日誌記錄來追蹤程式的執行並記錄任何錯誤或重要訊息。我們將在 INFO 層級設定基本日誌記錄:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__)
要與BotHub API進行交互,您首先需要在BotHub平台上註冊並取得API金鑰。此密鑰用於驗證我們將發送的請求。
為了安全金鑰存儲,請在專案的根目錄中建立一個 .env 檔案並新增產生的 API 金鑰:
BOTHUB_API_KEY=your_api_key
接下來,使用 dotenv 庫的 load_dotenv() 函數從 .env 檔案載入數據,使其可供我們的程式碼存取:
from dotenv import load_dotenv load_dotenv()
要使用 BotHub API,請建立 OpenAI 實例,並為 BotHub 服務提供 api_key 和 base_url。 API 金鑰是使用 os.getenv('BOTHUB_API_KEY') 從環境中載入的:
import os from openai import OpenAI client = OpenAI( api_key=os.getenv('BOTHUB_API_KEY'), base_url='https://bothub.chat/api/v2/openai/v1' )
此步驟涉及建立一個將音訊檔案轉錄為文字的函數。我們將利用 BotHub API 和 Whisper-1 進行語音辨識。音訊檔案以二進位讀取模式(rb)打開,然後我們使用client.audio.transcriptions.create方法將音訊檔案傳送到伺服器進行處理。回應包含文字轉錄。如果轉錄成功,則會記錄「轉錄完成」訊息,並返回文字以供進一步處理。如果出現錯誤,則會記錄錯誤訊息。
def transcribe_audio(audio_file_path): try: with open(audio_file_path, "rb") as audio_file: transcript = client.audio.transcriptions.create( model="whisper-1", file=audio_file ) logger.info("Transcription complete.") return transcript.text except Exception as e: logger.error(f"Error during audio transcription: {e}") return None
After transcription, we have the text of our meeting. Now, our goal is to extract key insights, such as discussed tasks, decisions made, and any identified problems. Using client.chat.completions.create, we create a request to extract these key points, specifying the model, the meeting text, and the request in a messages format, where we instruct the model to identify the main tasks and problems. The function returns a string containing the key insights upon successful execution.
def extract_key_points(meeting_text): try: response = client.chat.completions.create( model="claude-3.5-sonnet", messages=[ { "role": "user", "content": f"Analyze the following meeting transcript and extract key insights, such as tasks, decisions, and discussed problems:\n\n{meeting_text}" } ] ) logger.info("Key insight extraction complete.") return response.choices[0].message.content except Exception as e: logger.error(f"Error extracting key insights: {e}") return None
We can also analyze the sentiment of the meeting text. Similar to extract_key_points, we use client.chat.completions.create to request a sentiment analysis of the provided text. The function returns the sentiment analysis result or an error message.
def analyze_sentiment(meeting_text): try: response = client.chat.completions.create( model="claude-3.5-sonnet", messages=[ { "role": "user", "content": f"Analyze the sentiment of the following text:\n\n{meeting_text}" } ] ) logger.info("Sentiment analysis complete.") return response.choices[0].message.content except Exception as e: logger.error(f"Error during sentiment analysis: {e}") return None
Once the key insights and sentiment analysis are complete, we need to compile them into a report. This report should be logical, coherent, and concise. We use client.chat.completions.create, providing a request with the key points and sentiment analysis, allowing the API to generate the final report text. The function returns the report text upon successful completion.
def generate_report(key_points, sentiment): try: content = f"Compile a meeting report considering the following key points and sentiment analysis:\n\nKey Points:\n{key_points}\n\nSentiment:\n{sentiment}" report = client.chat.completions.create( model="claude-3.5-sonnet", messages=[ { "role": "user", "content": content } ] ) logger.info("Report generation complete.") return report.choices[0].message.content except Exception as e: logger.error(f"Error generating report: {e}") return None
To facilitate easy storage and sharing, we save the report as a PDF. We use the FPDF library for PDF creation. We add a page, enable automatic text wrapping with multi_cell. After creating and populating the page with the report text, we save the report using output(file_path).
from fpdf import FPDF def save_report_as_pdf(report_text, file_path="meeting_report.pdf"): pdf = FPDF() pdf.add_page() pdf.set_auto_page_break(auto=True, margin=15) pdf.output(file_path) logger.info(f"Report saved as {file_path}")
This function orchestrates all the previous steps. It begins by transcribing the audio. If transcription fails, an error message is displayed, and the function terminates. Next, it calls the function to extract key insights. If an error occurs, it returns an appropriate message. Sentiment analysis is performed similarly, and if successful, the report text is generated. If all steps complete successfully, save_report_as_pdf is called to save the report in PDF format. Finally, the function returns the generated report text.
def analyze_meeting(audio_file_path): meeting_text = transcribe_audio(audio_file_path) if not meeting_text: return "Error during audio transcription." key_points = extract_key_points(meeting_text) if not key_points: return "Error extracting key insights." sentiment = analyze_sentiment(meeting_text) if not sentiment: return "Error during sentiment analysis." report_text = generate_report(key_points, sentiment) # Pass sentiment to report generation if not report_text: return "Error generating report." save_report_as_pdf(report_text) return report_text
In conclusion, we've built a small application that can boost your productivity and help you manage your time more effectively. We implemented a series of core functions, including audio transcription, key insight extraction, report generation, and saving the report in PDF format. This tool will help you keep track of important ideas and tasks, saving you time and effort.
Hope this helped! If so, any support is welcome, thanks for reading! ?
以上是使用 AI 和 BotHub API 建立簡單的 Python 應用程式以提高工作效率的詳細內容。更多資訊請關注PHP中文網其他相關文章!