yuna1126 commited on
Commit
d7ec6ce
·
verified ·
1 Parent(s): 9da5836

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -20,11 +20,13 @@ from huggingface_hub import hf_hub_download
20
  model_id = "kawasumi/Tema_Q-R4.2-GGUF"
21
  model_file = "Tema_Q-R4.2-Q2_K.gguf"
22
 
 
 
 
23
  print("Downloading model...")
24
  model_path = hf_hub_download(repo_id=model_id, filename=model_file)
25
 
26
  print(f"Loading model from {model_path}...")
27
- # メモリ節約のための設定を強化
28
  llm = Llama(
29
  model_path=model_path,
30
  n_ctx=1024,
@@ -35,25 +37,35 @@ llm = Llama(
35
  print("Model loaded.")
36
 
37
  def chat_response(message, history):
 
 
 
 
 
38
 
39
  prompt = f"<start_of_turn>user\n{message}<end_of_turn>\n<start_of_turn>model\n"
40
 
41
- output = llm(
42
- prompt,
43
- max_tokens=512,
44
- stop=["<end_of_turn>", "user"],
45
- stream=True
46
- )
47
-
48
- response = ""
49
- for chunk in output:
50
- text = chunk["choices"][0]["text"]
51
- response += text
52
- yield response
 
 
 
53
 
 
54
  demo = gr.ChatInterface(
55
  fn=chat_response,
56
  title="Tema_Q-R4.2 Chat",
 
57
  )
58
 
59
  if __name__ == "__main__":
 
20
  model_id = "kawasumi/Tema_Q-R4.2-GGUF"
21
  model_file = "Tema_Q-R4.2-Q2_K.gguf"
22
 
23
+ # 入力制限文字数
24
+ MAX_INPUT_CHARS = 15
25
+
26
  print("Downloading model...")
27
  model_path = hf_hub_download(repo_id=model_id, filename=model_file)
28
 
29
  print(f"Loading model from {model_path}...")
 
30
  llm = Llama(
31
  model_path=model_path,
32
  n_ctx=1024,
 
37
  print("Model loaded.")
38
 
39
  def chat_response(message, history):
40
+ # --- 文字数制限の追加 ---
41
+ if len(message) > MAX_INPUT_CHARS:
42
+ yield f"【エラー】入力が長すぎます。{MAX_INPUT_CHARS}文字以内で入力してください。(現在 {len(message)} 文字)"
43
+ return
44
+ # ----------------------
45
 
46
  prompt = f"<start_of_turn>user\n{message}<end_of_turn>\n<start_of_turn>model\n"
47
 
48
+ try:
49
+ output = llm(
50
+ prompt,
51
+ max_tokens=512,
52
+ stop=["<end_of_turn>", "user"],
53
+ stream=True
54
+ )
55
+
56
+ response = ""
57
+ for chunk in output:
58
+ text = chunk["choices"][0]["text"]
59
+ response += text
60
+ yield response
61
+ except Exception as e:
62
+ yield f"エラーが発生しました: {str(e)}"
63
 
64
+ # UIの構築(説明欄に制限の記載を追加)
65
  demo = gr.ChatInterface(
66
  fn=chat_response,
67
  title="Tema_Q-R4.2 Chat",
68
+ description=f"※{MAX_INPUT_CHARS}文字以内で入力してください。CPU環境のため、回答開始まで時間がかかります。"
69
  )
70
 
71
  if __name__ == "__main__":