Spaces:
Sleeping
Sleeping
| import os | |
| import pandas as pd | |
| from typing import List, Dict | |
| from pathlib import Path | |
| # Prefer HF router via OpenAI-compatible client. Use env `HF_TOKEN`. | |
| # HF_TOKEN loaded lazily to allow dotenv loading after import | |
| def get_hf_token(): | |
| return os.environ.get('HF_TOKEN') | |
| def rule_summary(row: pd.Series) -> str: | |
| parts = [] | |
| if pd.notna(row.get('EventType')): | |
| parts.append(f"ประเภท: {row['EventType']}") | |
| if pd.notna(row.get('CauseType')): | |
| parts.append(f"สาเหตุ: {row['CauseType']}") | |
| if pd.notna(row.get('OutageDateTime')): | |
| parts.append(f"เวลาเริ่ม: {row['OutageDateTime']}") | |
| if pd.notna(row.get('AffectedCustomer')): | |
| parts.append(f"ลูกค้าได้รับผลกระทบ: {row['AffectedCustomer']}") | |
| if pd.notna(row.get('Load(MW)')): | |
| parts.append(f"โหลด: {row['Load(MW)']}") | |
| return ' | '.join(parts) if parts else '' | |
| def openai_summary(text: str, verbosity: str = 'brief', model: str = 'meta-llama/Llama-3.1-8B-Instruct:novita') -> str: | |
| HF_TOKEN = get_hf_token() | |
| if not HF_TOKEN: | |
| return None | |
| try: | |
| # Import here to avoid requiring OpenAI client unless HF_TOKEN set | |
| from openai import OpenAI | |
| client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN) | |
| if verbosity == 'analyze': | |
| instruction = 'วิเคราะห์สาเหตุไฟฟ้าจากข้อมูลนี้ สรุปไม่เกิน 3-4 บรรทัด (ไทย) ระบุสาเหตุทางเทคนิค ผลกระทบต่อลูกค้าและระบบ และช่วงเวลา:' | |
| elif verbosity == 'recommend': | |
| instruction = 'วิเคราะห์สาเหตุไฟฟ้าจากข้อมูลนี้ พร้อมแนะนำการแก้ไข สรุปไม่เกิน 3-4 บรรทัด (ไทย) ระบุสาเหตุทางเทคนิค ผลกระทบต่อลูกค้าและระบบ ช่วงเวลาและข้อเสนอแนะในการป้องกัน:' | |
| prompt = f"{instruction}\n\n{text}\n\nสรุป:" | |
| completion = client.chat.completions.create( | |
| model=model, | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=1000, | |
| ) | |
| # Extract text from response | |
| choice = completion.choices[0] | |
| msg = choice.message | |
| content = msg.content | |
| return content.strip() if content else None | |
| except Exception: | |
| return None | |
| def summarize_events(df: pd.DataFrame, use_hf: bool = False, verbosity: str = 'brief', model: str = 'meta-llama/Llama-3.1-8B-Instruct:novita') -> List[Dict]: | |
| rows = [] | |
| for _, r in df.iterrows(): | |
| text_fields = [] | |
| for col in df.columns: | |
| if pd.notna(r.get(col)) and str(r.get(col)).strip(): | |
| text_fields.append(f"{col}: {r[col]}") | |
| long_text = '\n'.join(text_fields) | |
| print(f"Debug: EventNumber={r.get('EventNumber','')}, long_text='{long_text}'") | |
| summary = None | |
| if use_hf and get_hf_token() and long_text: | |
| # prefer HF router | |
| summary = openai_summary(long_text, verbosity=verbosity, model=model) | |
| if not summary: | |
| summary = rule_summary(r) | |
| rows.append({ | |
| 'EventNumber': r.get('EventNumber',''), | |
| 'OutageDateTime': r.get('OutageDateTime',''), | |
| 'Summary': summary, | |
| }) | |
| return rows | |