Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
-
import time # Untuk simulasi delay
|
5 |
|
6 |
# Load the model and tokenizer
|
7 |
model_name = "maulanayyy/codet5_code_translation-v3" # Ganti dengan nama model yang benar
|
@@ -12,7 +11,7 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
model.to(device) # Pindahkan model ke GPU jika tersedia
|
14 |
|
15 |
-
# Function to perform inference
|
16 |
def translate_code(input_code):
|
17 |
try:
|
18 |
# Prepare the input text
|
@@ -27,18 +26,14 @@ def translate_code(input_code):
|
|
27 |
|
28 |
# Decode the output
|
29 |
translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
30 |
-
|
31 |
-
# Simulate streaming output
|
32 |
-
for char in translated_code:
|
33 |
-
yield char # Mengirimkan karakter satu per satu
|
34 |
-
time.sleep(0.05) # Simulasi delay untuk streaming (dapat disesuaikan)
|
35 |
|
36 |
except Exception as e:
|
37 |
print(f"Error during translation: {e}")
|
38 |
-
|
39 |
|
40 |
-
# Create Gradio interface
|
41 |
-
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#."
|
42 |
|
43 |
# Launch the interface
|
44 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
4 |
|
5 |
# Load the model and tokenizer
|
6 |
model_name = "maulanayyy/codet5_code_translation-v3" # Ganti dengan nama model yang benar
|
|
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
model.to(device) # Pindahkan model ke GPU jika tersedia
|
13 |
|
14 |
+
# Function to perform inference
|
15 |
def translate_code(input_code):
|
16 |
try:
|
17 |
# Prepare the input text
|
|
|
26 |
|
27 |
# Decode the output
|
28 |
translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
return translated_code # Kembalikan hasil akhir
|
|
|
|
|
|
|
|
|
30 |
|
31 |
except Exception as e:
|
32 |
print(f"Error during translation: {e}")
|
33 |
+
return "An error occurred during translation."
|
34 |
|
35 |
+
# Create Gradio interface
|
36 |
+
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#.")
|
37 |
|
38 |
# Launch the interface
|
39 |
demo.launch(share=True)
|