Upload 3 files
Browse files- app.py +44 -0
- sample.cs +1 -0
- translator.py +11 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from translator import get_translation_pipeline, translate_cobol_to_csharp
|
4 |
+
|
5 |
+
# β
Load the translation pipeline
|
6 |
+
pipeline = get_translation_pipeline()
|
7 |
+
|
8 |
+
# β
This function handles file upload and saving the output
|
9 |
+
def translate_and_save(file_obj):
|
10 |
+
try:
|
11 |
+
if not file_obj:
|
12 |
+
return "β No file uploaded."
|
13 |
+
|
14 |
+
# β
Get path of uploaded file
|
15 |
+
file_path = file_obj.name
|
16 |
+
|
17 |
+
# β
Read the file content
|
18 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
19 |
+
cobol_code = f.read()
|
20 |
+
|
21 |
+
# β
Extract base name
|
22 |
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
23 |
+
|
24 |
+
# β
Translate COBOL to C#
|
25 |
+
csharp_code = translate_cobol_to_csharp(pipeline, cobol_code)
|
26 |
+
|
27 |
+
# β
Save the translated output
|
28 |
+
output_file = f"{base_name}.cs"
|
29 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
30 |
+
f.write(csharp_code)
|
31 |
+
|
32 |
+
return f"β
Translation complete. Saved as: {output_file}\n\n{csharp_code}"
|
33 |
+
|
34 |
+
except Exception as e:
|
35 |
+
return f"β ERROR: {str(e)}"
|
36 |
+
|
37 |
+
# β
Create the Gradio interface
|
38 |
+
gr.Interface(
|
39 |
+
fn=translate_and_save,
|
40 |
+
inputs=gr.File(label="Upload COBOL File (.cob)", file_types=[".cob"]),
|
41 |
+
outputs=gr.Textbox(label="C# Output", lines=25),
|
42 |
+
title="COBOL to C# (.NET) Translator",
|
43 |
+
description="Upload a COBOL file. This app will translate it and save a .cs file."
|
44 |
+
).launch()
|
sample.cs
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Translate this COBOL code to C# .
|
translator.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
def get_translation_pipeline():
|
4 |
+
# Use a public model that doesn't require login
|
5 |
+
model_name = "Salesforce/codet5-base-multi-sum"
|
6 |
+
return pipeline("text2text-generation", model=model_name, tokenizer=model_name)
|
7 |
+
|
8 |
+
def translate_cobol_to_csharp(pipe, cobol_code: str) -> str:
|
9 |
+
prompt = f"Translate this COBOL code to C#:\n\n{cobol_code}"
|
10 |
+
result = pipe(prompt, max_length=512, do_sample=False)[0]["generated_text"]
|
11 |
+
return result
|