import gradio as gr
import subprocess
import os
import language_tool_python

# Function to call the Java method
def call_java_method(input_text):
    # Here you can call your Java application.
    # Example: Adjust this line to your actual Java application call.
    try:
        result = subprocess.run(
            ["java", "-jar", "YourJavaApp.jar", input_text],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout.strip()  # Return the output from the Java application
    except subprocess.CalledProcessError as e:
        return f"Error: {e}"

# Function to correct grammar using LanguageTool
def correct_grammar(text):
    tool = language_tool_python.LanguageTool('en-US')
    matches = tool.check(text)
    corrected_text = language_tool_python.utils.correct(text, matches)
    return corrected_text

# Function to process input text
def process_text(input_text):
    java_output = call_java_method(input_text)
    corrected_output = correct_grammar(java_output)
    return corrected_output

# Gradio interface without flagging
iface = gr.Interface(
    fn=process_text,
    inputs="text",
    outputs="text",
    title="Text Processing App",
    description="Enter text to process it through Java and correct grammar using LanguageTool.",
    allow_flagging='never'  # Disable flagging feature
)

if __name__ == "__main__":
    iface.launch(share=True)