Tortured Phrase Detector and Corrector
Detect and correct unnatural AIML paraphrases in scientific text
# ============================================================ # LEXOSYNTH - FRONTEND : GRADIO UI # File : frontend.py # # Provides a minimal Gradio interface that calls the Flask # backend at localhost:5000/analyze. # # Layout : # - Heading at top center # - Text input box (rounded) # - Dropdown "Choose Engine" on left # - "Analyze" button on right # - Output textbox with copy button below # ============================================================ import gradio as gr import requests # Flask backend URL FLASK_URL = "http://localhost:5000" # ============================================================ # SECTION 1 : ENGINE OPTIONS # ============================================================ ENGINES = [ "— Select from dropdown —", "Supersonic (Fast)", "Go Deep (Llama)", "Hybimix (Llama + Supersonic)", "Go Deep Max (Sarvam)", "Hybimix Max (Sarvam + Supersonic)" ] # Maps display name to backend engine key ENGINE_MAP = { "Supersonic (Fast)" : "supersonic", "Go Deep (Llama)" : "go_deep", "Hybimix (Llama + Supersonic)" : "hybimix", "Go Deep Max (Sarvam)" : "go_deep_max", "Hybimix Max (Sarvam + Supersonic)": "hybimix_max" } # ============================================================ # SECTION 2 : ANALYZE FUNCTION (CALLS FLASK API) # ============================================================ def analyze(text, engine_choice): """ Called when the user clicks Analyze. Sends text + engine to Flask /analyze and returns the formatted output string for display. Parameters: ----------- text : User input text (one or more sentences) engine_choice : Selected engine display name from dropdown Returns: ----------- str : Formatted result string shown in the output textbox """ # Input validation if not text or not text.strip(): return "⚠ Please enter some text before clicking Analyze." if not engine_choice or engine_choice == "— Select from dropdown —": return "⚠ Please select an engine from the dropdown." engine_key = ENGINE_MAP.get(engine_choice) if not engine_key: return "⚠ Invalid engine selection." # Show which engine is running engine_label = engine_choice # AI engines can be very slow on CPU — notify user is_ai = engine_key in ("go_deep", "hybimix", "go_deep_max", "hybimix_max") try: response = requests.post( f"{FLASK_URL}/analyze", json = {"text": text.strip(), "engine": engine_key}, timeout = 600 # 10 min timeout — AI inference on CPU is slow ) if response.status_code == 200: data = response.json() count = data.get("sentence_count", 0) output = data.get("formatted_output", "No output received.") # Add a summary header header = ( f"Engine : {engine_label}\n" f"Sentences processed : {count}\n" + "═" * 52 + "\n\n" ) return header + output else: error = response.json().get("error", "Unknown server error.") return f"❌ Server error : {error}" except requests.exceptions.ConnectionError: return ( "❌ Could not connect to the Flask backend.\n" " Please make sure test.py is running." ) except requests.exceptions.Timeout: return ( "❌ Request timed out.\n" " AI engines on CPU can take several minutes.\n" " Try again or use Supersonic (Fast) for quick results." ) except Exception as e: return f"❌ Unexpected error : {str(e)}" # ============================================================ # SECTION 3 : CUSTOM CSS # ============================================================ CUSTOM_CSS = """ /* Center the main heading */ .main-title { text-align : center; padding : 24px 0 8px 0; } /* Rounded text input */ .rounded-input textarea, .rounded-input input { border-radius : 12px !important; } /* Rounded output box */ .rounded-output textarea { border-radius : 12px !important; font-family : 'Courier New', monospace; font-size : 0.88rem; } /* Analyze button */ .analyze-btn button { min-height : 46px !important; border-radius : 10px !important; font-size : 1rem !important; font-weight : 600 !important; } /* Dropdown rounded */ .engine-dropdown .wrap { border-radius : 10px !important; } /* Subtle section spacing */ .section-gap { margin-top : 8px; } """ # ============================================================ # SECTION 4 : BUILD GRADIO UI # ============================================================ with gr.Blocks( title = "Tortured Phrase Detector and Corrector", theme = gr.themes.Soft( primary_hue = "slate", secondary_hue = "blue", neutral_hue = "gray" ), css = CUSTOM_CSS ) as demo: # ── Heading ── gr.HTML("""
Detect and correct unnatural AIML paraphrases in scientific text