-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
66 lines (54 loc) · 1.36 KB
/
webapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sys
import torch
import time
import json
import gradio as gr
from typing import Tuple
from pathlib import Path
from llama import Llama, ModelArgs, Transformer, Tokenizer
ckpt_dir = "llama_xb_model_path"
tokenizer_path = "tokenizer.model"
temperature = 0.8
top_p = 0.8
max_seq_len = 512
max_batch_size = 4
def load(
ckpt_dir: str,
tokenizer_path: str,
max_seq_len: int,
max_batch_size: int,
):
generator = Llama.build(
ckpt_dir=ckpt_dir,
tokenizer_path=tokenizer_path,
max_seq_len=max_seq_len,
max_batch_size=max_batch_size,
)
return generator
generator = load(
ckpt_dir, tokenizer_path, max_seq_len, max_batch_size
)
def process(prompt: str):
dialogs = [
[
{"role": "system", "content": "AI"},
{"role": "user", "content": prompt}
],
]
results = generator.chat_completion(
dialogs, # type: ignore
max_gen_len=256,
temperature=temperature,
top_p=top_p,
)
return str(results[0]["generation"]["content"])
demo = gr.Interface(
title="极简Llama2问答对话",
description="还没有做成连续对话,虽然它可以",
article="基于Llama2",
fn = process,
inputs = gr.Textbox(lines=10, placeholder="请输入。。。", label="用户输入"),
outputs = "text",
)
demo.launch(share=False)