1earner1 commited on
Commit
0df2e4b
·
verified ·
1 Parent(s): 4880ea2

Upload try_starcoder.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. try_starcoder.py +49 -0
try_starcoder.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ # Use a pipeline as a high-level helper
4
+ #from transformers import pipeline
5
+ #from transformers import pipeline
6
+
7
+
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+
11
+
12
+
13
+ # Load model directly
14
+ from transformers import AutoTokenizer, AutoModelForCausalLM
15
+ # Load model directly
16
+ # Load model directly
17
+ from transformers import AutoTokenizer, AutoModelForCausalLM
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
20
+ model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
21
+ #tokenizer = AutoTokenizer.from_pretrained("bigcode/starcoderbase-3b")
22
+ #model = AutoModelForCausalLM.from_pretrained("bigcode/starcoderbase-3b")
23
+ #tokenizer = AutoTokenizer.from_pretrained("bigcode/santacoder", trust_remote_code=True)
24
+ #model = AutoModelForCausalLM.from_pretrained("bigcode/santacoder", trust_remote_code=True, attn_implementation="flash_attention_2")
25
+
26
+ #pipe = pipeline("text-generation", model=model, trust_remote_code=True, device=0 if torch.cuda.is_available() else -1)
27
+ #pipe = pipeline("text-generation", model=model, device=0 if torch.cuda.is_available() else -1)
28
+
29
+ prompt = """ Given two programs, check if they both solve the same problem or not i.e. for any given input, would they both give the same output or not. Think step by step and give the final answer as either 'Yes' or 'No'.
30
+ Program 1: "\nimport java.util.ArrayList;\nimport java.util.Scanner;\n\nimport java.util.Collections;\nimport java.util.Comparator;\n\npublic class Solution {\n\n public static void main(String args[]) {\n\n Scanner sc = new Scanner(System.in);\n int n = sc.nextInt();\n sc.nextLine();\n int a[] = new int[n];\n int odd = 0;\n ArrayList<Integer> od = new ArrayList<Integer>();\n ArrayList<Integer> ev = new ArrayList<Integer>();\n int even = 0;\n int max = 0;\n int maxi = 0;\n long sum = 0;\n\n for (int i = 0; i < n; i++) {\n a[i] = sc.nextInt();\n if (a[i] % 2 == 0) {\n even++;\n ev.add(a[i]);\n } else {\n odd++;\n od.add(a[i]);\n }\n\n if (a[i] > max) {\n max = a[i];\n maxi = i;\n }\n\n sum = sum + a[i];\n\n }\n\n int fodd = odd;\n int feven = even;\n Comparator r = Collections.reverseOrder();\n Collections.sort(ev, r);\n\n Collections.sort(od, r);\n\n \/\/ System.out.println(ev);\n \/\/ System.out.println(od);\n if (odd - even == 1 || odd - even == 0) {\n System.out.println(\"0\");\n } else if (even == 0 || odd == 0) {\n\n System.out.println(sum - max);\n } else {\n\n int o = 0;\n int e = 0;\n long deleted = 0;\n\n int turn = 1;\n\n\/\/ if (od.get(0) > ev.get(0)) {\n\/\/ \n\/\/ System.out.println(od.get(0) + \" deleted first odd id \");\n\/\/ deleted += od.get(0);\n\/\/ o++;\n\/\/ odd--;\n\/\/ turn = 2;\n\/\/\n\/\/ } else {\n\/\/ System.out.println(ev.get(0) + \" deleted firt evenid \");\n\/\/ deleted += ev.get(e);\n\/\/ e++;\n\/\/ even--;\n\/\/ turn = 1;\n\/\/\n\/\/ }\n while (odd > 0 && even > 0) {\n\n if (turn == 1) {\n \/\/ System.out.println(od.get(0) + \" deleted first odd id \");\n\n deleted += od.get(o);\n o++;\n odd--;\n turn = 2;\n\n } else if (turn == 2) {\n \/\/ System.out.println(ev.get(0) + \" deleted firt evenid \");\n deleted += ev.get(e);\n e++;\n even--;\n turn = 1;\n\n }\n\n }\n\n if (odd == 0) {\n deleted += ev.get(e);\n } else if (even == 0) {\n\n deleted += od.get(o);\n }\n\n \/\/-------------------------------------------------------------------------------------------\n turn = 2;\n o = 0;\n e = 0;\n odd = fodd;\n even = feven;\n long deleted2 = 0;\n while (odd > 0 && even > 0) {\n\n if (turn == 1) {\n \/\/ System.out.println(od.get(0) + \" deleted first odd id \");\n\n deleted2 += od.get(o);\n o++;\n odd--;\n turn = 2;\n\n } else if (turn == 2) {\n \/\/ System.out.println(ev.get(0) + \" deleted firt evenid \");\n deleted2 += ev.get(e);\n e++;\n even--;\n turn = 1;\n\n }\n\n }\n\n if (odd == 0) {\n deleted2 += ev.get(e);\n } else if (even == 0) {\n\n deleted2 += od.get(o);\n }\n\n if (deleted2 > deleted) {\n System.out.println(sum - deleted2);\n } else {\n System.out.println(sum - deleted);\n }\n }\n\n }\n}\n"
31
+ Program 2: "n=int(input())\narr=list(map(int,input().split()))\narr.sort()\neven=[]\nodd=[]\ne=0\no=0\nfor i in arr:\n\tif (i%2)==0:\n\t\teven=even+[i]\n\t\te=e+1\n\telse:\n\t\todd=odd+[i]\n\t\to=o+1\nif (e>o) and (e-o)>1:\n\tprint(sum(even[:(e-o-1)]))\nelif (o>e) and (o-e)>1:\n\tprint(sum(odd[:(o-e-1)]))\t\nelse:\n\tprint(0)\n\t\n\t\t\n\t\t\n\t\n\n\t \n
32
+ Answer:"""
33
+ prompt = """ Given Java program, translate it to python program.
34
+ Java Program: "\/\/package javaapplication1;\n\/*\n * To change this license header, choose License Headers in Project Properties.\n * To change this template file, choose Tools | Templates\n * and open the template in the editor.\n *\/\nimport java.io.*;\nimport java.util.*;\npublic class Main {\n public static void main(String[] args) throws IOException{\n \/\/BufferedReader br=new BufferedReader(new InputStreamReader(System.in));\n PrintWriter pw=new PrintWriter(new PrintStream(System.out));\n \/\/Scanner sc=new Scanner(System.in);\n FastScanner in=new FastScanner();\n String x=in.nextToken();\n String z=in.nextToken();\n char [] ans=new char[x.length()];\n for (int i = 0; i < x.length(); ++i) {\n if(z.charAt(i)<x.charAt(i))\n ans[i]=z.charAt(i);\n else if(z.charAt(i)>x.charAt(i)){pw.println(-1);pw.flush();System.exit(0);}\n else \n ans[i]=x.charAt(i);\n }\n pw.println(String.valueOf(ans));pw.flush();\n }\n public static class FastScanner\n\t{\n\t\tBufferedReader br;\n\t\tStringTokenizer st;\n\n\t\tpublic FastScanner(String s)\n\t\t{\n\t\t\ttry {\n\t\t\t\tbr = new BufferedReader(new FileReader(s));\n\t\t\t} catch (FileNotFoundException e) {\n\t\t\t\t\/\/ TODO Auto-generated catch block\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\n\t\tpublic FastScanner()\n\t\t{\n\t\t\tbr = new BufferedReader(new InputStreamReader(System.in));\n\t\t}\n\n\t\tString nextToken()\n\t\t{\n\t\t\twhile (st == null || !st.hasMoreElements())\n\t\t\t{\n\t\t\t\ttry {\n\t\t\t\t\tst = new StringTokenizer(br.readLine());\n\t\t\t\t} catch (IOException e) {\n\t\t\t\t\t\/\/ TODO Auto-generated catch block\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn st.nextToken();\n\t\t}\n\n\t\tint nextInt()\n\t\t{\n\t\t\treturn Integer.parseInt(nextToken());\n\t\t}\n\n\t\tlong nextLong()\n\t\t{\n\t\t\treturn Long.parseLong(nextToken());\n\t\t}\n\n\t\tdouble nextDouble()\n\t\t{\n\t\t\treturn Double.parseDouble(nextToken());\n\t\t}\n\t\t\n\t\tString next()\n\t\t{\n\t\t\treturn nextToken();\n\t\t}\n\t}\n }\n"
35
+ Python Program: """
36
+
37
+ print(tokenizer.tokenize(prompt))
38
+ # Generate text using the pipeline
39
+ # generated_text = pipe(prompt, max_length=8192, num_return_sequences=1)[0]['generated_text']
40
+
41
+ # print("Generated Text:")
42
+ # print(generated_text)
43
+
44
+
45
+ inputs = tokenizer.encode(prompt, return_tensors="pt").to(device)
46
+ print(inputs)
47
+ outputs = model.generate(inputs, max_length=2048)
48
+ print(tokenizer.decode(outputs[0]))
49
+