Transcription and analysis

Published

June 5, 2026

!pip install mistralai -q
!pip install python-dotenv
!pip install ddgs -q
/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pty.py:95: DeprecationWarning: This process (pid=74844) is multi-threaded, use of forkpty() may lead to deadlocks in the child.
  pid, fd = os.forkpty()
/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pty.py:95: DeprecationWarning: This process (pid=74844) is multi-threaded, use of forkpty() may lead to deadlocks in the child.
  pid, fd = os.forkpty()
Requirement already satisfied: python-dotenv in /Users/wattez/Documents/pro/citopia/.venv/lib/python3.13/site-packages (1.2.2)
/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pty.py:95: DeprecationWarning: This process (pid=74844) is multi-threaded, use of forkpty() may lead to deadlocks in the child.
  pid, fd = os.forkpty()
# Constants and productions

chemin_audio = "../_data/interview.m4a"
transcript_path = "../_data/transcript.txt"
summary_path = "../_data/summary.md"
graph_path = "../_data/knowledge_graph.json"
graph_path_inc = "../_data/knowledge_graph_inc.json"
from dotenv import load_dotenv
import os

path_venv = os.getenv("VIRTUAL_ENV")
idx_last_sep = path_venv.rfind('/')

load_dotenv(path_venv[:idx_last_sep+1] + "_environment.local")

mistral_api_key = os.getenv("MISTRAL_API_KEY")
from mistralai.client import Mistral

model = "mistral-medium-latest"
client = Mistral(api_key=mistral_api_key)
def get_transcription(chemin_audio):
    # Ouverture du fichier et lecture du contenu binaire
    with open(chemin_audio, "rb") as f:
        contenu_audio = f.read()

    # Appel à l'API de transcription avec le bon format
    reponse = client.audio.transcriptions.complete(
        file={
            "file_name": chemin_audio,  # Le nom du fichier est requis
            "content": contenu_audio    # Le fichier lui-même en binaire
        },
        model="voxtral-mini-latest"
    )

    return reponse.text
if not os.path.isfile(transcript_path):
    with open(transcript_path, "w", encoding="utf-8") as f:
        transcript = get_transcription(chemin_audio)
        f.write(transcript)
    print(f"Transcript saved to {transcript_path}")
else:
    print(f"Already transcripted")
    with open(transcript_path) as f: 
        transcript = f.read()
Already transcripted
def get_summarization(transcript):
    prompt = f"""
    You are an expert technical editor.

    Produce a comprehensive summary of the following transcript.

    Requirements:
    - Cover every important topic discussed.
    - Preserve key arguments, ideas, examples, decisions, and conclusions.
    - Remove filler, repetitions, hesitations, and off-topic remarks.
    - Organize the summary into clear sections with descriptive headings.
    - Keep all meaningful information while making the document significantly shorter.
    - Write in fluent English using Markdown.

    Transcript:

    {transcript}
    """

    response = client.chat.complete(
        model="mistral-large-latest",
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        temperature=0.2,
    )

    return response.choices[0].message.content
if not os.path.isfile(summary_path):
    with open(summary_path, "w", encoding="utf-8") as f:
        summary = get_summarization(transcript)
        f.write(summary)
    print(f"Summary saved to {summary_path}")
else:
    print(f"Already summarized")
    with open(summary_path) as f: 
        summary = f.read()
Already summarized
from pydantic import BaseModel, Field
from typing import List
import json


class KnowledgeNode(BaseModel):
    name: str = Field(description="Title of the concept.")
    description: str = Field(description="Self-contained explanation of the concept.")
    query: str = Field(
        description="Optimized search-engine query to deepen this concept. Only english keywords."
    )
    children: List["KnowledgeNode"] = Field(default_factory=list)


KnowledgeNode.model_rebuild()


def get_structured_summarization(transcript):

    prompt = f"""
You are an expert knowledge engineer.

Your task is to transform the following transcript into a hierarchical knowledge tree.

## Objective

Produce a global and exhaustive representation of the discussion as a tree of concepts.

The hierarchy should capture:
- every major topic,
- important subtopics,
- key ideas,
- arguments,
- examples,
- decisions,
- conclusions,
- relationships between concepts.

The graph should be exhaustive. Remove conversational filler while preserving all meaningful information.

## Rules

- Every node must contain:
    - name
    - description
    - query
- query must be a concise and effective web search query that would help someone learn more about that node (in english).
- children is an array of nodes.
- Create as many levels as necessary.
- Prefer a semantic hierarchy over a chronological one.
- Merge duplicate concepts.
- Descriptions should be self-contained.
- The root node summarizes the entire discussion.

Transcript:

{transcript}
"""

    chatResponse = client.chat.parse(
        model="mistral-large-latest",
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        response_format=KnowledgeNode,
        temperature=0.1,
    )

    return chatResponse.choices[0].message.parsed
if not os.path.isfile(graph_path):
    structured_summary = get_structured_summarization(transcript)
    structured_summary = structured_summary.model_dump()

    with open(graph_path, "w", encoding="utf-8") as f:
        json.dump(
            structured_summary,
            f,
            indent=2,
            ensure_ascii=False,
        )

    print(f"Graph saved to {graph_path}")
else:
    print(f"Already structured and summarized")
    with open(graph_path) as f:
        structured_summary = json.load(f)
Graph saved to ../_data/knowledge_graph.json
from ddgs import DDGS
import time

def get_structured_summarization_inc(structured_summary):
    # Initialisation du moteur de recherche
    ddgs = DDGS()

    def enrichir_avec_sources(noeud):
        query = noeud.get("query", "")
        
        print(f"🔍 Recherche de sources pour : {query}")
        
        # Construction de la requête (on cible les articles ou papiers de recherche)
        # L'opérateur "filetype:pdf" aide souvent à remonter des papiers scientifiques ou académiques
        requete = f"{query} (research OR article OR scientific)"
        
        try:
            # Exécution de la recherche (on limite à 3 résultats pour ne pas surcharger le graphe)
            resultats = ddgs.text(requete, max_results=3)
            
            sources = []
            if resultats:
                for res in resultats:
                    sources.append({
                        "title": res.get("title"),
                        "url": res.get("href")
                    })
            
            # Ajout des sources dans le noeud actuel
            noeud["sources"] = sources
            
            # Petite pause pour éviter de se faire bloquer par le moteur de recherche (Rate Limiting)
            time.sleep(1)
            
        except Exception as e:
            print(f"  ⚠️ Erreur lors de la recherche pour '{query}' : {e}")
            noeud["structured_summary"] = []

        # Appel récursif pour parcourir les enfants s'il y en a
        if "children" in noeud and isinstance(noeud["children"], list):
            for enfant in noeud["children"]:
                enrichir_avec_sources(enfant)

    print("--- Début de l'enrichissement ---")
    enrichir_avec_sources(structured_summary)
    print("--- Fin de l'enrichissement ---")

    return structured_summary
if not os.path.isfile(graph_path_inc):
    with open(graph_path_inc, "w", encoding="utf-8") as f:
        structured_summary_inc = get_structured_summarization_inc(structured_summary)
        json.dump(structured_summary_inc, f, indent=2, ensure_ascii=False)
    print(f"Increased graph saved to {graph_path_inc}")
else:
    print(f"Already structured, summarized and increased")
    with open(graph_path_inc) as f:
        structured_summary_inc = json.load(f)
--- Début de l'enrichissement ---
🔍 Recherche de sources pour : multi-agent systems in AI knowledge management, agentic AI architecture, knowledge graphs for reducing LLM hallucinations, orchestration of AI agents, dynamic knowledge management with markdown, AI agent roles and skills, Web3 decentralized knowledge bases, local-first AI models, open-source AI tools for knowledge verification
🔍 Recherche de sources pour : agentic AI fundamentals, context windows in AI agents, reducing hallucinations in LLMs with structured knowledge
🔍 Recherche de sources pour : AI agent context windows explained, working memory in AI systems, short-term memory for LLMs
🔍 Recherche de sources pour : how to reduce LLM hallucinations with knowledge graphs, structured knowledge for AI accuracy, error reduction in AI agents
🔍 Recherche de sources pour : knowledge graph architecture for AI, dynamic knowledge graphs, ingesting scientific articles into knowledge graphs
🔍 Recherche de sources pour : markdown for knowledge graph nodes, structured knowledge representation in AI, metadata in knowledge graphs
🔍 Recherche de sources pour : metadata in knowledge graphs, structuring knowledge graphs with metadata, article categorization in AI
🔍 Recherche de sources pour : evolving knowledge graphs, dynamic graph structures in AI, hypergraphs for knowledge representation
🔍 Recherche de sources pour : reingestion in knowledge graphs, updating AI knowledge bases, maintaining dynamic knowledge graphs
🔍 Recherche de sources pour : cross-reference index in knowledge graphs, hierarchical knowledge summarization, AI navigation of knowledge graphs
🔍 Recherche de sources pour : folder-based knowledge organization, practical knowledge graph management, markdown file hierarchy for AI
🔍 Recherche de sources pour : AI agent roles and skills, multi-agent system design, specialized AI agents for knowledge management
🔍 Recherche de sources pour : expert AI agents, domain-specific AI agents, AI agents as teachers
🔍 Recherche de sources pour : AI-generated educational content, expert agents for teaching, AI podcast generation
🔍 Recherche de sources pour : AI agents for knowledge management, maintaining knowledge graphs with AI, tracing research genealogy with AI
🔍 Recherche de sources pour : integrity guardian AI agent, knowledge graph consistency, detecting knowledge drift in AI systems
🔍 Recherche de sources pour : AI agent identity definition, interaction protocols for AI agents, designing AI agent roles
🔍 Recherche de sources pour : AI agent interaction protocols, knowledge graph navigation rules, AI agent constraints
🔍 Recherche de sources pour : collaborative knowledge updates with AI, dynamic knowledge co-writing, AI agent task assignment
🔍 Recherche de sources pour : orchestration of multi-agent systems, AI agent team coordination, optimizing AI agent productivity
🔍 Recherche de sources pour : AI agent synergy, complementary roles in multi-agent systems, avoiding redundancy in AI teams
🔍 Recherche de sources pour : agent duplication for optimization, stochastic AI agent techniques, improving AI performance with redundancy
🔍 Recherche de sources pour : cost management in AI multi-agent systems, token optimization for AI agents, balancing AI performance and resource usage
🔍 Recherche de sources pour : applications of multi-agent AI systems, AI agents for research productivity, practical uses of knowledge graphs
🔍 Recherche de sources pour : AI agents for research assistance, managing scientific literature with AI, AI for academic productivity
🔍 Recherche de sources pour : AI agents for workflow automation, productivity tools with multi-agent systems, automating knowledge management
🔍 Recherche de sources pour : novel use cases for AI agents, emerging applications of multi-agent systems, future of AI agent tasks
🔍 Recherche de sources pour : challenges in multi-agent AI systems, limitations of knowledge graphs, ethical considerations in AI agent design
🔍 Recherche de sources pour : knowledge drift in AI systems, maintaining knowledge graph integrity, consistency in dynamic knowledge bases
🔍 Recherche de sources pour : computational costs of AI agents, energy efficiency in multi-agent systems, optimizing token usage in AI
🔍 Recherche de sources pour : flexibility vs. rigidity in AI knowledge systems, balancing structure and adaptability in AI, trade-offs in knowledge graph design
🔍 Recherche de sources pour : local vs. cloud-based AI models, on-device AI for privacy, trade-offs in AI deployment
🔍 Recherche de sources pour : future of multi-agent AI systems, societal impact of AI agents, philosophical implications of agentic AI
🔍 Recherche de sources pour : user autonomy in AI systems, empowering users with AI agents, local-first AI for privacy
🔍 Recherche de sources pour : local-first AI models, on-device AI for privacy, reducing cloud dependency in AI
🔍 Recherche de sources pour : decentralized knowledge systems, Web3 for user autonomy, reducing data monopolies with AI
🔍 Recherche de sources pour : future of the web with AI agents, Web3 and decentralized knowledge, impact of AI on web evolution
🔍 Recherche de sources pour : websites as knowledge bases for AI, dynamic web interactions with AI agents, future of web design
🔍 Recherche de sources pour : Web3 for decentralized knowledge, user-controlled AI interactions, decentralized web with AI agents
🔍 Recherche de sources pour : dynamic web interfaces with AI, personalized AI agent interactions, customizable web experiences
🔍 Recherche de sources pour : open-source AI tools, community-driven AI development, verifiable AI systems
🔍 Recherche de sources pour : open-source AI tools for knowledge management, local AI model development, community-driven AI innovation
🔍 Recherche de sources pour : verification in AI systems, transparency in open-source AI, auditing AI agent behavior
🔍 Recherche de sources pour : ethical implications of AI agents, societal impact of multi-agent systems, risks of AI-driven knowledge management
🔍 Recherche de sources pour : human-AI collaboration with agents, augmenting human productivity with AI, AI as cognitive extensions
🔍 Recherche de sources pour : bias in AI agents, manipulation risks in multi-agent systems, ethical risks of AI-driven knowledge
🔍 Recherche de sources pour : regulation of AI agents, compliance in multi-agent systems, ethical AI development policies
🔍 Recherche de sources pour : CITOPIA multi-agent knowledge system, decentralized AI knowledge bases, case study of AI-driven knowledge management
🔍 Recherche de sources pour : goals of CITOPIA project, decentralized knowledge bases, user-controlled AI interactions
🔍 Recherche de sources pour : challenges in implementing CITOPIA, technical hurdles in multi-agent systems, balancing flexibility and rigidity in AI
🔍 Recherche de sources pour : future vision for CITOPIA, decentralized knowledge systems, local AI integration in CITOPIA
--- Fin de l'enrichissement ---
Increased graph saved to ../_data/knowledge_graph_inc.json