File size: 6,236 Bytes
21b8553
3f559a4
 
a456c65
2425e2a
9899531
21b8553
a456c65
 
 
 
9899531
 
 
 
 
 
 
 
 
 
 
 
 
a456c65
9899531
 
 
 
 
2425e2a
9899531
 
2425e2a
9899531
 
2425e2a
9899531
 
 
 
 
 
 
 
2425e2a
 
9899531
 
21b8553
9899531
 
 
 
3f559a4
9899531
 
3f559a4
9899531
 
 
 
 
21b8553
3f559a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9899531
3f559a4
 
 
 
 
21b8553
3f559a4
 
9899531
3f559a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a456c65
3f559a4
 
a456c65
3f559a4
 
 
a456c65
 
 
3f559a4
 
 
 
a456c65
 
 
3f559a4
 
 
 
 
 
 
21b8553
a456c65
 
 
 
 
 
 
 
 
21b8553
2425e2a
3f559a4
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
import gradio as gr
import os
import json
import logging
import time
import random

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger(__name__)

# Define mock responses for reliability
MOCK_RESPONSES = [
    "I understand how you're feeling. Would you like to talk more about what's been going on?",
    "That sounds challenging. I'm here to support you through this. What has been helping you cope so far?",
    "Thank you for sharing that with me. How long have you been feeling this way?",
    "I hear you. It's important to acknowledge these emotions. Would it help to explore some calming techniques?",
    "I appreciate you opening up. Is there anything specific that triggers these feelings for you?",
    "Your feelings are valid. What kind of support would be most helpful for you right now?",
    "I'm here to listen without judgment. Would you like to talk more about this or would you prefer some suggestions?",
    "It takes courage to express how you're feeling. How can I best support you today?",
    "That's really challenging to deal with. Have you been able to talk to anyone else about this?",
    "I'm glad you reached out. Sometimes just putting feelings into words can help us process them better."
]

def select_mock_response(prompt):
    """Select an appropriate mock response based on the input."""
    # Simple keyword matching for a slightly more relevant response
    if any(word in prompt.lower() for word in ["sad", "depress", "down", "unhappy"]):
        return "I understand you're feeling down. Remember that emotions come and go, and you won't feel this way forever. What small activity might bring you a moment of joy today?"
    
    elif any(word in prompt.lower() for word in ["anxious", "worry", "stress", "nervous"]):
        return "I hear that anxiety is affecting you. Taking slow, deep breaths can help calm your nervous system. Would you like to try a quick breathing exercise together?"
    
    elif any(word in prompt.lower() for word in ["angry", "mad", "frustrated", "upset"]):
        return "It sounds like you're feeling frustrated. That's completely valid. Sometimes it helps to express these feelings in a safe way. What usually helps you process anger?"
    
    elif any(word in prompt.lower() for word in ["happy", "joy", "excite", "great"]):
        return "I'm glad to hear you're feeling positive! Moments of joy are worth celebrating. What contributed to this good feeling?"
    
    elif any(word in prompt.lower() for word in ["confus", "uncertain", "lost"]):
        return "Feeling uncertain can be uncomfortable. Breaking things down into smaller parts sometimes helps provide clarity. What specific aspect feels most confusing right now?"
    
    elif "hello" in prompt.lower() or "hi" in prompt.lower():
        return "Hello! I'm Nova, here to support your emotional wellbeing. How are you feeling today?"
    
    else:
        # Return a random response if no keywords match
        return random.choice(MOCK_RESPONSES)

def generate_response(prompt):
    """Generate a response using our mock system."""
    logger.info(f"Generating response for: {prompt[:30]}...")
    start_time = time.time()
    
    # Get appropriate mock response
    response = select_mock_response(prompt)
    
    # Simulate a brief delay for realism (but not too long)
    time.sleep(0.5)
    
    logger.info(f"Response generated in {time.time() - start_time:.2f} seconds")
    return response

# API endpoint that mimics your current backend API
def chat_api(message):
    """API endpoint that returns a response in the same format as your backend."""
    response_text = generate_response(message)
    
    # Create a response format similar to your current backend
    response = {
        "status": "ok",
        "data": {
            "response": response_text,
            "emotions": [{"label": "neutral", "score": 1.0}],  # Simplified emotions
            "triggers": [{"label": "unknown", "score": 1.0}],  # Simplified triggers
            "actions": ["Check in with yourself"],
            "guidance": [
                {"message": "How are you feeling right now?", "confidence": 0.9}
            ],
            "timestamp": None  # Will be filled by your backend
        },
        "meta": {
            "model_used": "nova-rule-based",
            "debug": None
        }
    }
    
    return json.dumps(response)

# Create Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown(f"# NOVA Rule-Based Backend\n**Note:** Using reliable rule-based responses for faster performance")
    
    with gr.Tab("Chat"):
        with gr.Row():
            with gr.Column():
                message_input = gr.Textbox(
                    label="Your message", 
                    placeholder="Type your message here...",
                    lines=2
                )
                submit_btn = gr.Button("Send")
            
            with gr.Column():
                output = gr.Textbox(label="Response")
                
        submit_btn.click(generate_response, inputs=message_input, outputs=output)
    
    with gr.Tab("API"):
        gr.Markdown(f"""
        ## API Endpoint
        
        Send POST requests to: `https://ram-n-nova-llm-backend.hf.space/api/predict`
        
        **Request format:**
        ```json
        {{
            "data": ["Your message here"]
        }}
        ```
        
        **Response format:**
        ```json
        {{
            "data": "{{\\\"status\\\": \\\"ok\\\", \\\"data\\\": {{\\\"response\\\": \\\"Generated response\\\", ...}}}}"
        }}
        ```
        """)
        
        api_input = gr.Textbox(label="Test API Input", placeholder="Type your message here...")
        api_output = gr.JSON(label="API Response")
        api_btn = gr.Button("Test API")
        api_btn.click(chat_api, inputs=api_input, outputs=api_output)

# Create a direct API endpoint
interface = gr.Interface(
    fn=chat_api,
    inputs="text",
    outputs="json",
    title="NOVA API Endpoint",
    description="Send a message to get a response",
    examples=["I'm feeling sad today", "I'm anxious about my test tomorrow"],
)

# Launch the app
demo.launch()