diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0220b9e..fc74776 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,5 +31,5 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.11' - - run: pip install ruff + - run: pip install ruff==0.16.0 - run: ruff check nlp-service diff --git a/nlp-service/main.py b/nlp-service/main.py index f81f218..c1a6a95 100644 --- a/nlp-service/main.py +++ b/nlp-service/main.py @@ -1,10 +1,10 @@ import os + import httpx from dotenv import load_dotenv from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel -from typing import Optional load_dotenv(dotenv_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".env")) @@ -44,9 +44,9 @@ def detect_abuse(req: AbuseRequest): class ChatRequest(BaseModel): messages: list - system_instruction: Optional[str] = None - generation_config: Optional[dict] = None - lessons_context: Optional[str] = None + system_instruction: str | None = None + generation_config: dict | None = None + lessons_context: str | None = None class ChatResponse(BaseModel): reply: str @@ -86,7 +86,7 @@ async def call_gemini(prompt: str, system_instruction: str = "") -> str: print(f"Gemini API error: {res.status_code} - {res.text[:200]}") return "" - except Exception as e: + except (httpx.HTTPError, KeyError, ValueError, IndexError) as e: print(f"Gemini API exception: {e}") return "" diff --git a/nlp-service/test_service.py b/nlp-service/test_service.py index 2a37b5e..9ebba32 100644 --- a/nlp-service/test_service.py +++ b/nlp-service/test_service.py @@ -1,8 +1,9 @@ +import importlib.util import os import traceback -import importlib.util import spacy + nlp_spacy = spacy.load("xx_ent_wiki_sm") spec = importlib.util.spec_from_file_location("nlp_main", os.path.join(os.path.dirname(__file__), "main.py")) @@ -20,21 +21,21 @@ try: cleaned = mod.normalize_arabic(text) print(f"\nCleaned: {cleaned}") -except Exception as e: +except Exception as e: # noqa: BLE001 -- diagnostic script must survive any step failure to run the rest print(f"\nClean error: {e}") traceback.print_exc() try: s = mod.ml_sentiment(text) print(f"Sentiment: {s}") -except Exception as e: +except Exception as e: # noqa: BLE001 -- diagnostic script must survive any step failure to run the rest print(f"Sentiment error: {e}") traceback.print_exc() try: e = mod.spacy_entities(text) print(f"Entities: {e}") -except Exception as e: +except Exception as e: # noqa: BLE001 -- diagnostic script must survive any step failure to run the rest print(f"Entities error: {e}") traceback.print_exc()