demo / app.py
Risty3's picture
Upload 3 files
0307592 verified
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()