Spaces:
Sleeping
Sleeping
| """ | |
| Azure OpenAI client wrapper for LLM-based analysis. | |
| """ | |
| import os | |
| import json | |
| from openai import AzureOpenAI | |
| from dotenv import load_dotenv | |
| from prompts import FOOTBALL_ANALYSIS_SYSTEM_PROMPT, FOOTBALL_ANALYSIS_USER_PROMPT | |
| load_dotenv() | |
| AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY") | |
| AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT") | |
| AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION", "2024-12-01-preview") | |
| AZURE_OPENAI_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT", "gpt-5.2-chat") | |
| def get_openai_client(): | |
| """Create and return an Azure OpenAI client.""" | |
| return AzureOpenAI( | |
| api_key=AZURE_OPENAI_KEY, | |
| azure_endpoint=AZURE_OPENAI_ENDPOINT, | |
| api_version=AZURE_OPENAI_API_VERSION, | |
| ) | |
| def analyze_football_content(transcript: str) -> dict: | |
| """ | |
| Send transcribed text to Azure OpenAI for football content analysis. | |
| Args: | |
| transcript: The transcribed speech text. | |
| Returns: | |
| A dict with categorized football data (teams, leagues, sentiment, etc.) | |
| """ | |
| if not transcript or not transcript.strip(): | |
| return {"error": "ไม่มีข้อความให้วิเคราะห์"} | |
| client = get_openai_client() | |
| user_message = FOOTBALL_ANALYSIS_USER_PROMPT.format(transcript=transcript) | |
| try: | |
| response = client.chat.completions.create( | |
| model=AZURE_OPENAI_DEPLOYMENT, | |
| messages=[ | |
| {"role": "system", "content": FOOTBALL_ANALYSIS_SYSTEM_PROMPT}, | |
| {"role": "user", "content": user_message}, | |
| ], | |
| max_completion_tokens=4096, | |
| ) | |
| content = response.choices[0].message.content.strip() | |
| # Clean up markdown code fences if the model wraps JSON in ```json ... ``` | |
| if content.startswith("```"): | |
| content = content.split("\n", 1)[1] # Remove first line (```json) | |
| content = content.rsplit("```", 1)[0] # Remove last ``` | |
| content = content.strip() | |
| result = json.loads(content) | |
| return result | |
| except json.JSONDecodeError: | |
| return { | |
| "error": "LLM ตอบกลับมาไม่ใช่ JSON ที่ถูกต้อง", | |
| "raw_response": content, | |
| } | |
| except Exception as e: | |
| return {"error": f"เกิดข้อผิดพลาด: {str(e)}"} | |
| def format_analysis_result(result: dict) -> str: | |
| """Format the analysis result as a pretty JSON string for display.""" | |
| return json.dumps(result, ensure_ascii=False, indent=2) | |