Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("AXCXEPT/llm-jp-3-3.7b-instruct-EZO-Humanities")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Hugging Face Hub上のモデルを指定
|
| 7 |
+
repo_id = "mmnga/ELYZA-japanese-Llama-2-7b-instruct-gguf"
|
| 8 |
+
filename = "ELYZA-japanese-Llama-2-7b-instruct-q4_K_M.gguf"
|
|
|
|
| 9 |
|
| 10 |
+
# モデルをダウンロード(キャッシュされている場合はキャッシュを使用)
|
| 11 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 12 |
|
| 13 |
+
CONTEXT_SIZE = 4096
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
llm = Llama(
|
| 16 |
+
model_path=model_path,
|
| 17 |
+
n_threads=os.cpu_count(),
|
| 18 |
+
n_batch=32,
|
| 19 |
+
verbose=False,
|
| 20 |
+
n_ctx=CONTEXT_SIZE,
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
def get_llama_response(prompt):
|
| 24 |
+
return llm(prompt, max_tokens=2048, temperature=0.7, top_p=0.95, repeat_penalty=1.1, stream=True)
|
| 25 |
+
|
| 26 |
+
def greet(prompt, intensity):
|
| 27 |
+
full_response = ""
|
| 28 |
+
for output in get_llama_response(prompt):
|
| 29 |
+
if len(output['choices']) > 0:
|
| 30 |
+
text_chunk = output['choices'][0]['text']
|
| 31 |
+
full_response += text_chunk
|
| 32 |
+
yield full_response
|
| 33 |
+
|
| 34 |
+
return full_response + "!" * int(intensity)
|
| 35 |
+
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
title="Llama.cpp-python-sample (Streaming)",
|
| 38 |
+
description=f"MODEL: {filename} from {repo_id}",
|
| 39 |
+
fn=greet,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(label="Enter your prompt"),
|
| 42 |
+
gr.Slider(minimum=0, maximum=10, step=1, label="Intensity")
|
| 43 |
],
|
| 44 |
+
outputs=gr.Textbox(label="Generated Response"),
|
| 45 |
+
live=False
|
| 46 |
)
|
| 47 |
|
| 48 |
+
demo.queue()
|
| 49 |
+
demo.launch()
|
|
|