Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,7 @@ def load_model(progress=gr.Progress()):
|
|
19 |
progress(0, desc="モデルのダウンロードを開始")
|
20 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
21 |
progress(0.5, desc="モデルをメモリに読み込み中")
|
|
|
22 |
llm = Llama(
|
23 |
model_path=model_path,
|
24 |
n_threads=N_THREADS,
|
@@ -29,45 +30,48 @@ def load_model(progress=gr.Progress()):
|
|
29 |
progress(1, desc="モデルの読み込み完了")
|
30 |
model_loaded = True
|
31 |
return "モデルの読み込みが完了しました。"
|
|
|
32 |
|
33 |
-
def get_llama_response(prompt):
|
34 |
global llm, model_loaded
|
35 |
if not model_loaded:
|
36 |
return [{"choices": [{"text": "モデルを読み込んでいます。しばらくお待ちください..."}]}]
|
37 |
try:
|
38 |
-
return llm(prompt, max_tokens=1024, temperature=
|
39 |
except Exception as e:
|
40 |
return [{"choices": [{"text": f"エラーが発生しました: {str(e)}"}]}]
|
41 |
|
42 |
-
|
|
|
43 |
global model_loaded
|
44 |
if not model_loaded:
|
45 |
return "モデルを読み込んでいます。しばらくお待ちください..."
|
46 |
|
47 |
full_response = ""
|
48 |
-
for output in get_llama_response(prompt):
|
49 |
if len(output['choices']) > 0:
|
50 |
text_chunk = output['choices'][0]['text']
|
51 |
full_response += text_chunk
|
52 |
yield full_response
|
53 |
-
|
54 |
-
|
55 |
|
56 |
with gr.Blocks() as demo:
|
57 |
-
gr.Markdown("#
|
58 |
gr.Markdown(f"MODEL: {filename} from {repo_id}")
|
59 |
|
60 |
loading_status = gr.Textbox(label="Loading Status")
|
61 |
|
62 |
with gr.Row():
|
63 |
-
input_text = gr.Textbox(label="
|
64 |
-
|
65 |
|
66 |
-
output_text = gr.Textbox(label="
|
67 |
-
submit_button = gr.Button("
|
68 |
|
69 |
-
submit_button.click(fn=greet, inputs=[input_text,
|
70 |
demo.load(fn=load_model, outputs=loading_status)
|
71 |
|
|
|
72 |
demo.queue()
|
73 |
demo.launch()
|
|
|
19 |
progress(0, desc="モデルのダウンロードを開始")
|
20 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
21 |
progress(0.5, desc="モデルをメモリに読み込み中")
|
22 |
+
|
23 |
llm = Llama(
|
24 |
model_path=model_path,
|
25 |
n_threads=N_THREADS,
|
|
|
30 |
progress(1, desc="モデルの読み込み完了")
|
31 |
model_loaded = True
|
32 |
return "モデルの読み込みが完了しました。"
|
33 |
+
|
34 |
|
35 |
+
def get_llama_response(prompt, temperature):
|
36 |
global llm, model_loaded
|
37 |
if not model_loaded:
|
38 |
return [{"choices": [{"text": "モデルを読み込んでいます。しばらくお待ちください..."}]}]
|
39 |
try:
|
40 |
+
return llm(prompt, max_tokens=1024, temperature=temperature, top_p=0.95, repeat_penalty=1.1, stream=True)
|
41 |
except Exception as e:
|
42 |
return [{"choices": [{"text": f"エラーが発生しました: {str(e)}"}]}]
|
43 |
|
44 |
+
|
45 |
+
def greet(prompt, temperature):
|
46 |
global model_loaded
|
47 |
if not model_loaded:
|
48 |
return "モデルを読み込んでいます。しばらくお待ちください..."
|
49 |
|
50 |
full_response = ""
|
51 |
+
for output in get_llama_response(prompt, temperature):
|
52 |
if len(output['choices']) > 0:
|
53 |
text_chunk = output['choices'][0]['text']
|
54 |
full_response += text_chunk
|
55 |
yield full_response
|
56 |
+
return full_response
|
57 |
+
|
58 |
|
59 |
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown(f"# LLMチャットボット(Streaming):{filename}")
|
61 |
gr.Markdown(f"MODEL: {filename} from {repo_id}")
|
62 |
|
63 |
loading_status = gr.Textbox(label="Loading Status")
|
64 |
|
65 |
with gr.Row():
|
66 |
+
input_text = gr.Textbox(label="プロンプトを入力してください")
|
67 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
|
68 |
|
69 |
+
output_text = gr.Textbox(label="生成されたレスポンス")
|
70 |
+
submit_button = gr.Button("送信")
|
71 |
|
72 |
+
submit_button.click(fn=greet, inputs=[input_text, temperature], outputs=output_text)
|
73 |
demo.load(fn=load_model, outputs=loading_status)
|
74 |
|
75 |
+
|
76 |
demo.queue()
|
77 |
demo.launch()
|