Ever leave an important online meeting with tasks assigned and ideas discussed, but can't quite remember who said what? It almost feels like you need a dedicated note-taker to keep track of everything and generate reports. A better solution is to automate this with a script, which is exactly what we'll do.
In this tutorial, I'll show you how to create an application that automatically analyzes meetings and generates reports using the BotHub API (Whisper-1 Claude 3.5 Sonnet). This application will transcribe audio recordings, identify key information—who said what and which tasks were discussed—and compile a report, including a PDF version.
Before we begin, let's ensure we have all the necessary components installed, including Python and the required libraries for working with the API and audio files. We'll install the following:
Install these packages using pip:
pip install openai python-dotenv fpdf
We'll also use logging to track the program's execution and record any errors or important messages. We'll set up basic logging at the INFO level:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__)
To interact with the BotHub API, you first need to register on the BotHub platform and obtain an API key. This key is used to authenticate the requests we'll be sending.
For secure key storage, create a .env file in the root directory of your project and add your generated API key:
BOTHUB_API_KEY=your_api_key
Next, use the dotenv library's load_dotenv() function to load the data from the .env file, making it accessible to our code:
from dotenv import load_dotenv load_dotenv()
To work with the BotHub API, create an OpenAI instance, providing the api_key and base_url for the BotHub service. The API key is loaded from the environment using 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' )
This step involves creating a function that transcribes an audio file into text. We'll utilize the BotHub API and Whisper-1 for speech recognition. The audio file is opened in binary read mode (rb), and then we use the client.audio.transcriptions.create method to send the audio file to the server for processing. The response contains the text transcription. If the transcription is successful, a "Transcription complete" message is logged, and the text is returned for further processing. In case of an error, the error message is logged.
pip install openai python-dotenv fpdf
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.
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__)
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.
from dotenv import load_dotenv load_dotenv()
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.
import os from openai import OpenAI client = OpenAI( api_key=os.getenv('BOTHUB_API_KEY'), base_url='https://bothub.chat/api/v2/openai/v1' )
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).
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
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 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
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! ?
The above is the detailed content of Building a simple Python app to boost productivity using AI and the BotHub API. For more information, please follow other related articles on the PHP Chinese website!