Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
# Load the model and tokenizer | |
model_name = "maulanayyy/codet5_code_translation" # Ganti dengan nama model yang benar | |
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}" | |
# Check if GPU is available | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
# Tokenize the input | |
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(device) # Pastikan input_ids ada di GPU | |
# 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 |