Spaces:
Runtime error
Runtime error
root commited on
Commit ·
9b5475f
1
Parent(s): b4a7050
Modified the code
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
tokenizer = AutoTokenizer.from_pretrained("shibsankardhara2/Qwen2.5-Coder-1.5B-Java-CSharp_V2")
|
|
@@ -16,19 +18,38 @@ def single_function(code):
|
|
| 16 |
if beg_count == end_count and beg_count > 0:
|
| 17 |
start = n
|
| 18 |
break
|
|
|
|
|
|
|
| 19 |
return code[:start+1]
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
demo=gr.Interface(
|
| 30 |
fn=generate_code,
|
| 31 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 32 |
outputs=gr.Textbox(label="Generated Code"),
|
| 33 |
title="CodeGen NL to Java and Java to C#"
|
| 34 |
)
|
|
|
|
| 1 |
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_V2")
|
|
|
|
| 18 |
if beg_count == end_count and beg_count > 0:
|
| 19 |
start = n
|
| 20 |
break
|
| 21 |
+
if start == 0:
|
| 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#":
|
| 28 |
+
prompt = f"### Instruction:\n\n{nl_input} Write the solution in Java.\n\n### Response:\n\n"
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 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 |
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
fn=generate_code,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Dropdown(label="Select Task", choices=["NL to Java", "Java to C#", "End to End NL to C#"], value="NL to Java"),
|
| 51 |
+
gr.Textbox(label="Input")
|
| 52 |
+
],
|
| 53 |
outputs=gr.Textbox(label="Generated Code"),
|
| 54 |
title="CodeGen NL to Java and Java to C#"
|
| 55 |
)
|