File size: 1,011 Bytes
c202b1d
539d41b
 
c202b1d
539d41b
 
 
 
c202b1d
539d41b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Load the model and tokenizer
model_name = "maulanayyy/code_translation_codet5"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

# Function to perform inference
def translate_code(input_code):
    # Prepare the input text
    input_text = f"translate Java to C#: {input_code}"
    
    # Tokenize the input
    input_ids = tokenizer(input_text, return_tensors="pt").input_ids
    
    # Generate the output
    with torch.no_grad():
        outputs = model.generate(input_ids, max_length=512)
    
    # Decode the output
    translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return translated_code

# Create Gradio interface
demo = gr.Interface(fn=translate_code, inputs="text", outputs="text", title="Java to C# Code Translator", description="Enter Java code to translate it to C#.")

# Launch the interface
demo.launch()