Home > Backend Development > Python Tutorial > Building a simple Python app to boost productivity using AI and the BotHub API

Building a simple Python app to boost productivity using AI and the BotHub API

Barbara Streisand
Release: 2024-11-15 12:40:03
Original
865 people have browsed it

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.

Setting up Dependencies and Project Configuration

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:

  • os and pathlib.Path: for working with environment variables and file paths;
  • dotenv: for loading sensitive data from a .env file;
  • fpdf: for generating PDF files;
  • openai: for interacting with the BotHub API.

Install these packages using pip:

pip install openai python-dotenv fpdf
Copy after login
Copy after login

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__)
Copy after login
Copy after login

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.

Building a simple Python app to boost productivity using AI and the BotHub API

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()
Copy after login
Copy after login

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'
)
Copy after login
Copy after login

Core Function for Audio Processing

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
Copy after login
Copy after login

Extracting Key Insights

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__)
Copy after login
Copy after login

Sentiment Analysis

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()
Copy after login
Copy after login

Report Generation

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'
)
Copy after login
Copy after login

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
Copy after login

Main Function

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
Copy after login

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template