Spaces:
Runtime error
Runtime error
Commit
Β·
737641a
1
Parent(s):
bb441f2
application deployed
Browse files
app.py
CHANGED
@@ -9,19 +9,25 @@
|
|
9 |
#######################
|
10 |
|
11 |
import gradio as gr
|
12 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
13 |
|
|
|
14 |
model_id = "codellama/CodeLlama-7b-Instruct-hf"
|
15 |
|
16 |
-
# Load
|
17 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
18 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
|
23 |
-
# Define the function to handle the conversion
|
24 |
def convert_python_to_r(python_code):
|
|
|
25 |
prompt = f"""### Task:
|
26 |
Convert the following Python code to equivalent R code.
|
27 |
|
@@ -29,22 +35,40 @@ Convert the following Python code to equivalent R code.
|
|
29 |
{python_code}
|
30 |
|
31 |
### R code:"""
|
32 |
-
|
33 |
-
|
34 |
-
prompt,
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
)
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Gradio Interface
|
44 |
gr.Interface(
|
45 |
fn=convert_python_to_r,
|
46 |
inputs=gr.Textbox(lines=10, placeholder="Paste your Python code here..."),
|
47 |
outputs="text",
|
48 |
-
title="Python to R Code Converter using CodeLlama",
|
49 |
-
description="Enter Python code, and
|
50 |
).launch()
|
|
|
|
9 |
#######################
|
10 |
|
11 |
import gradio as gr
|
12 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
13 |
+
import torch
|
14 |
|
15 |
+
# Model ID (7B is safer for free-tier Space)
|
16 |
model_id = "codellama/CodeLlama-7b-Instruct-hf"
|
17 |
|
18 |
+
# Load tokenizer and model
|
19 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
21 |
+
model_id,
|
22 |
+
device_map="auto",
|
23 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
24 |
+
)
|
25 |
|
26 |
+
# Ensure pad token is set to eos token to avoid padding warnings
|
27 |
+
tokenizer.pad_token = tokenizer.eos_token
|
28 |
|
|
|
29 |
def convert_python_to_r(python_code):
|
30 |
+
# Prompt construction
|
31 |
prompt = f"""### Task:
|
32 |
Convert the following Python code to equivalent R code.
|
33 |
|
|
|
35 |
{python_code}
|
36 |
|
37 |
### R code:"""
|
38 |
+
|
39 |
+
# Tokenize input with truncation
|
40 |
+
input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids
|
41 |
+
|
42 |
+
# Move input to GPU if available
|
43 |
+
if torch.cuda.is_available():
|
44 |
+
input_ids = input_ids.to("cuda")
|
45 |
+
|
46 |
+
# Generate output
|
47 |
+
outputs = model.generate(
|
48 |
+
input_ids,
|
49 |
+
max_length=1024,
|
50 |
+
do_sample=True,
|
51 |
+
temperature=0.2,
|
52 |
+
pad_token_id=tokenizer.eos_token_id,
|
53 |
+
num_return_sequences=1
|
54 |
)
|
55 |
+
|
56 |
+
# Decode the generated output
|
57 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
58 |
+
|
59 |
+
# Remove the prompt part from output, keep only R code
|
60 |
+
# Optional: Try to split from "### R code:"
|
61 |
+
if "### R code:" in generated_text:
|
62 |
+
generated_text = generated_text.split("### R code:")[-1].strip()
|
63 |
+
|
64 |
+
return generated_text
|
65 |
|
66 |
# Gradio Interface
|
67 |
gr.Interface(
|
68 |
fn=convert_python_to_r,
|
69 |
inputs=gr.Textbox(lines=10, placeholder="Paste your Python code here..."),
|
70 |
outputs="text",
|
71 |
+
title="Python to R Code Converter using CodeLlama 7B Instruct",
|
72 |
+
description="Enter Python code below, and the tool will convert it to R code using the CodeLlama 7B Instruct model."
|
73 |
).launch()
|
74 |
+
|