Spaces:
Running
Running
Commit
·
539d41b
1
Parent(s):
c202b1d
update app.py and add requirement.txt
Browse files- app.py +27 -4
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,7 +1,30 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/code_translation_codet5"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
9 |
|
10 |
+
# Function to perform inference
|
11 |
+
def translate_code(input_code):
|
12 |
+
# Prepare the input text
|
13 |
+
input_text = f"translate Java to C#: {input_code}"
|
14 |
+
|
15 |
+
# Tokenize the input
|
16 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
|
17 |
+
|
18 |
+
# Generate the output
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model.generate(input_ids, max_length=512)
|
21 |
+
|
22 |
+
# Decode the output
|
23 |
+
translated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
+
return translated_code
|
25 |
+
|
26 |
+
# Create Gradio interface
|
27 |
+
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#.")
|
28 |
+
|
29 |
+
# Launch the interface
|
30 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
gradio
|