|
| 1 | +from pathlib import Path |
| 2 | +from langchain_community.document_loaders import PyPDFLoader |
| 3 | +from openai import OpenAI |
| 4 | +import json |
| 5 | + |
| 6 | +import tiktoken |
| 7 | + |
| 8 | +# Initialize OpenAI client |
| 9 | +client = OpenAI() |
| 10 | + |
| 11 | + |
| 12 | +def extract_text_from_pdf(pdf_path): |
| 13 | + """Extract text from PDF using PyPDFLoader.""" |
| 14 | + loader = PyPDFLoader(str(pdf_path)) |
| 15 | + pages = loader.load() |
| 16 | + return "\n".join(page.page_content for page in pages) |
| 17 | + |
| 18 | + |
| 19 | +def get_diarization(): |
| 20 | + """Get the diarization data from the JSON file.""" |
| 21 | + diarization_path = Path( |
| 22 | + "./notebooks/experiments/minutes_diarization/regular_council_meeting___2025_02_26.diarized.json" |
| 23 | + ) |
| 24 | + if not diarization_path.exists(): |
| 25 | + raise FileNotFoundError("Diarization JSON file not found") |
| 26 | + |
| 27 | + with open(diarization_path, "r") as f: |
| 28 | + return json.load(f) |
| 29 | + |
| 30 | + |
| 31 | +def simplify_diarization(transcript_data): |
| 32 | + def format_timestamp(seconds: float) -> str: |
| 33 | + """Convert seconds to HH:MM:SS format""" |
| 34 | + hours = int(seconds // 3600) |
| 35 | + minutes = int((seconds % 3600) // 60) |
| 36 | + secs = int(seconds % 60) |
| 37 | + return f"{hours:02d}:{minutes:02d}:{secs:02d}" |
| 38 | + |
| 39 | + # Create formatted HTML output |
| 40 | + speaker_lines = ["Meeting Script - Combined by Speaker"] |
| 41 | + |
| 42 | + current_speaker = None |
| 43 | + current_text = [] |
| 44 | + current_start = None |
| 45 | + |
| 46 | + for segment in transcript_data["segments"]: |
| 47 | + if current_speaker != segment["speaker"]: |
| 48 | + # Output previous speaker's text |
| 49 | + if current_speaker: |
| 50 | + timestamp = format_timestamp(current_start) |
| 51 | + wrapped_text = " ".join(current_text) |
| 52 | + speaker_lines.append( |
| 53 | + f"[{timestamp}] {current_speaker}:\n{wrapped_text}\n" |
| 54 | + ) |
| 55 | + |
| 56 | + # Start new speaker |
| 57 | + current_speaker = segment["speaker"] |
| 58 | + current_text = [segment["text"].strip()] |
| 59 | + current_start = segment["start"] |
| 60 | + else: |
| 61 | + # Continue current speaker |
| 62 | + current_text.append(segment["text"].strip()) |
| 63 | + |
| 64 | + # Output final speaker |
| 65 | + if current_speaker: |
| 66 | + timestamp = format_timestamp(current_start) |
| 67 | + wrapped_text = " ".join(current_text) |
| 68 | + speaker_lines.append(f"[{timestamp}] {current_speaker}:\n{wrapped_text}") |
| 69 | + return "\n".join(speaker_lines) |
| 70 | + |
| 71 | + |
| 72 | +def match_speakers_with_chatgpt(minutes_text, diarization): |
| 73 | + """Use ChatGPT to match speakers from diarization with names from minutes.""" |
| 74 | + # Format diarization data for the prompt |
| 75 | + |
| 76 | + prompt = f"""I have a city council meeting minutes document and a diarization of the audio recording. |
| 77 | +The diarization has identified different speakers but doesn't know their names. |
| 78 | +Please analyze the minutes text and match the speakers from the diarization with the names mentioned in the minutes. |
| 79 | +
|
| 80 | +Minutes text: |
| 81 | +{minutes_text} |
| 82 | +
|
| 83 | +Diarization segments: |
| 84 | +{diarization} |
| 85 | +
|
| 86 | +For each speaker in the diarization, please identify who they are based on the minutes text. |
| 87 | +If you can't determine who they are, mark them as "Unknown". |
| 88 | +Format your response as a JSON object where the keys are the speaker numbers (e.g., "SPEAKER_00") |
| 89 | +and the values are the identified names or "Unknown". |
| 90 | +""" |
| 91 | + |
| 92 | + response = client.chat.completions.create( |
| 93 | + model="gpt-4o", |
| 94 | + messages=[ |
| 95 | + { |
| 96 | + "role": "system", |
| 97 | + "content": "You are a helpful assistant that analyzes meeting minutes and audio diarization to identify speakers.", |
| 98 | + }, |
| 99 | + {"role": "user", "content": prompt}, |
| 100 | + ], |
| 101 | + response_format={"type": "json_object"}, |
| 102 | + ) |
| 103 | + |
| 104 | + return json.loads(response.choices[0].message.content) |
| 105 | + |
| 106 | + |
| 107 | +def main(): |
| 108 | + minutes_path = Path( |
| 109 | + "./notebooks/experiments/minutes_diarization/test_data/25-173-2_25-173-2 2025-02-26 5PM Minutes.pdf" |
| 110 | + ) |
| 111 | + # Extract text from PDF |
| 112 | + minutes_text = extract_text_from_pdf(minutes_path) |
| 113 | + |
| 114 | + # Get diarization data |
| 115 | + diarization = get_diarization() |
| 116 | + |
| 117 | + simple_diarization = simplify_diarization(diarization) |
| 118 | + print(simple_diarization) |
| 119 | + |
| 120 | + encoding = tiktoken.encoding_for_model("gpt-4o-mini") |
| 121 | + |
| 122 | + print( |
| 123 | + f"Diarization segments length: {len(encoding.encode(str(simple_diarization)))}" |
| 124 | + ) |
| 125 | + print(f"Minutes text length: {len(encoding.encode(minutes_text))}") |
| 126 | + |
| 127 | + # Use ChatGPT to match speakers |
| 128 | + speaker_matches = match_speakers_with_chatgpt(minutes_text, simple_diarization) |
| 129 | + print(speaker_matches) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + main() |
0 commit comments