Spaces:
Running
Running
| """ | |
| 🎵 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." | |
| }) | |