diff --git a/cases/qwen.py b/cases/qwen.py new file mode 100644 index 00000000..eceeeae3 --- /dev/null +++ b/cases/qwen.py @@ -0,0 +1,37 @@ +from modelscope import AutoModelForCausalLM, AutoTokenizer +from modelscope import GenerationConfig + +# Note: The default behavior now has injection attack prevention off. +tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-14B-Chat", trust_remote_code=True) + +# use bf16 +# model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-14B-Chat", device_map="auto", trust_remote_code=True, bf16=True).eval() +# use fp16 +# model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-14B-Chat", device_map="auto", trust_remote_code=True, fp16=True).eval() +# use cpu only +# model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-14B-Chat", device_map="cpu", trust_remote_code=True).eval() +# use auto mode, automatically select precision based on the device. +model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-14B-Chat", device_map="auto", trust_remote_code=True).eval() + +# Specify hyperparameters for generation. But if you use transformers>=4.32.0, there is no need to do this. +# model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-14B-Chat", trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参 + +# 第一轮对话 1st dialogue turn +response, history = model.chat(tokenizer, "你好", history=None) +print(response) +# 你好!很高兴为你提供帮助。 + +# 第二轮对话 2nd dialogue turn +response, history = model.chat(tokenizer, "给我讲一个年轻人奋斗创业最终取得成功的故事。", history=history) +print(response) +# 这是一个关于一个年轻人奋斗创业最终取得成功的故事。 +# 故事的主人公叫李明,他来自一个普通的家庭,父母都是普通的工人。从小,李明就立下了一个目标:要成为一名成功的企业家。 +# 为了实现这个目标,李明勤奋学习,考上了大学。在大学期间,他积极参加各种创业比赛,获得了不少奖项。他还利用课余时间去实习,积累了宝贵的经验。 +# 毕业后,李明决定开始自己的创业之路。他开始寻找投资机会,但多次都被拒绝了。然而,他并没有放弃。他继续努力,不断改进自己的创业计划,并寻找新的投资机会。 +# 最终,李明成功地获得了一笔投资,开始了自己的创业之路。他成立了一家科技公司,专注于开发新型软件。在他的领导下,公司迅速发展起来,成为了一家成功的科技企业。 +# 李明的成功并不是偶然的。他勤奋、坚韧、勇于冒险,不断学习和改进自己。他的成功也证明了,只要努力奋斗,任何人都有可能取得成功。 + +# 第三轮对话 3rd dialogue turn +response, history = model.chat(tokenizer, "给这个故事起一个标题", history=history) +print(response) +# 《奋斗创业:一个年轻人的成功之路》 diff --git a/cases/test3.py b/cases/test3.py new file mode 100644 index 00000000..1f9768eb --- /dev/null +++ b/cases/test3.py @@ -0,0 +1,10 @@ + +def invert_dictionary(obj): + return { value: key for key, value in obj.items() } + +ages = { + 'Peter': 10, + 'Isabel': 11, + 'Anna': 9, +} +invert_dictionary(ages) # { 10: 'Peter', 11: 'Isabel', 9: 'Anna' } diff --git a/cases/unsupported/cmp2.py b/cases/unsupported/cmp2.py new file mode 100644 index 00000000..84958179 --- /dev/null +++ b/cases/unsupported/cmp2.py @@ -0,0 +1,8 @@ + +def in_range(n, start, end = 0): + return start <= n <= end if end >= start else end <= n <= start + +in_range(3, 2, 5) # True +in_range(3, 4) # True +in_range(2, 3, 5) # False +in_range(3, 2) # False diff --git a/cases/unsupported/gen.py b/cases/unsupported/gen.py new file mode 100644 index 00000000..526cb952 --- /dev/null +++ b/cases/unsupported/gen.py @@ -0,0 +1,5 @@ + +def find_last_index(lst, fn): + return len(lst) - 1 - next(i for i, x in enumerate(lst[::-1]) if fn(x)) + +find_last_index([1, 2, 3, 4], lambda n: n % 2 == 1) # 2 diff --git a/cases/unsupported/gen2.py b/cases/unsupported/gen2.py new file mode 100644 index 00000000..c8577f4e --- /dev/null +++ b/cases/unsupported/gen2.py @@ -0,0 +1,11 @@ + +from collections import Counter + +def is_anagram(s1, s2): + return Counter( + c.lower() for c in s1 if c.isalnum() + ) == Counter( + c.lower() for c in s2 if c.isalnum() + ) + +is_anagram('#anagram', 'Nag a ram!') # True diff --git a/cases/unsupported/gen3.py b/cases/unsupported/gen3.py new file mode 100644 index 00000000..8f912c5e --- /dev/null +++ b/cases/unsupported/gen3.py @@ -0,0 +1,6 @@ + +def none(lst, fn = lambda x: x): + return all(not fn(x) for x in lst) + +none([0, 1, 2, 0], lambda x: x >= 2 ) # False +none([0, 0, 0]) # True diff --git a/cases/unsupported/gen4.py b/cases/unsupported/gen4.py new file mode 100644 index 00000000..74b81118 --- /dev/null +++ b/cases/unsupported/gen4.py @@ -0,0 +1,5 @@ + +def weighted_average(nums, weights): + return sum(x * y for x, y in zip(nums, weights)) / sum(weights) + +weighted_average([1, 2, 3], [0.6, 0.2, 0.3]) # 1.72727 diff --git a/cases/unsupported/star.py b/cases/unsupported/star.py new file mode 100644 index 00000000..55296376 --- /dev/null +++ b/cases/unsupported/star.py @@ -0,0 +1,8 @@ + +from time import sleep + +def delay(fn, ms, *args): + sleep(ms / 1000) + return fn(*args) + +delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second diff --git a/cases/unsupported/star2.py b/cases/unsupported/star2.py new file mode 100644 index 00000000..7f9b012a --- /dev/null +++ b/cases/unsupported/star2.py @@ -0,0 +1,6 @@ + +def transpose(lst): + return list(zip(*lst)) + +transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) +# [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] diff --git a/cases/unsupported/star3.py b/cases/unsupported/star3.py new file mode 100644 index 00000000..af26c7cd --- /dev/null +++ b/cases/unsupported/star3.py @@ -0,0 +1,9 @@ + +from functools import partial + +def curry(fn, *args): + return partial(fn, *args) + +add = lambda x, y: x + y +add10 = curry(add, 10) +add10(20) # 30 diff --git a/conv.php b/conv.php index f730d7bb..5e8dae87 100644 --- a/conv.php +++ b/conv.php @@ -126,6 +126,8 @@ class Translator return $this->parseCall($attr); case 'Subscript': return $this->parseSubscript($attr); + case 'BinOp': + return $this->parseBinOp($attr); default: var_dump(__METHOD__, __LINE__); debug($attr); @@ -165,6 +167,7 @@ class Translator foreach ($args as $arg) { $args_list[] = $this->parseValue($arg); } + foreach ($kwargs as $arg) { $name = $arg->arg; $args_list[] = $name . ': ' . $this->parseValue($arg->value); @@ -226,7 +229,7 @@ class Translator $code = ''; $this->indentLevel++; $code .= $this->getIndent() . '$___ = [];' . PHP_EOL; - $recipient = '$___[]'; + $recipient = '$___'; $generators = $this->parseGenerators($listComp->generators, $recipient, $captures); $code .= $this->getIndent() . $generators; $code .= $this->getIndent() . 'return $___;' . PHP_EOL; @@ -363,6 +366,8 @@ class Translator return $this->parseSlice($target); case 'BoolOp': return $this->parseBoolOp($target); + case 'ListComp': + return $this->parseListComp($target); default: debug($target); break; @@ -482,6 +487,8 @@ class Translator return $this->parseDict($iter); case 'Name': return $this->parseName($iter); + case 'Subscript': + return $this->parseSubscript($iter); default: debug($iter); } @@ -616,6 +623,18 @@ class Translator case 'BinOp': $line = $this->parseBinOp($node) . ';'; break; + case 'IfExp': + $line = $this->parseIfExp($node) . ';'; + break; + case 'Tuple': + $line = $this->parseTuple($node) . ';'; + break; + case 'Lambda': + $line = $this->parseLambda($node) . ';'; + break; + case 'Call': + $line = $this->parseCall($node) . ';'; + break; default: debug($node); $line = ''; @@ -649,19 +668,28 @@ class Translator private function parseGenerators($generators, $recipient, &$captures) { - $code = '$elts = [];' . PHP_EOL; + $captures = []; + $code = ''; + foreach ($generators as $k => $generator) { $target = $this->parseTarget($generator->target); - $iter = $generator->iter->id; - $name = '$' . $iter; - $captures[] = $name; - $code .= $this->getIndent() . 'foreach(' . $name . ' as ' . $target . ') {' . PHP_EOL; + if ($generator->iter->_type == 'Name') { + $name = '$' . $generator->iter->id; + $captures[] = $name; + } else { + $name = '$___iter'; + $code .= $this->getIndent() . '$___iter = ' . $this->parseIter($generator->iter) . ';' . PHP_EOL; + } + $code .= $this->getIndent() . 'foreach(' . $name . ' as $___i => ' . $target . ') {' . PHP_EOL; $this->indentLevel++; - $code .= $this->getIndent() . '$elts[' . $k . '] = ' . $target . ';' . PHP_EOL; + if (count($generators) === 1) { + $code .= $this->getIndent() . $recipient . '[] = ' . $target . ';' . PHP_EOL; + } else { + $code .= $this->getIndent() . $recipient . '[$___i][' . $k . '] = ' . $target . ';' . PHP_EOL; + } $this->indentLevel--; $code .= $this->getIndent() . '}' . PHP_EOL; } - $code .= $this->getIndent() . $recipient . ' = $elts;' . PHP_EOL;; return $code; } @@ -817,7 +845,7 @@ class Translator private function parseWhile(mixed $node) { $test = $this->parseTest($node->test); - $code = $this->getIndent() . 'while(' . $test . ') {' . PHP_EOL; + $code = 'while(' . $test . ') {' . PHP_EOL; $this->indentLevel++; $code .= $this->parseBody($node->body); $this->indentLevel--; @@ -914,12 +942,10 @@ class Translator $code = 'function (' . $args . ') {' . PHP_EOL; $this->indentLevel++; $this->parseLine($value->body, $lines); + $code .= $this->getIndent() . 'return ' . $lines[0] . PHP_EOL; $this->indentLevel--; - if (count($lines) != 1) { - debug($value); - } - $code = 'return ' . $lines[0] . PHP_EOL; - return $code . PHP_EOL . $this->getIndent() . '}'; + $code .= $this->getIndent() . '}'; + return $code; } private function parseFormattedValue($value)