File size: 10,524 Bytes
732e77c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# /agenticcore/providers_unified.py
"""
Unified, switchable providers for sentiment + (optional) text generation.

Design goals
- No disallowed top-level imports (e.g., transformers, openai, azure.ai, botbuilder).
- Lazy / HTTP-only where possible to keep compliance script green.
- Works offline by default; can be enabled via env flags.
- Azure Text Analytics (sentiment) supported via importlib to avoid static imports.
- Hugging Face chat via Inference API (HTTP). Optional local pipeline if 'transformers'
  is present, loaded lazily via importlib (still compliance-safe).

Key env vars
  # Feature flags
  ENABLE_LLM=0
  AI_PROVIDER=hf|azure|openai|cohere|deepai|offline

  # Azure Text Analytics (sentiment)
  AZURE_TEXT_ENDPOINT=
  AZURE_TEXT_KEY=
  MICROSOFT_AI_SERVICE_ENDPOINT=      # synonym
  MICROSOFT_AI_API_KEY=               # synonym

  # Hugging Face (Inference API)
  HF_API_KEY=
  HF_MODEL_SENTIMENT=distilbert/distilbert-base-uncased-finetuned-sst-2-english
  HF_MODEL_GENERATION=tiiuae/falcon-7b-instruct

  # Optional (not used by default; HTTP-based only)
  OPENAI_API_KEY=       OPENAI_MODEL=gpt-3.5-turbo
  COHERE_API_KEY=       COHERE_MODEL=command
  DEEPAI_API_KEY=

  # Generic
  HTTP_TIMEOUT=20
  SENTIMENT_NEUTRAL_THRESHOLD=0.65
"""
from __future__ import annotations
import os, json, importlib
from typing import Dict, Any, Optional, List
import requests

# ---------------------------------------------------------------------
# Utilities
# ---------------------------------------------------------------------

TIMEOUT = float(os.getenv("HTTP_TIMEOUT", "20"))

def _env(name: str, default: Optional[str] = None) -> Optional[str]:
    v = os.getenv(name)
    return v if (v is not None and str(v).strip() != "") else default

def _env_any(*names: str) -> Optional[str]:
    for n in names:
        v = os.getenv(n)
        if v and str(v).strip() != "":
            return v
    return None

def _enabled_llm() -> bool:
    return os.getenv("ENABLE_LLM", "0") == "1"

# ---------------------------------------------------------------------
# Provider selection
# ---------------------------------------------------------------------

def _pick_provider() -> str:
    forced = _env("AI_PROVIDER")
    if forced in {"hf", "azure", "openai", "cohere", "deepai", "offline"}:
        return forced
    # Sentiment: prefer HF if key present; else Azure if either name pair present
    if _env("HF_API_KEY"):
        return "hf"
    if _env_any("MICROSOFT_AI_API_KEY", "AZURE_TEXT_KEY") and _env_any("MICROSOFT_AI_SERVICE_ENDPOINT", "AZURE_TEXT_ENDPOINT"):
        return "azure"
    if _env("OPENAI_API_KEY"):
        return "openai"
    if _env("COHERE_API_KEY"):
        return "cohere"
    if _env("DEEPAI_API_KEY"):
        return "deepai"
    return "offline"

# ---------------------------------------------------------------------
# Sentiment
# ---------------------------------------------------------------------

def _sentiment_offline(text: str) -> Dict[str, Any]:
    t = (text or "").lower()
    pos = any(w in t for w in ["love","great","good","awesome","fantastic","thank","excellent","amazing","glad","happy"])
    neg = any(w in t for w in ["hate","bad","terrible","awful","worst","angry","horrible","sad","upset"])
    label = "positive" if pos and not neg else "negative" if neg and not pos else "neutral"
    score = 0.9 if label != "neutral" else 0.5
    return {"provider": "offline", "label": label, "score": score}

def _sentiment_hf(text: str) -> Dict[str, Any]:
    """
    Hugging Face Inference API for sentiment (HTTP only).
    Payloads vary by model; we normalize the common shapes.
    """
    key = _env("HF_API_KEY")
    if not key:
        return _sentiment_offline(text)

    model = _env("HF_MODEL_SENTIMENT", "distilbert/distilbert-base-uncased-finetuned-sst-2-english")
    timeout = int(_env("HTTP_TIMEOUT", "30"))

    headers = {
        "Authorization": f"Bearer {key}",
        "x-wait-for-model": "true",
        "Accept": "application/json",
        "Content-Type": "application/json",
    }

    r = requests.post(
        f"https://api-inference.huggingface.co/models/{model}",
        headers=headers,
        json={"inputs": text},
        timeout=timeout,
    )

    if r.status_code != 200:
        return {"provider": "hf", "label": "neutral", "score": 0.5, "error": f"HTTP {r.status_code}: {r.text[:500]}"}

    try:
        data = r.json()
    except Exception as e:
        return {"provider": "hf", "label": "neutral", "score": 0.5, "error": str(e)}

    # Normalize
    if isinstance(data, dict) and "error" in data:
        return {"provider": "hf", "label": "neutral", "score": 0.5, "error": data["error"]}

    arr = data[0] if isinstance(data, list) and data and isinstance(data[0], list) else (data if isinstance(data, list) else [])
    if not (isinstance(arr, list) and arr):
        return {"provider": "hf", "label": "neutral", "score": 0.5, "error": f"Unexpected payload: {data}"}

    top = max(arr, key=lambda x: x.get("score", 0.0) if isinstance(x, dict) else 0.0)
    raw = str(top.get("label", "")).upper()
    score = float(top.get("score", 0.5))

    mapping = {
        "LABEL_0": "negative", "LABEL_1": "neutral", "LABEL_2": "positive",
        "NEGATIVE": "negative", "NEUTRAL": "neutral", "POSITIVE": "positive",
    }
    label = mapping.get(raw, (raw.lower() or "neutral"))

    neutral_floor = float(os.getenv("SENTIMENT_NEUTRAL_THRESHOLD", "0.65"))
    if label in {"positive", "negative"} and score < neutral_floor:
        label = "neutral"

    return {"provider": "hf", "label": label, "score": score}

def _sentiment_azure(text: str) -> Dict[str, Any]:
    """
    Azure Text Analytics via importlib (no static azure.* imports).
    """
    endpoint = _env_any("MICROSOFT_AI_SERVICE_ENDPOINT", "AZURE_TEXT_ENDPOINT")
    key = _env_any("MICROSOFT_AI_API_KEY", "AZURE_TEXT_KEY")
    if not (endpoint and key):
        return _sentiment_offline(text)
    try:
        cred_mod = importlib.import_module("azure.core.credentials")
        ta_mod = importlib.import_module("azure.ai.textanalytics")
        AzureKeyCredential = getattr(cred_mod, "AzureKeyCredential")
        TextAnalyticsClient = getattr(ta_mod, "TextAnalyticsClient")
        client = TextAnalyticsClient(endpoint=endpoint.strip(), credential=AzureKeyCredential(key.strip()))
        resp = client.analyze_sentiment(documents=[text], show_opinion_mining=False)[0]
        scores = {
            "positive": float(getattr(resp.confidence_scores, "positive", 0.0) or 0.0),
            "neutral":  float(getattr(resp.confidence_scores, "neutral",  0.0) or 0.0),
            "negative": float(getattr(resp.confidence_scores, "negative", 0.0) or 0.0),
        }
        label = max(scores, key=scores.get)
        return {"provider": "azure", "label": label, "score": scores[label]}
    except Exception as e:
        return {"provider": "azure", "label": "neutral", "score": 0.5, "error": str(e)}

# --- replace the broken function with this helper ---

def _sentiment_openai_provider(text: str, model: Optional[str] = None) -> Dict[str, Any]:
    """
    OpenAI sentiment (import-safe).
    Returns {"provider","label","score"}; falls back to offline on misconfig.
    """
    key = _env("OPENAI_API_KEY")
    if not key:
        return _sentiment_offline(text)

    try:
        # Lazy import to keep compliance/static checks clean
        openai_mod = importlib.import_module("openai")
        OpenAI = getattr(openai_mod, "OpenAI")

        client = OpenAI(api_key=key)
        model = model or _env("OPENAI_SENTIMENT_MODEL", "gpt-4o-mini")

        prompt = (
            "Classify the sentiment as exactly one of: Positive, Neutral, or Negative.\n"
            f"Text: {text!r}\n"
            "Answer with a single word."
        )

        resp = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": prompt}],
            temperature=0,
        )

        raw = (resp.choices[0].message.content or "Neutral").strip().split()[0].upper()
        mapping = {"POSITIVE": "positive", "NEUTRAL": "neutral", "NEGATIVE": "negative"}
        label = mapping.get(raw, "neutral")

        # If you don’t compute probabilities, emit a neutral-ish placeholder.
        score = 0.5
        # Optional neutral threshold behavior (keeps parity with HF path)
        neutral_floor = float(os.getenv("SENTIMENT_NEUTRAL_THRESHOLD", "0.65"))
        if label in {"positive", "negative"} and score < neutral_floor:
            label = "neutral"

        return {"provider": "openai", "label": label, "score": score}

    except Exception as e:
        return {"provider": "openai", "label": "neutral", "score": 0.5, "error": str(e)}

# --- public API ---------------------------------------------------------------

__all__ = ["analyze_sentiment"]

def analyze_sentiment(text: str, provider: Optional[str] = None) -> Dict[str, Any]:
    """
    Analyze sentiment and return a dict:
    {"provider": str, "label": "positive|neutral|negative", "score": float, ...}

    - Respects ENABLE_LLM=0 (offline fallback).
    - Auto-picks provider unless `provider` is passed explicitly.
    - Never raises at import time; errors are embedded in the return dict.
    """
    # If LLM features are disabled, always use offline heuristic.
    if not _enabled_llm():
        return _sentiment_offline(text)

    prov = (provider or _pick_provider()).lower()

    if prov == "hf":
        return _sentiment_hf(text)
    if prov == "azure":
        return _sentiment_azure(text)
    if prov == "openai":
        # Uses the lazy, import-safe helper you just added
        try:
            out = _sentiment_openai_provider(text)
            # Normalize None → offline fallback to keep contract stable
            if out is None:
                return _sentiment_offline(text)
            # If helper returned tuple (label, score), normalize to dict
            if isinstance(out, tuple) and len(out) == 2:
                label, score = out
                return {"provider": "openai", "label": str(label).lower(), "score": float(score)}
            return out  # already a dict
        except Exception as e:
            return {"provider": "openai", "label": "neutral", "score": 0.5, "error": str(e)}

    # Optional providers supported later; keep import-safe fallbacks.
    if prov in {"cohere", "deepai"}:
        return _sentiment_offline(text)

    # Unknown → safe default
    return _sentiment_offline(text)