subham1707 commited on
Commit
737641a
Β·
1 Parent(s): bb441f2

application deployed

Browse files
Files changed (1) hide show
  1. app.py +41 -17
app.py CHANGED
@@ -9,19 +9,25 @@
9
  #######################
10
 
11
  import gradio as gr
12
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
13
 
 
14
  model_id = "codellama/CodeLlama-7b-Instruct-hf"
15
 
16
- # Load the tokenizer and model
17
  tokenizer = AutoTokenizer.from_pretrained(model_id)
18
- model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
 
 
 
 
19
 
20
- # Use text-generation pipeline
21
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
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
- #result = generator(prompt, max_length=1024, temperature=0.2, num_return_sequences=1)
33
- result = generator(
34
- prompt,
35
- max_length=1024,
36
- temperature=0.2,
37
- num_return_sequences=1,
38
- do_sample=True, # <-- Add this!
39
- pad_token_id=tokenizer.eos_token_id # <-- Safely set pad_token_id
 
 
 
 
 
 
 
 
40
  )
41
- return result[0]['generated_text']
 
 
 
 
 
 
 
 
 
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 this tool will convert it into equivalent R code using CodeLlama Instruct model."
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
+