import gradio as gr import os from translator import get_translation_pipeline, translate_cobol_to_csharp # ✅ Load the translation pipeline pipeline = get_translation_pipeline() # ✅ This function handles file upload and saving the output def translate_and_save(file_obj): try: if not file_obj: return "❌ No file uploaded." # ✅ Get path of uploaded file file_path = file_obj.name # ✅ Read the file content with open(file_path, "r", encoding="utf-8") as f: cobol_code = f.read() # ✅ Extract base name base_name = os.path.splitext(os.path.basename(file_path))[0] # ✅ Translate COBOL to C# csharp_code = translate_cobol_to_csharp(pipeline, cobol_code) # ✅ Save the translated output output_file = f"{base_name}.cs" with open(output_file, "w", encoding="utf-8") as f: f.write(csharp_code) return f"✅ Translation complete. Saved as: {output_file}\n\n{csharp_code}" except Exception as e: return f"❌ ERROR: {str(e)}" # ✅ Create the Gradio interface gr.Interface( fn=translate_and_save, inputs=gr.File(label="Upload COBOL File (.cob)", file_types=[".cob"]), outputs=gr.Textbox(label="C# Output", lines=25), title="COBOL to C# (.NET) Translator", description="Upload a COBOL file. This app will translate it and save a .cs file." ).launch()