File size: 1,483 Bytes
0307592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()