ThreatLevelD commited on
Commit
8652e94
·
1 Parent(s): 9b2f3b7

Refactored main.py

Browse files
Files changed (2) hide show
  1. gradio_ui.py +148 -42
  2. main.py +7 -5
gradio_ui.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
2
  import yaml
3
  import os
 
 
 
4
 
5
  from core.codex_informer import CodexInformer
6
  from core.eil_processor import EILProcessor
@@ -8,35 +11,73 @@ from core.esil_inference import ESILInference
8
  from core.eris_reasoner import ERISReasoner
9
  from core.fec_controller import FECController
10
 
11
- # Initialize MEC Core Components
12
  codex_informer = CodexInformer()
13
  eil_processor = EILProcessor(codex_informer)
14
  esil_formatter = ESILInference(codex_informer)
15
  eris_engine = ERISReasoner()
16
  fec_controller = FECController()
17
 
18
- # Load Response Strategies YAML
 
19
  config_path = os.path.join("config", "response_strategies.yaml")
20
  with open(config_path, 'r', encoding='utf-8') as f:
21
  response_strategies = yaml.safe_load(f).get('response_strategies', {})
22
 
23
- def process_input(user_text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  try:
25
- print(f"[DEBUG] Received user input: {user_text!r}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Step 1: EIL Processing
28
- eil_result = eil_processor.infer_emotion(user_text)
29
- print(f"[DEBUG] EIL Result: {eil_result}")
30
 
31
  # Step 2: ESIL Formatting
32
  esil_packet = esil_formatter.infer_esil(eil_result)
33
- print(f"[DEBUG] ESIL Packet: {esil_packet}")
34
 
35
  # Step 3: ERIS Reasoning
36
  eris_result = eris_engine.reason_emotion_state(esil_packet)
37
- print(f"[DEBUG] ERIS Result: {eris_result}")
38
 
39
- # Pull out your primary_emotion_code (case-agnostic)
40
  fam_code = (
41
  eris_result.get('primary_emotion_code') or
42
  eris_result.get('Primary Emotion Code') or
@@ -45,6 +86,16 @@ def process_input(user_text):
45
  if not fam_code:
46
  raise KeyError("`primary_emotion_code` missing in ERIS result")
47
 
 
 
 
 
 
 
 
 
 
 
48
  # Lookup response strategy
49
  rs = response_strategies.get(fam_code, {})
50
  rsm_code = rs.get('rsm_code', 'RSM-UNKNOWN')
@@ -57,44 +108,99 @@ def process_input(user_text):
57
  f"{sample_response}"
58
  )
59
 
60
- # Step 4: Fusion Engine ensure keys match what FECController expects
61
- final_uesp = {
62
- # FEC expects these keys:
63
- 'emotion_family': eris_result.get('emotion_family', fam_code),
64
- 'Primary Emotion Code': fam_code,
65
- **eris_result # Carry over everything else, in case
66
- }
67
-
68
- fusion_prompt = fec_controller.generate_prompt(final_uesp)
69
- print("[DEBUG] Fusion Prompt Generated")
70
-
71
- return fusion_prompt, simulated_output
72
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  except Exception as e:
74
- print(f"[ERROR] Exception in process_input: {e}")
75
- return "[ERROR: Unable to process input]", f"An error occurred: {e}"
 
 
 
 
 
76
 
77
- def launch_ui():
78
- with gr.Blocks(title="MEC MVP – Empathic UI") as demo:
79
- gr.Markdown("# Master Emotional Core (MEC) MVP UI\nEmpathy-first AI. Built to protect.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- with gr.Row():
82
- user_input = gr.Textbox(label="Enter your message:", placeholder="Type here...", lines=3)
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- with gr.Row():
85
- submit_button = gr.Button("Process Input")
 
86
 
87
- with gr.Row():
88
- fusion_prompt_display = gr.Textbox(label="Fusion Prompt", lines=6, interactive=False)
89
- empathic_response_display = gr.Textbox(label="Simulated Empathic Response", lines=8, interactive=False)
90
 
91
- submit_button.click(
92
- fn=process_input,
93
- inputs=[user_input],
94
- outputs=[fusion_prompt_display, empathic_response_display]
95
- )
96
 
97
- demo.launch()
 
 
 
 
 
98
 
99
- if __name__ == "__main__":
100
- launch_ui()
 
1
  import gradio as gr
2
  import yaml
3
  import os
4
+ from datetime import datetime
5
+
6
+ from googletrans import Translator
7
 
8
  from core.codex_informer import CodexInformer
9
  from core.eil_processor import EILProcessor
 
11
  from core.eris_reasoner import ERISReasoner
12
  from core.fec_controller import FECController
13
 
14
+ # 1️⃣ --- SETUP & BRANDING ---
15
  codex_informer = CodexInformer()
16
  eil_processor = EILProcessor(codex_informer)
17
  esil_formatter = ESILInference(codex_informer)
18
  eris_engine = ERISReasoner()
19
  fec_controller = FECController()
20
 
21
+ logo_path = "D:\EmpathyEthics\MEC docs\GIT\mec-mvp\assets\Transparent_logo_40.webp" # Update if needed
22
+
23
  config_path = os.path.join("config", "response_strategies.yaml")
24
  with open(config_path, 'r', encoding='utf-8') as f:
25
  response_strategies = yaml.safe_load(f).get('response_strategies', {})
26
 
27
+ LANGS = {
28
+ "English": "en",
29
+ "Spanish": "es",
30
+ "French": "fr",
31
+ "Chinese (Simplified)": "zh-cn",
32
+ "German": "de"
33
+ }
34
+ DEFAULT_LANG = "English"
35
+ DEFAULT_TONE = "Empathic (Default)"
36
+ TONES = [
37
+ "Empathic (Default)",
38
+ "Direct",
39
+ "Gentle",
40
+ "Encouraging",
41
+ "Reflective"
42
+ ]
43
+
44
+ translator = Translator()
45
+
46
+ def translate_text(text, target_lang_code):
47
+ if target_lang_code == "en":
48
+ return text
49
  try:
50
+ result = translator.translate(text, dest=target_lang_code)
51
+ return result.text
52
+ except Exception:
53
+ return f"[Translation Error] {text}"
54
+
55
+ # 2️⃣ --- PIPELINE ---
56
+ def process_input(user_text, selected_tone, selected_language):
57
+ trace = []
58
+ try:
59
+ trace.append(f"[DEBUG] Input Text: {user_text!r}")
60
+ trace.append(f"[DEBUG] Selected Tone: {selected_tone}")
61
+ trace.append(f"[DEBUG] Selected Language: {selected_language}")
62
+
63
+ # Optionally translate input to English if not already (for model)
64
+ lang_code = LANGS.get(selected_language, "en")
65
+ user_text_en = translate_text(user_text, "en") if lang_code != "en" else user_text
66
+ if lang_code != "en":
67
+ trace.append(f"[DEBUG] Translated input to English: {user_text_en}")
68
 
69
  # Step 1: EIL Processing
70
+ eil_result = eil_processor.infer_emotion(user_text_en)
71
+ trace.append(f"[DEBUG] EIL Result: {eil_result}")
72
 
73
  # Step 2: ESIL Formatting
74
  esil_packet = esil_formatter.infer_esil(eil_result)
75
+ trace.append(f"[DEBUG] ESIL Packet: {esil_packet}")
76
 
77
  # Step 3: ERIS Reasoning
78
  eris_result = eris_engine.reason_emotion_state(esil_packet)
79
+ trace.append(f"[DEBUG] ERIS Result: {eris_result}")
80
 
 
81
  fam_code = (
82
  eris_result.get('primary_emotion_code') or
83
  eris_result.get('Primary Emotion Code') or
 
86
  if not fam_code:
87
  raise KeyError("`primary_emotion_code` missing in ERIS result")
88
 
89
+ # Pass tone through to Fusion Engine
90
+ final_uesp = {
91
+ 'emotion_family': eris_result.get('emotion_family', fam_code),
92
+ 'Primary Emotion Code': fam_code,
93
+ 'response_tone': selected_tone, # This is new!
94
+ **eris_result
95
+ }
96
+ fusion_prompt = fec_controller.generate_prompt(final_uesp)
97
+ trace.append(f"[DEBUG] Fusion Prompt Generated: {fusion_prompt}")
98
+
99
  # Lookup response strategy
100
  rs = response_strategies.get(fam_code, {})
101
  rsm_code = rs.get('rsm_code', 'RSM-UNKNOWN')
 
108
  f"{sample_response}"
109
  )
110
 
111
+ # Optionally translate output back to user language
112
+ if lang_code != "en":
113
+ fusion_prompt = translate_text(fusion_prompt, lang_code)
114
+ simulated_output = translate_text(simulated_output, lang_code)
115
+
116
+ # 3️⃣ --- EMPATHY REPORT GENERATION ---
117
+ report_contents = (
118
+ f"Empathy Report - MEC MVP\n"
119
+ f"Date: {datetime.now().isoformat(timespec='seconds')}\n"
120
+ f"Input: {user_text}\n"
121
+ f"Tone: {selected_tone}\n"
122
+ f"Language: {selected_language}\n"
123
+ f"\n"
124
+ f"Primary Emotion: {fam_code}\n"
125
+ f"Blend: {eris_result.get('blend')}\n"
126
+ f"Arc: {eris_result.get('arc')}\n"
127
+ f"Resonance: {eris_result.get('resonance')}\n"
128
+ f"\n"
129
+ f"Fusion Prompt:\n{fusion_prompt}\n"
130
+ f"\n"
131
+ f"Simulated Empathic Response:\n{simulated_output}\n"
132
+ )
133
+ report_file = f"/tmp/mec_empathy_report_{datetime.now().strftime('%Y%m%d%H%M%S')}.txt"
134
+ with open(report_file, "w", encoding='utf-8') as f:
135
+ f.write(report_contents)
136
+
137
+ return (
138
+ fusion_prompt,
139
+ simulated_output,
140
+ "\n".join(trace),
141
+ report_file
142
+ )
143
  except Exception as e:
144
+ trace.append(f"[ERROR] Exception in process_input: {e}")
145
+ return (
146
+ "[ERROR: Unable to process input]",
147
+ f"An error occurred: {e}",
148
+ "\n".join(trace),
149
+ None
150
+ )
151
 
152
+ # 4️⃣ --- UI LAYOUT ---
153
+ with gr.Blocks(title="MEC MVP – Empathic UI", theme="soft", css="#logo-img {margin-bottom: -30px;}") as demo:
154
+ # Branding: Logo & Title
155
+ with gr.Row():
156
+ gr.Image(value=logo_path, label="", show_label=False, height=100, elem_id="logo-img")
157
+ gr.Markdown("## Master Emotional Core (MEC) MVP UI\nEmpathy-first AI. Built to protect.")
158
+
159
+ # Guided Onboarding
160
+ gr.Markdown(
161
+ "Welcome to the MEC Empathy MVP! Enter a story, thought, or feeling below and select a tone or language for the response. "
162
+ "You can expand the Emotional Reasoning Trace below to see how the AI made its decision. "
163
+ "Download your empathy report after each run for further review or research."
164
+ )
165
+
166
+ with gr.Row():
167
+ user_input = gr.Textbox(
168
+ label="Enter your message or story:",
169
+ placeholder="Type here... (Long-form, multi-sentence, or single phrase)",
170
+ lines=5,
171
+ info="Try stories, complex feelings, or emotional blends!"
172
+ )
173
 
174
+ with gr.Row():
175
+ selected_tone = gr.Dropdown(
176
+ choices=TONES,
177
+ value=DEFAULT_TONE,
178
+ label="Select Empathic Response Tone",
179
+ info="Choose how the AI should frame its empathic response."
180
+ )
181
+ selected_language = gr.Dropdown(
182
+ choices=list(LANGS.keys()),
183
+ value=DEFAULT_LANG,
184
+ label="Language",
185
+ info="Choose your preferred language for input/output."
186
+ )
187
 
188
+ with gr.Row():
189
+ submit_button = gr.Button("Process Input")
190
+ download_report_btn = gr.File(label="Download Empathy Report", visible=True)
191
 
192
+ with gr.Row():
193
+ fusion_prompt_display = gr.Textbox(label="Fusion Prompt", lines=5, interactive=False)
194
+ empathic_response_display = gr.Textbox(label="Simulated Empathic Response", lines=7, interactive=False)
195
 
196
+ with gr.Accordion("Show Emotional Reasoning Trace (Technical)", open=False):
197
+ reasoning_trace = gr.Textbox(label="Debug/Trace", lines=15, interactive=False)
 
 
 
198
 
199
+ # Submit logic
200
+ submit_button.click(
201
+ fn=process_input,
202
+ inputs=[user_input, selected_tone, selected_language],
203
+ outputs=[fusion_prompt_display, empathic_response_display, reasoning_trace, download_report_btn]
204
+ )
205
 
206
+ demo.launch()
 
main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from core.eil_processor import EILProcessor
2
  from core.esil_inference import ESILInference
3
  from core.eris_reasoner import ERISReasoner
@@ -13,15 +14,16 @@ def run_pipeline(user_input_text, force_hei=False):
13
  with open('config/response_strategies.yaml', 'r', encoding='utf-8') as f:
14
  response_strategies = yaml.safe_load(f)['response_strategies']
15
 
16
- # 1️⃣ EIL Processor (handles both normalization and emotion processing)
17
- eil = EILProcessor()
 
18
 
19
  # Run emotion inference
20
  eil_packet = eil.infer_emotion(user_input_text)
21
  print(f"[Main] EIL Packet Output: {eil_packet}")
22
 
23
  # 2️⃣ ESIL Inference
24
- esil = ESILInference()
25
  esil_packet = esil.infer_esil(eil_packet)
26
 
27
  # 3️⃣ Forced HEI Mode: Ensure it forces the low confidence path if True
@@ -45,7 +47,7 @@ def run_pipeline(user_input_text, force_hei=False):
45
  print(f"[Main] Final Fusion Prompt:\n{fusion_prompt}")
46
 
47
  # 6️⃣ Simulated Empathic Response
48
- fam_code = final_uesp['primary_emotion_code']
49
  rsm_code = response_strategies.get(fam_code, {}).get('rsm_code', 'RSM-UNKNOWN')
50
  strategy_name = response_strategies.get(fam_code, {}).get('strategy', 'Strategy not defined')
51
  sample_response = response_strategies.get(fam_code, {}).get('sample_response', 'No response available')
@@ -76,7 +78,7 @@ def run_pipeline(user_input_text, force_hei=False):
76
  print(f"[Main] Final Fusion Prompt:\n{fusion_prompt}")
77
 
78
  # 8️⃣ Simulated Empathic Response
79
- fam_code = final_uesp['primary_emotion_code']
80
  rsm_code = response_strategies.get(fam_code, {}).get('rsm_code', 'RSM-UNKNOWN')
81
  strategy_name = response_strategies.get(fam_code, {}).get('strategy', 'Strategy not defined')
82
  sample_response = response_strategies.get(fam_code, {}).get('sample_response', 'No response available')
 
1
+ from core.codex_informer import CodexInformer
2
  from core.eil_processor import EILProcessor
3
  from core.esil_inference import ESILInference
4
  from core.eris_reasoner import ERISReasoner
 
14
  with open('config/response_strategies.yaml', 'r', encoding='utf-8') as f:
15
  response_strategies = yaml.safe_load(f)['response_strategies']
16
 
17
+ # 1️⃣ CodexInformer and EIL Processor (handles both normalization and emotion processing)
18
+ codex_informer = CodexInformer()
19
+ eil = EILProcessor(codex_informer)
20
 
21
  # Run emotion inference
22
  eil_packet = eil.infer_emotion(user_input_text)
23
  print(f"[Main] EIL Packet Output: {eil_packet}")
24
 
25
  # 2️⃣ ESIL Inference
26
+ esil = ESILInference(codex_informer)
27
  esil_packet = esil.infer_esil(eil_packet)
28
 
29
  # 3️⃣ Forced HEI Mode: Ensure it forces the low confidence path if True
 
47
  print(f"[Main] Final Fusion Prompt:\n{fusion_prompt}")
48
 
49
  # 6️⃣ Simulated Empathic Response
50
+ fam_code = final_uesp.get('primary_emotion_code') or final_uesp.get('Primary Emotion Code')
51
  rsm_code = response_strategies.get(fam_code, {}).get('rsm_code', 'RSM-UNKNOWN')
52
  strategy_name = response_strategies.get(fam_code, {}).get('strategy', 'Strategy not defined')
53
  sample_response = response_strategies.get(fam_code, {}).get('sample_response', 'No response available')
 
78
  print(f"[Main] Final Fusion Prompt:\n{fusion_prompt}")
79
 
80
  # 8️⃣ Simulated Empathic Response
81
+ fam_code = final_uesp.get('primary_emotion_code') or final_uesp.get('Primary Emotion Code')
82
  rsm_code = response_strategies.get(fam_code, {}).get('rsm_code', 'RSM-UNKNOWN')
83
  strategy_name = response_strategies.get(fam_code, {}).get('strategy', 'Strategy not defined')
84
  sample_response = response_strategies.get(fam_code, {}).get('sample_response', 'No response available')