File size: 4,804 Bytes
4bd368a
 
39c0ee6
4bd368a
b9493b2
f990274
4bd368a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f990274
4bd368a
 
 
 
 
 
 
 
 
 
 
 
 
 
f990274
4bd368a
 
 
 
 
 
 
 
 
 
 
 
 
f990274
4bd368a
 
 
 
101d758
 
f990274
101d758
4bd368a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import json
import os
import shutil
import pandas as pd

class AgentBridge:  # 🟢 Ensure this says AgentBridge, not BridgeAgent
    def __init__(self, input_agent, brain_agent, trust_agent):
        print("🌉 Bridge Agent (React API) Online.")
        self.input = input_agent
        self.brain = brain_agent
        self.trust = trust_agent

    # --- 1. REPUTATION CHECK ---
    def api_check_reputation(self, user_key):
        """Matches React 'run_login' expectations."""
        if not user_key:
            return [None, "Unknown", "No key provided", False, 0, "Novice", 0, "Error"]
        
        # Simulated DB fetch
        current_xp = 150 
        role = "Novice"
        can_validate = False
        display_addr = f"{user_key[:6]}...{user_key[-4:]}"
        
        return [
            user_key,       # 0: Key
            display_addr,   # 1: Address
            "Welcome back", # 2: Message
            can_validate,   # 3: Is Validator?
            current_xp,     # 4: XP
            role,           # 5: Role
            12,             # 6: Rank
            "Keep playing to unlock validation!" # 7: Motivation
        ]

    # --- 2. SUBMISSION LOGIC (The Nexus) ---
    def api_submit_entry(self, transcribed, dialect, customD, clarification, tone, context, pragmatics, 
                         sourceTag, clar_source, userKey, blob, confirm):
        """
        Proxies the React submission to the UX Agent's logic.
        """
        # Ensure Brain has the UX link (injected in main.py)
        if hasattr(self.brain, 'ux'):
            return self.brain.ux.check_and_submit_logic(
                transcribed, dialect, customD, clarification, tone, context, pragmatics,
                sourceTag, clar_source, userKey, blob, confirm
            )
        return "❌ Error: UX Backend not linked.", None, None

    # --- 3. GET DIALECTS ---
    def api_get_dialects(self):
        """Returns list of available dialects for the dropdown."""
        try:
            # Fetch from CSV files in dataset dir
            dataset_dir = self.brain.config.DATASET_DIR
            if os.path.exists(dataset_dir):
                files = [f.replace(".csv", "") for f in os.listdir(dataset_dir) if f.endswith(".csv")]
                return files if files else ["American English", "Korean English", "Nigerian English"]
            return ["American English", "Korean English", "Nigerian English"]
        except:
            return ["American English", "Korean English", "Nigerian English"]

    # --- 4. GENERATE MISSION ---
    def api_generate_mission(self, context):
        """Generates a daily prompt."""
        if self.brain:
            return self.brain.generate_mission(context)
        return {"text": "Describe your morning routine.", "emoji": "☀️"}

    # --- 5. TRANSCRIBE CHECK ---
    def api_transcribe_check(self, audio_path, dialect):
        """Fast transcription for 'The Listener' game verification."""
        if not audio_path: return "No Audio"
        try:
            result = self.input.transcribe(audio_path)
            return result[0]['text']
        except Exception as e:
            return f"Error: {e}"

    # --- LAUNCHER ---
    def launch(self):
        with gr.Blocks() as api:
            # Inputs matching App.js calls
            in_txt = gr.Textbox(label="Text Input")
            in_aud = gr.Audio(type="filepath", label="Audio Input")
            
            # Outputs
            out_json = gr.JSON()
            out_txt = gr.Textbox()
            out_aud = gr.Audio()

            # 1. Login / Reputation
            gr.Button("API Rep").click(
                self.api_check_reputation, 
                inputs=[in_txt], 
                outputs=[out_json], 
                api_name="run_login"
            )
            
            # 2. Submit Entry (12 Args)
            # [orig, d_drop, d_new, clar, tone, context, prag, GAME_SOURCE, CLAR_SOURCE, user_key] + [Audio, Checkbox]
            sub_args = [gr.Textbox() for _ in range(10)] + [gr.Audio(type="filepath"), gr.Checkbox()]
            
            gr.Button("API Submit").click(
                self.api_submit_entry, 
                inputs=sub_args, 
                outputs=[gr.Textbox(), gr.Textbox(), gr.Audio()], 
                api_name="check_and_submit_logic"
            )
            
            # 3. Helpers
            gr.Button("API List").click(self.api_get_dialects, inputs=[], outputs=[out_json], api_name="get_dialects")
            gr.Button("API Mission").click(self.api_generate_mission, inputs=[in_txt], outputs=[out_json], api_name="generate_mission")
            gr.Button("API Transcribe").click(self.api_transcribe_check, inputs=[in_aud, in_txt], outputs=[out_txt], api_name="transcribe_check")

        return api