Spaces:
Sleeping
Sleeping
File size: 2,658 Bytes
c5027b2 | 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 68 69 70 71 72 73 74 75 76 77 78 | """
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)
|