Spaces:
Running
Running
| 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 |