File size: 1,409 Bytes
086aec6
8ab3f99
 
 
086aec6
8ab3f99
 
 
086aec6
8ab3f99
 
086aec6
8ab3f99
086aec6
8ab3f99
 
 
 
 
 
 
086aec6
8ab3f99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
086aec6
8ab3f99
 
086aec6
 
8ab3f99
 
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
import gradio as gr
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
import os

# Hugging Face Hub上のモデルを指定
repo_id = "mmnga/ELYZA-japanese-Llama-2-7b-instruct-gguf"
filename = "ELYZA-japanese-Llama-2-7b-instruct-q4_K_M.gguf"

# モデルをダウンロード(キャッシュされている場合はキャッシュを使用)
model_path = hf_hub_download(repo_id=repo_id, filename=filename)

CONTEXT_SIZE = 4096

llm = Llama(
    model_path=model_path,
    n_threads=os.cpu_count(),
    n_batch=32,
    verbose=False,
    n_ctx=CONTEXT_SIZE,
)

def get_llama_response(prompt):
    return llm(prompt, max_tokens=2048, temperature=0.7, top_p=0.95, repeat_penalty=1.1, stream=True)

def greet(prompt, intensity):
    full_response = ""
    for output in get_llama_response(prompt):
        if len(output['choices']) > 0:
            text_chunk = output['choices'][0]['text']
            full_response += text_chunk
            yield full_response
    
    return full_response + "!" * int(intensity)

demo = gr.Interface(
    title="Llama.cpp-python-sample (Streaming)",
    description=f"MODEL: {filename} from {repo_id}",
    fn=greet,
    inputs=[
        gr.Textbox(label="Enter your prompt"),
        gr.Slider(minimum=0, maximum=10, step=1, label="Intensity")
    ],
    outputs=gr.Textbox(label="Generated Response"),
    live=False
)

demo.queue()
demo.launch()