File size: 2,736 Bytes
881ea9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
🎵 Agent 5 (Acoustics) - PureVersation Linguistic Pipeline
-------------------------------------------------
This agent converts transcribed text and dialect metadata into
International Phonetic Alphabet (IPA) representations and
analyzes regional intonation patterns.
"""

import json

class AgentAcoustic:
    def __init__(self, llm_manager):
        self.llm_manager = llm_manager
        print("🎵 Agent 5 (Acoustic) Online: Phonetic Analysis Engine Ready.")

    def generate_phonetic_profile(self, text, dialect):
        """
        Uses the AI engine to generate an IPA representation and intonation rules
        for the given text based on the specific dialect.
        """
        if not self.llm_manager:
            return json.dumps({
                "ipa": "/unavailable/",
                "intonation": "LLM Manager not connected. Cannot perform acoustic analysis."
            })

        prompt = f"""
        You are an expert socio-linguist and phonetician.
        The user has spoken the following text in the following dialect/language:
        Text: "{text}"
        Dialect: "{dialect}"

        Please provide the following:
        1. "ipa": The most accurate International Phonetic Alphabet (IPA) transcription of how this exact phrase would be pronounced in this specific dialect.
        2. "intonation": A brief, 1-2 sentence description of the stress, rhythm, and intonation patterns typical for this phrase in this dialect.

        Output ONLY valid JSON in this exact format, with no markdown formatting or backticks:
        {{
            "ipa": "/həˈloʊ/",
            "intonation": "Stress falls on the second syllable with a rising pitch at the end."
        }}
        """

        try:
            # 🟢 FIX 1: Use the safe, isolated async wrapper to prevent UI freezing
            response_obj = self._safe_generate(prompt)
            
            # 🟢 FIX 2: GroqManager returns an object, so we must extract '.text'
            response_text = response_obj.text if hasattr(response_obj, 'text') else str(response_obj)
            
            # Clean response
            clean_res = response.replace("```json", "").replace("```", "").strip()
            
            # Verify it's valid JSON
            parsed = json.loads(clean_res)
            return json.dumps({
                "ipa": parsed.get("ipa", "Unknown"),
                "intonation": parsed.get("intonation", "Unknown")
            })
            
        except Exception as e:
            print(f"🎵 Acoustic Agent Error: {e}")
            return json.dumps({
                "ipa": f"/error parsing {dialect} phonetics/",
                "intonation": "Error occurred during generation."
            })