Spaces:
Sleeping
Sleeping
Commit
·
09df582
1
Parent(s):
df73242
generate_response fix
Browse files
app.py
CHANGED
@@ -34,8 +34,10 @@ def get_model_and_tokenizer(model_id):
|
|
34 |
def generate_response(user_input, model_id):
|
35 |
prompt = formatted_prompt(user_input)
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
get_model_and_tokenizer(model_id) # Load model and tokenizer
|
40 |
|
41 |
# Prepare the input tensors
|
@@ -44,7 +46,6 @@ def generate_response(user_input, model_id):
|
|
44 |
generation_config = GenerationConfig(
|
45 |
max_new_tokens=100,
|
46 |
min_length=5,
|
47 |
-
#temperature=0.7,
|
48 |
do_sample=False,
|
49 |
num_beams=1,
|
50 |
pad_token_id=tokenizer.eos_token_id,
|
@@ -59,7 +60,7 @@ def generate_response(user_input, model_id):
|
|
59 |
except Exception as e:
|
60 |
print(f"Error generating response: {e}")
|
61 |
return "Error generating response."
|
62 |
-
|
63 |
def formatted_prompt(question) -> str:
|
64 |
return f"<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant:"
|
65 |
|
|
|
34 |
def generate_response(user_input, model_id):
|
35 |
prompt = formatted_prompt(user_input)
|
36 |
|
37 |
+
global model, tokenizer
|
38 |
+
|
39 |
+
# Load the model and tokenizer if they are not already loaded or if the model_id has changed
|
40 |
+
if model is None or tokenizer is None or (model.config._name_or_path != model_id):
|
41 |
get_model_and_tokenizer(model_id) # Load model and tokenizer
|
42 |
|
43 |
# Prepare the input tensors
|
|
|
46 |
generation_config = GenerationConfig(
|
47 |
max_new_tokens=100,
|
48 |
min_length=5,
|
|
|
49 |
do_sample=False,
|
50 |
num_beams=1,
|
51 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
60 |
except Exception as e:
|
61 |
print(f"Error generating response: {e}")
|
62 |
return "Error generating response."
|
63 |
+
|
64 |
def formatted_prompt(question) -> str:
|
65 |
return f"<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant:"
|
66 |
|