Skip to content

๐Ÿค—transformers ์‚ฌ์šฉํ•˜์—ฌ QG ๋ชจ๋ธ ๋ถ€ํŠธ & flask-restful ์”Œ์šฐ๋Š” ์˜ˆ์ œ. ํ™•์žฅ ์„ค๊ณ„ ์—†์Œ.

Notifications You must be signed in to change notification settings

kuPacemaker/flask-modelboot-qg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

49 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

gpt2-qg-boot

qg์— ๊ฐ•(ๅผท)์˜์กดํ•˜๋Š” ์ž…์ถœ๋ ฅ ์–ด๋Œ‘ํ„ฐ

๋ฌธ์ œ ์ •์˜

์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ์ด ์žˆ๋‹ค. ๋ชจ๋ธ์€ python ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์‹คํ–‰๋˜๋ฉฐ, ์ž์—ฐ์–ด๊ฐ€ ๋ฐฐ์น˜๋กœ ์ž…๋ ฅ๋˜์–ด์„œ ๊ทธ์— ๋Œ€ํ•œ ๋ฐฐ์น˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

[python ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— UniLMv1์„ ์ ์žฌํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค]

python biunilm/decode_seq2seq.py --bert_model bert-large-cased --new_segment_ids --mode s2s \\
  --input_file ${DATA_DIR}/dev.pq.txt --split ${EVAL_SPLIT} \\
  --model_recover_path ${MODEL_RECOVER_PATH} \\
  --max_seq_length 512 --max_tgt_length 48 \\
  --batch_size 16 --beam_size 1 --length_penalty 0

์˜ˆ์‹œ๋Š” ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํŒŒ์ธํŠ ๋“œ ๋ชจ๋ธ: UniLMv1์ด๋‹ค.

๋ณด์•˜๋“ฏ์ด biunilm/decode_seq2seq.py์˜ ์ธํ„ฐํŽ˜์ด์Šค๋Š” args๋กœ ์ „๋‹ฌ๋œ ํ…์ŠคํŠธ ํŒŒ์ผ์—์„œ ์ž์—ฐ์–ด๋ฅผ ์ฝ์–ด๋“ค์ด๊ณ , ์—ฐ์‚ฐํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒ๋˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค.

์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์„œ๋น„์Šค์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋ชจ๋ธ์˜ ์ ์žฌ ์†๋„๋Š” ๋„ˆ๋ฌด ๋Š๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ ์š”์ฒญ๋งˆ๋‹ค ์ ์žฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ผ์€ ์—†์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ฐฐ์น˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋ฐฐ์น˜ ์ถœ๋ ฅ์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ,

  1. ์ด ๋ชจ๋ธ์„ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์ ์žฌํ•˜๊ณ 
  2. ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ง€์†ํ•˜๋„๋ก ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์ด ์š”๊ตฌ์‚ฌํ•ญ์„ ๋งŒ์กฑ์‹œํ‚ค๋Š” ์–ด๋Œ‘ํ„ฐ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ ์ฝ”๋“œ(.py)๋ฅผ ์ตœ์†Œํ•œ์œผ๋กœ ์ˆ˜์ •ํ•˜์—ฌ ๊ฒฐํ•ฉํ•˜์‹œ์˜ค.

๋„๋ฉ”์ธ ์ •์˜: ์ž์—ฐ์–ด์ฒ˜๋ฆฌ ์„œ๋น„์Šค

์ž์—ฐ์–ด์ฒ˜๋ฆฌ ์„œ๋น„์Šค๋ž€, ์ด์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž์—ฐ์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ž…๋ ฅ์„ ์ธ๊ณต์ง€๋Šฅ ๋ชจ๋ธ์— ์ „ํŒŒํ•˜์—ฌ ๋„์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์ด์šฉ์ž์—๊ฒŒ ๋˜๋Œ๋ ค ์ฃผ๋Š” ์„œ๋น„์Šค์ด๋‹ค.

์ฃผ์ฒด ๋ฐ ์—ญํ• 

  • ์ด์šฉ์ž: ๋ชจ๋ธ์— ์ž…๋ ฅํ•˜๊ณ ํ”ˆ ์ž์—ฐ์–ด๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ž์—ฐ์–ด์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค ๋ฐ›๋Š”๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ ์•ฑ: ์ด์šฉ์ž์™€ ๋งˆ์ฃผํ•˜๋Š” ์–ผ๊ตด ๋งˆ๋‹ด์ด๋‹ค. ์ด์šฉ์ž๋Š” ์ž์—ฐ์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ด ์ž…๋ ฅ์€ ์–ด๋Œ‘ํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ์–ด ํด๋ผ์ด์–ธํŠธ ์•ฑ์ด ์–ด๋Œ‘ํ„ฐ์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค. ์ดํ›„ ์–ด๋Œ‘ํ„ฐ๊ฐ€ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด, ์ด๊ฒƒ์„ ์ด์šฉ์ž๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ์–ด ๋‹ค์‹œ ์ด์šฉ์ž์—๊ฒŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ์–ด๋Œ‘ํ„ฐ: ํด๋ผ์ด์–ธํŠธ ์•ฑ๊ณผ ์–ด๋Œ‘ํ‹ฐ ๋ชจ๋ธ ์‚ฌ์ด์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ์„ ๋ž˜ํ•‘ํ•˜์—ฌ ์ด ๋ชจ๋ธ์ด ์ ์žฌ ์ดํ›„ ์ง€์†์ ์ธ '์ž์—ฐ์–ด ์ž…๋ ฅ- ๋ชจ๋ธ ์ „ํŒŒ - ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜' ํ”„๋กœ์„ธ์Šค๋กœ ์‹คํ–‰๋˜๋„๋ก ํ•œ๋‹ค.
  • ์ž์—ฐ์–ด์ฒ˜๋ฆฌ ๋ชจ๋ธ: ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ์ž์—ฐ์–ด๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ , ๋„คํŠธ์›Œํฌ์— ์ด๋ฅผ ์ „ํŒŒํ•˜์—ฌ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“œ๋Š” ๋ชจ๋“ˆ์ด๋‹ค.
  • ์–ด๋Œ‘ํ‹ฐ ๋ชจ๋ธ: ์–ด๋Œ‘ํ„ฐ์™€ ์ƒํ˜ธ์ž‘์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ธํ„ฐํŽ˜์ด์Šค ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ตฌํ˜„ํ•œ, "์ˆ˜์ •๋œ ์ž์—ฐ์–ด์ฒ˜๋ฆฌ ๋ชจ๋ธ"์ด๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค ์ •์˜

์ด ํ”„๋กœ์ ํŠธ๋Š” Question Generation ํƒœ์Šคํฌ์— ํ•œ์ •ํ•˜์—ฌ ํ™•์žฅ์„ฑ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค.

์ด์šฉ์ž - ํด๋ผ์ด์–ธํŠธ ์•ฑ

GET http://localhost/qg/{baseKnowledge}

์ž์—ฐ์–ด ๋‹จ๋ฝ baseKnowledge ์— ๋Œ€ํ•œ ์งˆ๋ฌธ ์ƒ์„ฑ์„ ์š”์ฒญํ•œ๋‹ค.

์ƒ์„ฑ๋œ ์งˆ๋ฌธ์€ JSON bkd ์™€ q ๋กœ ์ด๋ค„์ง„ ๊ฐ์ฒด์ด๋‹ค.

{
  "bkd": "์งˆ๋ฌธ ์ƒ์„ฑ์— ์‚ฌ์šฉํ•  ๋‹จ๋ฝ",
  "q": "๋ชจ๋ธ์ด ์ƒ์„ฑํ•œ ์งˆ๋ฌธ"
}

{
  "bkd": "Well, but I still wanna ask: 'How was your day?'",
  "q": "Why do you think I still want to say hello to?"
}

ํด๋ผ์ด์–ธํŠธ ์•ฑ - ์–ด๋Œ‘ํ„ฐ

ํด๋ผ์ด์–ธํŠธ ์•ฑ๊ณผ ์–ด๋Œ‘ํ„ฐ๊ฐ€ Java๋กœ ๊ตฌํ˜„๋  ๊ฒฝ์šฐ ๋ช…์„ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

@Controller
class QuestionGenerationController {
  private final NLPModel model;
  
  @GetMapping("/qg/{baseKnowledge}")
  public QuestionGenerationResponseDto questionGeneration(@RequestParam String baseKnowledge) {
    return new QuestionGenerationResponseDto(baseKnowledge, model.offer(baseKnowledge));
  }
}

interface NLPModel {
  public void boot();
  public String offer(String message);
  public List<String> offer(List<String> message);
  public void close();
} 

์–ด๋Œ‘ํ„ฐ - ์–ด๋Œ‘ํ‹ฐ ๋ชจ๋ธ

์–ด๋Œ‘ํ„ฐ์™€ ์–ด๋Œ‘ํ‹ฐ ๋ชจ๋ธ์€ HTTP REST ํ˜•์‹์˜ ํ†ต์‹ ์„ ํ•œ๋‹ค.

abstract class DefaultModelAdapter implements ModelAdapter<ByteArray> {

  private final PythonInterpreter process;
  private final RestIO restio;
  private final Queue<String> resultQueue;

  @Override
  public void boot(){...}

  @Override
  public void write(String message) {
    JSONObject response = restio.send(message);
    resultQueue.offer(resopnse.get("question"));
  }

  @Override
  public ByteArray read() {
    ...
    return resultQueue.pop();
  }
}
[์–ด๋Œ‘ํ‹ฐ ๋ชจ๋ธ ๊ฐœ๋žต ๊ตฌ์กฐ]
Flask์•ฑ:
  1. ์˜์กด ๋ชจ๋ธ ๋ถ€ํŒ… (huggingface Auto API)
  2. REST ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
  3. WebService๋กœ์„œ ์‹คํ–‰

์ถ”๊ฐ€ ์ •๋ณด

๋งˆ๊ฐ์ผ

~21.07.17 ><;;;

๋ชจ๋ธ ์˜์กด ๊ณ„ํš

2๊ฐ€์ง€ GPT2 ๋ชจ๋ธ์„ ๋ž˜ํ•‘ํ•ด ๋ณผ ๊ฒƒ์„ ๊ถŒ๊ณ ํ•œ๋‹ค.

PA ๋ชจ๋ธ ํ…Œ์ŠคํŠธ ์‘๋‹ต

image

T5 Prepend ๋ชจ๋ธ ํ…Œ์ŠคํŠธ ์‘๋‹ต

  • ์งง์€ ๋‹จ๋ฝ๋“ค์—” ์ „ํ˜€ ๋‚ฉ๋“ํ•  ์ˆ˜ ์—†๋Š” ์งˆ๋ฌธ๋“ค์ด ์ƒ์„ฑ image
  • ์ฐฝ์„ธ๊ธฐ 1์žฅ (๊ธด ๋‹จ๋ฝ) ์˜ค ์ข€ ๋” ์—ฐ๊ตฌํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค image

About

๐Ÿค—transformers ์‚ฌ์šฉํ•˜์—ฌ QG ๋ชจ๋ธ ๋ถ€ํŠธ & flask-restful ์”Œ์šฐ๋Š” ์˜ˆ์ œ. ํ™•์žฅ ์„ค๊ณ„ ์—†์Œ.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages