subham1707 commited on
Commit
62a097b
·
verified ·
1 Parent(s): 2778cc6

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +15 -0
  2. app.py +55 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ # Set up environment
4
+ WORKDIR /code
5
+ COPY . /code
6
+
7
+ # Install dependencies
8
+ RUN pip install --upgrade pip
9
+ RUN pip install gradio==3.50.2 transformers torch accelerate
10
+
11
+ # Expose the default Gradio port
12
+ EXPOSE 7860
13
+
14
+ # Run the app
15
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import spaces
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ import torch
6
+
7
+ @spaces.GPU
8
+ def main():
9
+ # Optional: force install gradio 3.50.2 to avoid node issues
10
+ os.system("pip install gradio==3.50.2")
11
+
12
+ # Load model and tokenizer
13
+ model_id = "codellama/CodeLlama-7b-Instruct-hf"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ model_id,
17
+ device_map="auto",
18
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
19
+ )
20
+ tokenizer.pad_token = tokenizer.eos_token
21
+
22
+ def convert_python_to_r(python_code):
23
+ prompt = f"""### Task:
24
+ Convert the following Python code to equivalent R code.
25
+
26
+ ### Python code:
27
+ {python_code}
28
+
29
+ ### R code:"""
30
+ input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids
31
+ if torch.cuda.is_available():
32
+ input_ids = input_ids.to("cuda")
33
+ outputs = model.generate(
34
+ input_ids,
35
+ max_length=1024,
36
+ do_sample=True,
37
+ temperature=0.2,
38
+ pad_token_id=tokenizer.eos_token_id,
39
+ num_return_sequences=1
40
+ )
41
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
42
+ if "### R code:" in generated_text:
43
+ generated_text = generated_text.split("### R code:")[-1].strip()
44
+ return generated_text
45
+
46
+ gr.Interface(
47
+ fn=convert_python_to_r,
48
+ inputs=gr.Textbox(lines=10, placeholder="Paste your Python code here..."),
49
+ outputs="text",
50
+ title="Python to R Code Converter using CodeLlama 7B Instruct",
51
+ description="Enter Python code below, and the tool will convert it to R code using the CodeLlama 7B Instruct model."
52
+ ).launch()
53
+
54
+ if __name__ == "__main__":
55
+ main()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio==3.50.2
4
+ accelerate