pull/1/head
韩天峰 3 years ago
parent 034c09c8a2
commit 166d6faa9e
  1. 37
      cases/qwen.py
  2. 10
      cases/test3.py
  3. 8
      cases/unsupported/cmp2.py
  4. 5
      cases/unsupported/gen.py
  5. 11
      cases/unsupported/gen2.py
  6. 6
      cases/unsupported/gen3.py
  7. 5
      cases/unsupported/gen4.py
  8. 8
      cases/unsupported/star.py
  9. 6
      cases/unsupported/star2.py
  10. 9
      cases/unsupported/star3.py
  11. 54
      conv.php

@ -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)
# 《奋斗创业:一个年轻人的成功之路》

@ -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' }

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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)]

@ -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

@ -126,6 +126,8 @@ class Translator
return $this->parseCall($attr); return $this->parseCall($attr);
case 'Subscript': case 'Subscript':
return $this->parseSubscript($attr); return $this->parseSubscript($attr);
case 'BinOp':
return $this->parseBinOp($attr);
default: default:
var_dump(__METHOD__, __LINE__); var_dump(__METHOD__, __LINE__);
debug($attr); debug($attr);
@ -165,6 +167,7 @@ class Translator
foreach ($args as $arg) { foreach ($args as $arg) {
$args_list[] = $this->parseValue($arg); $args_list[] = $this->parseValue($arg);
} }
foreach ($kwargs as $arg) { foreach ($kwargs as $arg) {
$name = $arg->arg; $name = $arg->arg;
$args_list[] = $name . ': ' . $this->parseValue($arg->value); $args_list[] = $name . ': ' . $this->parseValue($arg->value);
@ -226,7 +229,7 @@ class Translator
$code = ''; $code = '';
$this->indentLevel++; $this->indentLevel++;
$code .= $this->getIndent() . '$___ = [];' . PHP_EOL; $code .= $this->getIndent() . '$___ = [];' . PHP_EOL;
$recipient = '$___[]'; $recipient = '$___';
$generators = $this->parseGenerators($listComp->generators, $recipient, $captures); $generators = $this->parseGenerators($listComp->generators, $recipient, $captures);
$code .= $this->getIndent() . $generators; $code .= $this->getIndent() . $generators;
$code .= $this->getIndent() . 'return $___;' . PHP_EOL; $code .= $this->getIndent() . 'return $___;' . PHP_EOL;
@ -363,6 +366,8 @@ class Translator
return $this->parseSlice($target); return $this->parseSlice($target);
case 'BoolOp': case 'BoolOp':
return $this->parseBoolOp($target); return $this->parseBoolOp($target);
case 'ListComp':
return $this->parseListComp($target);
default: default:
debug($target); debug($target);
break; break;
@ -482,6 +487,8 @@ class Translator
return $this->parseDict($iter); return $this->parseDict($iter);
case 'Name': case 'Name':
return $this->parseName($iter); return $this->parseName($iter);
case 'Subscript':
return $this->parseSubscript($iter);
default: default:
debug($iter); debug($iter);
} }
@ -616,6 +623,18 @@ class Translator
case 'BinOp': case 'BinOp':
$line = $this->parseBinOp($node) . ';'; $line = $this->parseBinOp($node) . ';';
break; 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: default:
debug($node); debug($node);
$line = ''; $line = '';
@ -649,19 +668,28 @@ class Translator
private function parseGenerators($generators, $recipient, &$captures) private function parseGenerators($generators, $recipient, &$captures)
{ {
$code = '$elts = [];' . PHP_EOL; $captures = [];
$code = '';
foreach ($generators as $k => $generator) { foreach ($generators as $k => $generator) {
$target = $this->parseTarget($generator->target); $target = $this->parseTarget($generator->target);
$iter = $generator->iter->id; if ($generator->iter->_type == 'Name') {
$name = '$' . $iter; $name = '$' . $generator->iter->id;
$captures[] = $name; $captures[] = $name;
$code .= $this->getIndent() . 'foreach(' . $name . ' as ' . $target . ') {' . PHP_EOL; } 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++; $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--; $this->indentLevel--;
$code .= $this->getIndent() . '}' . PHP_EOL; $code .= $this->getIndent() . '}' . PHP_EOL;
} }
$code .= $this->getIndent() . $recipient . ' = $elts;' . PHP_EOL;;
return $code; return $code;
} }
@ -817,7 +845,7 @@ class Translator
private function parseWhile(mixed $node) private function parseWhile(mixed $node)
{ {
$test = $this->parseTest($node->test); $test = $this->parseTest($node->test);
$code = $this->getIndent() . 'while(' . $test . ') {' . PHP_EOL; $code = 'while(' . $test . ') {' . PHP_EOL;
$this->indentLevel++; $this->indentLevel++;
$code .= $this->parseBody($node->body); $code .= $this->parseBody($node->body);
$this->indentLevel--; $this->indentLevel--;
@ -914,12 +942,10 @@ class Translator
$code = 'function (' . $args . ') {' . PHP_EOL; $code = 'function (' . $args . ') {' . PHP_EOL;
$this->indentLevel++; $this->indentLevel++;
$this->parseLine($value->body, $lines); $this->parseLine($value->body, $lines);
$code .= $this->getIndent() . 'return ' . $lines[0] . PHP_EOL;
$this->indentLevel--; $this->indentLevel--;
if (count($lines) != 1) { $code .= $this->getIndent() . '}';
debug($value); return $code;
}
$code = 'return ' . $lines[0] . PHP_EOL;
return $code . PHP_EOL . $this->getIndent() . '}';
} }
private function parseFormattedValue($value) private function parseFormattedValue($value)

Loading…
Cancel
Save