tori29umai commited on
Commit
82f2d09
1 Parent(s): 90c6cb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -1,8 +1,10 @@
 
 
 
1
  import spaces
2
  import gradio as gr
3
  from jinja2 import Template
4
  from llama_cpp import Llama
5
- import os
6
  import configparser
7
  from utils.dl_utils import dl_guff_model
8
 
@@ -61,9 +63,15 @@ class LlamaCppAdapter:
61
  @spaces.GPU(duration=120)
62
  def __init__(self, model_path, n_ctx=4096):
63
  print(f"モデルの初期化: {model_path}")
64
- self.llama = Llama(model_path=model_path, n_ctx=n_ctx, n_gpu_layers=-1)
 
 
 
 
65
 
66
  def generate(self, prompt, max_new_tokens=4096, temperature=0.5, top_p=0.7, top_k=80, stop=["<END>"]):
 
 
67
  return self._generate(prompt, max_new_tokens, temperature, top_p, top_k, stop)
68
 
69
  def _generate(self, prompt: str, max_new_tokens: int, temperature: float, top_p: float, top_k: int, stop: list):
@@ -116,8 +124,12 @@ class CharacterMaker:
116
  prompt = self._generate_aki(input_str)
117
  print(prompt)
118
  print("-----------------")
119
- res = self.llama.generate(prompt, max_new_tokens=1000, stop=["<END>", "\n"])
120
- res_text = res["choices"][0]["text"]
 
 
 
 
121
  self.history.append({"user": input_str, "assistant": res_text})
122
  return res_text
123
 
@@ -139,7 +151,6 @@ class CharacterMaker:
139
  self.settings["example_quotes"],
140
  input_str
141
  )
142
- print(prompt)
143
  return prompt
144
 
145
  def update_settings(self, new_settings):
@@ -193,7 +204,6 @@ function adjustChatbotHeight() {
193
  chatbot.style.height = window.innerHeight * 0.6 + 'px';
194
  }
195
  }
196
-
197
  // ページ読み込み時と画面サイズ変更時にチャットボットの高さを調整
198
  window.addEventListener('load', adjustChatbotHeight);
199
  window.addEventListener('resize', adjustChatbotHeight);
 
1
+ import os
2
+ os.environ['CUDA_VISIBLE_DEVICES'] = ''
3
+
4
  import spaces
5
  import gradio as gr
6
  from jinja2 import Template
7
  from llama_cpp import Llama
 
8
  import configparser
9
  from utils.dl_utils import dl_guff_model
10
 
 
63
  @spaces.GPU(duration=120)
64
  def __init__(self, model_path, n_ctx=4096):
65
  print(f"モデルの初期化: {model_path}")
66
+ try:
67
+ self.llama = Llama(model_path=model_path, n_ctx=n_ctx, n_gpu_layers=0)
68
+ except Exception as e:
69
+ print(f"モデルの初期化中にエラーが発生しました: {e}")
70
+ self.llama = None
71
 
72
  def generate(self, prompt, max_new_tokens=4096, temperature=0.5, top_p=0.7, top_k=80, stop=["<END>"]):
73
+ if self.llama is None:
74
+ return {"choices": [{"text": "モデルの初期化に失敗しました。"}]}
75
  return self._generate(prompt, max_new_tokens, temperature, top_p, top_k, stop)
76
 
77
  def _generate(self, prompt: str, max_new_tokens: int, temperature: float, top_p: float, top_k: int, stop: list):
 
124
  prompt = self._generate_aki(input_str)
125
  print(prompt)
126
  print("-----------------")
127
+ try:
128
+ res = self.llama.generate(prompt, max_new_tokens=1000, stop=["<END>", "\n"])
129
+ res_text = res["choices"][0]["text"]
130
+ except Exception as e:
131
+ print(f"生成中にエラーが発生しました: {e}")
132
+ res_text = "申し訳ありません。応答の生成中にエラーが発生しました。"
133
  self.history.append({"user": input_str, "assistant": res_text})
134
  return res_text
135
 
 
151
  self.settings["example_quotes"],
152
  input_str
153
  )
 
154
  return prompt
155
 
156
  def update_settings(self, new_settings):
 
204
  chatbot.style.height = window.innerHeight * 0.6 + 'px';
205
  }
206
  }
 
207
  // ページ読み込み時と画面サイズ変更時にチャットボットの高さを調整
208
  window.addEventListener('load', adjustChatbotHeight);
209
  window.addEventListener('resize', adjustChatbotHeight);