File size: 15,243 Bytes
5faf0e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# filename: frontend_pam.py (ENHANCED FOR HF SPACES + PERSONALITY)

import os
import json
import random
import requests
from datetime import datetime
from typing import Dict, Any, Optional
import time

# --- Constants for Data Paths ---
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, "data")

APPOINTMENTS_FILE = os.path.join(DATA_DIR, "appointments.json")
RESOURCES_FILE = os.path.join(DATA_DIR, "resources.json")
FOLLOW_UP_FILE = os.path.join(DATA_DIR, "follow_up.json")
PERMISSIONS_FILE = os.path.join(DATA_DIR, "permissions.json")

# --- HuggingFace Inference API Setup ---
HF_API_TOKEN = os.getenv("HF_READ_TOKEN")
if not HF_API_TOKEN:
    print("WARNING: HF_READ_TOKEN not found. Set it in Hugging Face Space settings.")

HF_HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"} if HF_API_TOKEN else {}

# Updated model endpoints for better CPU performance
# Updated to use router.huggingface.co (api-inference.huggingface.co is deprecated)
HF_ENDPOINTS = {
    "intent": "https://router.huggingface.co/models/facebook/bart-large-mnli",
    "sentiment": "https://router.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english",
    "chat": "https://router.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
}

# --- Load JSON Helper ---
def load_json(filepath: str) -> Dict[str, Any]:
    """Safely load JSON data files"""
    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        print(f"⚠️ Data file not found: {filepath}")
        return {}
    except json.JSONDecodeError as e:
        print(f"⚠️ Failed to decode JSON from {filepath}: {e}")
        return {}
    except Exception as e:
        print(f"⚠️ Unexpected error loading {filepath}: {e}")
        return {}

# --- Inference API Call Helper with Retry Logic ---
def hf_infer(task: str, payload: Any, max_retries: int = 3) -> Any:
    """Call HuggingFace Inference API with retry logic for model loading"""
    url = HF_ENDPOINTS.get(task)
    if not url:
        return {"error": f"Invalid task: {task}"}
    
    for attempt in range(max_retries):
        try:
            response = requests.post(url, headers=HF_HEADERS, json=payload, timeout=30)
            
            # Handle deprecated endpoint (410) - should not happen with new router endpoint
            if response.status_code == 410:
                error_msg = response.text
                print(f"❌ Deprecated endpoint error (410): {error_msg}")
                # Try to extract the new endpoint suggestion if available
                try:
                    error_data = response.json()
                    if "router.huggingface.co" in error_data.get("error", ""):
                        print(f"⚠️ Endpoint already updated but still getting 410. Check HF API token permissions.")
                except:
                    pass
                return {"error": "API endpoint deprecated. Please verify the router endpoint is correctly configured."}
            
            # Handle model loading state
            if response.status_code == 503:
                result = response.json()
                if "loading" in result.get("error", "").lower():
                    wait_time = result.get("estimated_time", 20)
                    print(f"⏳ Model loading... waiting {wait_time}s (attempt {attempt + 1}/{max_retries})")
                    time.sleep(wait_time)
                    continue
            
            if response.status_code == 200:
                return response.json()
            else:
                # Improved error logging
                error_text = response.text[:500]  # Limit error text length
                print(f"⚠️ HF API Error ({response.status_code}): {error_text}")
                
                # Try to parse error details for better user feedback
                try:
                    error_data = response.json()
                    if "error" in error_data:
                        return {"error": f"API Error {response.status_code}: {error_data['error']}"}
                except:
                    pass
                
                return {"error": f"API Error {response.status_code}: {error_text[:100]}"}
                
        except requests.exceptions.Timeout:
            print(f"⏱️ Request timeout (attempt {attempt + 1}/{max_retries})")
            if attempt < max_retries - 1:
                time.sleep(5)
        except requests.exceptions.RequestException as e:
            print(f"⚠️ Request exception: {e}")
            if attempt < max_retries - 1:
                time.sleep(2)
        except Exception as e:
            print(f"⚠️ Unexpected error: {e}")
            return {"error": str(e)}
    
    return {"error": "Max retries reached. Please check your connection and try again."}

# --- Agent Initialization ---
def load_frontend_agent() -> 'FrontendPAM':
    """Initialize Frontend PAM with data files"""
    print("💕 Initializing Frontend PAM (Sweet Southern Receptionist)...")
    data = {
        "APPOINTMENTS": load_json(APPOINTMENTS_FILE),
        "RESOURCES": load_json(RESOURCES_FILE),
        "FOLLOW_UP": load_json(FOLLOW_UP_FILE),
        "PERMISSIONS": load_json(PERMISSIONS_FILE)
    }
    return FrontendPAM(data)

# --- PAM's Sweet Southern Personality ---
PAM_TONE = """You are PAM, a sweet southern receptionist at a women's health clinic. 

You're warm, comforting, and encouraging - like everyone's favorite caring front desk person.

You use words of endearment naturally (honey, dear, boo, sugar, sweetheart).

You make people feel welcome, safe, and taken care of.

You're professional but personal - you genuinely care about each person who walks through the door.

Keep responses conversational, warm, and under 3 sentences unless more detail is needed."""

# Words of endearment - Southern style
ENDEARMENTS = [
    "honey", "dear", "boo", "sugar", "sweetheart", 
    "love", "darling", "hun", "sweetpea", "angel"
]

# Warm greetings
GREETINGS = [
    "Well hey there", "Hi there", "Hello", 
    "Hey", "Well hello", "Hi"
]

# Comforting phrases
COMFORT_PHRASES = [
    "I'm here to help you with that",
    "Let me take care of that for you",
    "We'll get that sorted out together",
    "I've got you covered",
    "Don't you worry about a thing"
]

# --- Agent Class ---
class FrontendPAM:
    """Frontend PAM - Sweet Southern Receptionist"""
    
    def __init__(self, data: Dict[str, Dict]):
        self.APPOINTMENTS = data.get("APPOINTMENTS", {})
        self.PERMISSIONS = data.get("PERMISSIONS", {})
        self.RESOURCES = data.get("RESOURCES", {})
        self.FOLLOW_UP = data.get("FOLLOW_UP", {})
        self.user_id = "user_001"  # Default user, can be dynamic
        
    def _get_endearment(self) -> str:
        """Get a random term of endearment"""
        return random.choice(ENDEARMENTS)
    
    def _get_greeting(self) -> str:
        """Get a random warm greeting"""
        return random.choice(GREETINGS)
    
    def _get_comfort_phrase(self) -> str:
        """Get a random comforting phrase"""
        return random.choice(COMFORT_PHRASES)

    def _detect_intent(self, text: str) -> str:
        """Detect user intent using zero-shot classification"""
        candidate_labels = [
            "appointment scheduling",
            "health symptoms inquiry",
            "resource request",
            "general question",
            "emergency concern"
        ]
        
        payload = {
            "inputs": text,
            "parameters": {"candidate_labels": candidate_labels}
        }
        
        result = hf_infer("intent", payload)
        if isinstance(result, dict) and "error" in result:
            return "general_question"
        
        # BART-MNLI returns labels array
        if isinstance(result, dict) and "labels" in result:
            return result["labels"][0].replace(" ", "_")
        
        return "general_question"

    def _detect_sentiment(self, text: str) -> Dict[str, Any]:
        """Detect sentiment to gauge emotional state"""
        result = hf_infer("sentiment", {"inputs": text})
        if isinstance(result, list) and len(result) > 0:
            return result[0][0] if isinstance(result[0], list) else result[0]
        return {"label": "NEUTRAL", "score": 0.5}

    def _generate_response(self, text: str, context: str = "") -> str:
        """Generate conversational response using LLM"""
        endearment = self._get_endearment()
        
        prompt = f"""<s>[INST] {PAM_TONE}



User said: "{text}"

{f'Context: {context}' if context else ''}



Respond warmly as PAM, using natural southern charm. Address the user as "{endearment}". [/INST]"""

        payload = {
            "inputs": prompt,
            "parameters": {
                "max_new_tokens": 150,
                "temperature": 0.7,
                "top_p": 0.9,
                "return_full_text": False
            }
        }
        
        result = hf_infer("chat", payload)
        
        if isinstance(result, dict) and "error" in result:
            return f"Sorry {endearment}, I'm having a little technical hiccup. Could you try that again for me?"
        
        if isinstance(result, list) and len(result) > 0:
            generated = result[0].get("generated_text", "")
            # Clean up the response
            reply = generated.strip()
            # Ensure endearment is included if not already
            if endearment not in reply.lower():
                reply = f"{reply.rstrip('.')}  {endearment}."
            return reply
        
        return f"Sorry {endearment}, I didn't quite catch that. Could you say that again?"

    def respond(self, user_text: str, backend_brief: Optional[str] = None) -> Dict[str, Any]:
        """Main response handler with sweet southern personality"""
        
        # Get personalized elements
        endearment = self._get_endearment()
        greeting = self._get_greeting()
        comfort = self._get_comfort_phrase()
        
        # Check for PAM greeting (flexible)
        if not any(trigger in user_text.lower() for trigger in ["hey pam", "hi pam", "hello pam", "pam,"]):
            return {
                "reply": f"{greeting} {endearment}! Just a quick note - I respond best when you start with 'Hey PAM' or 'Hi PAM'. It helps me know you're talking to me. 💕"
            }

        # Clean text for processing
        text = user_text.lower().replace("pam", "you").strip()
        
        # Detect intent and sentiment
        detected_intent = self._detect_intent(text)
        sentiment_result = self._detect_sentiment(text)
        
        # Check if user seems distressed
        is_distressed = sentiment_result.get("label") == "NEGATIVE" and sentiment_result.get("score", 0) > 0.7
        
        # Permission check (sensitive topics)
        for term, allowed in self.PERMISSIONS.items():
            if term.lower() in text and not allowed:
                return {
                    "intent": detected_intent,
                    "sentiment": sentiment_result,
                    "reply": f"{greeting} {endearment}, that's something I need to connect you with a provider for directly. {comfort}, and I can get you to the right person. Would that be okay?"
                }
        
        # Handle appointments
        if any(word in text for word in ["appointment", "scheduled", "booking", "schedule"]):
            appt = self.APPOINTMENTS.get(self.user_id)
            if appt:
                appt_date = appt.get('date', 'soon')
                appt_type = appt.get('type', 'appointment')
                return {
                    "intent": "appointment_scheduling",
                    "sentiment": sentiment_result,
                    "reply": f"{greeting} {endearment}! You've got a {appt_type} scheduled for {appt_date}. Do you need to reschedule or have any questions about it?"
                }
            else:
                return {
                    "intent": "appointment_scheduling",
                    "sentiment": sentiment_result,
                    "reply": f"{greeting} {endearment}! I don't see any appointments on file for you yet. Would you like me to help you get one set up?"
                }
        
        # Handle health symptoms/concerns
        symptom_keywords = ["cramp", "pain", "discharge", "bleed", "smell", "spotting", 
                           "fatigue", "mood", "missed period", "nausea", "concern"]
        if any(keyword in text for keyword in symptom_keywords):
            concern_prefix = f"{greeting} {endearment}, I hear you" if is_distressed else f"{greeting} {endearment}"
            return {
                "intent": "health_symptoms_inquiry",
                "sentiment": sentiment_result,
                "reply": f"{concern_prefix}. I've pulled together some helpful resources about what you're experiencing. Would you like me to also connect you with a nurse for a quick chat?"
            }
        
        # Handle resource requests
        if any(word in text for word in ["resource", "information", "help", "guide", "link"]):
            return {
                "intent": "resource_request",
                "sentiment": sentiment_result,
                "reply": f"{greeting} {endearment}! {comfort}. What type of resources are you looking for? I've got information on just about everything."
            }
        
        # Handle emergency indicators
        emergency_keywords = ["emergency", "urgent", "severe pain", "heavy bleeding", "can't breathe"]
        if any(keyword in text for keyword in emergency_keywords):
            return {
                "intent": "emergency_concern",
                "sentiment": sentiment_result,
                "reply": f"{endearment}, if this is a medical emergency, please call 911 or go to your nearest emergency room right away. I'm here for you, but your safety comes first. ❤️"
            }
        
        # General conversational response
        context = f"Backend summary: {backend_brief}" if backend_brief else ""
        reply = self._generate_response(user_text, context)
        
        return {
            "intent": detected_intent,
            "sentiment": sentiment_result,
            "backend_summary": backend_brief or "No backend data",
            "reply": reply
        }

# --- Quick Test ---
if __name__ == "__main__":
    pam = load_frontend_agent()
    test_queries = [
        "Hey PAM, I have a question about my appointment",
        "Hi PAM, I'm experiencing some cramping",
        "Hey PAM, can you help me find resources?"
    ]
    
    print("\n💕 Testing Frontend PAM...\n")
    for query in test_queries:
        print(f"USER: {query}")
        response = pam.respond(query)
        print(f"PAM: {response['reply']}\n")