Spaces:
Runtime error
Runtime error
root commited on
Commit ·
5cb3921
1
Parent(s): 8957595
Added helper function
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("shibsankardhara2/Qwen2.5-Coder-1.5B-Java-CSharp_V5")
|
| 7 |
model = AutoModelForCausalLM.from_pretrained("shibsankardhara2/Qwen2.5-Coder-1.5B-Java-CSharp_V5")
|
|
@@ -22,6 +23,27 @@ def single_function(code):
|
|
| 22 |
return code
|
| 23 |
return code[:start+1]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@spaces.GPU
|
| 26 |
def generate_code(task_type: str, nl_input: str):
|
| 27 |
if task_type == "NL to Java" or task_type == "End to End NL to C#":
|
|
@@ -30,19 +52,22 @@ def generate_code(task_type: str, nl_input: str):
|
|
| 30 |
outputs = model.generate(**inputs, max_new_tokens=128, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)
|
| 31 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Response:\n\n")[-1].strip()
|
| 32 |
if task_type == "NL to Java":
|
| 33 |
-
return single_function(response)
|
|
|
|
| 34 |
else:
|
| 35 |
prompt = f"### Instruction:\n\nConvert the below Java Code to C#\n\n{single_function(response)}\n\n### Response:\n\n"
|
| 36 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 37 |
outputs = model.generate(**inputs, max_new_tokens=128, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)
|
| 38 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Response:\n\n")[-1].strip()
|
| 39 |
-
return single_function(response)
|
|
|
|
| 40 |
elif task_type == "Java to C#":
|
| 41 |
prompt = f"### Instruction:\n\nConvert the below Java Code to C#\n\n{nl_input}\n\n### Response:\n\n"
|
| 42 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 43 |
outputs = model.generate(**inputs, max_new_tokens=128, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)
|
| 44 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Response:\n\n")[-1].strip()
|
| 45 |
-
return single_function(response)
|
|
|
|
| 46 |
elif task_type=="Generate Java Docs":
|
| 47 |
prompt= f"### Instruction:\n\nGenerate Java Docs for the below Java Code\n\n{nl_input}\n\n### Response:\n\n"
|
| 48 |
inputs=tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("shibsankardhara2/Qwen2.5-Coder-1.5B-Java-CSharp_V5")
|
| 8 |
model = AutoModelForCausalLM.from_pretrained("shibsankardhara2/Qwen2.5-Coder-1.5B-Java-CSharp_V5")
|
|
|
|
| 23 |
return code
|
| 24 |
return code[:start+1]
|
| 25 |
|
| 26 |
+
def _clean_csharp_output(code: str) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Remove spurious `virtual` / `override` modifiers the model adds to bare
|
| 29 |
+
(class-less) method snippets.
|
| 30 |
+
|
| 31 |
+
The model was trained on C# methods that usually live inside a class, where
|
| 32 |
+
`public virtual ...` is common. When it translates a *standalone* Java method
|
| 33 |
+
it carries the `virtual` keyword over — but `virtual`/`override` are only
|
| 34 |
+
valid on members of a class, so on a bare snippet they are invalid C#.
|
| 35 |
+
We therefore strip them ONLY when the snippet has no enclosing type
|
| 36 |
+
declaration (class / struct / interface / record / enum).
|
| 37 |
+
"""
|
| 38 |
+
if re.search(r"\b(class|struct|interface|record|enum)\b", code):
|
| 39 |
+
return code # real type present — leave modifiers intact
|
| 40 |
+
# Drop 'virtual'/'override' after an access modifier: 'public virtual int' -> 'public int'.
|
| 41 |
+
code = re.sub(r"\b(public|private|protected|internal)\s+(?:virtual|override)\s+",
|
| 42 |
+
r"\1 ", code)
|
| 43 |
+
# Drop a leading 'virtual'/'override' with no access modifier.
|
| 44 |
+
code = re.sub(r"(^|\n)(\s*)(?:virtual|override)\s+", r"\1\2", code)
|
| 45 |
+
return code
|
| 46 |
+
|
| 47 |
@spaces.GPU
|
| 48 |
def generate_code(task_type: str, nl_input: str):
|
| 49 |
if task_type == "NL to Java" or task_type == "End to End NL to C#":
|
|
|
|
| 52 |
outputs = model.generate(**inputs, max_new_tokens=128, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)
|
| 53 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Response:\n\n")[-1].strip()
|
| 54 |
if task_type == "NL to Java":
|
| 55 |
+
# return single_function(response)
|
| 56 |
+
return _clean_csharp_output(response)
|
| 57 |
else:
|
| 58 |
prompt = f"### Instruction:\n\nConvert the below Java Code to C#\n\n{single_function(response)}\n\n### Response:\n\n"
|
| 59 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 60 |
outputs = model.generate(**inputs, max_new_tokens=128, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)
|
| 61 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Response:\n\n")[-1].strip()
|
| 62 |
+
# return single_function(response)
|
| 63 |
+
return _clean_csharp_output(response)
|
| 64 |
elif task_type == "Java to C#":
|
| 65 |
prompt = f"### Instruction:\n\nConvert the below Java Code to C#\n\n{nl_input}\n\n### Response:\n\n"
|
| 66 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 67 |
outputs = model.generate(**inputs, max_new_tokens=128, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.eos_token_id)
|
| 68 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Response:\n\n")[-1].strip()
|
| 69 |
+
# return single_function(response)
|
| 70 |
+
return _clean_csharp_output(response)
|
| 71 |
elif task_type=="Generate Java Docs":
|
| 72 |
prompt= f"### Instruction:\n\nGenerate Java Docs for the below Java Code\n\n{nl_input}\n\n### Response:\n\n"
|
| 73 |
inputs=tokenizer(prompt, return_tensors="pt").to(model.device)
|