Spaces:
Sleeping
Sleeping
Update run_model.py
Browse files- run_model.py +19 -14
run_model.py
CHANGED
|
@@ -15,8 +15,8 @@ MODEL_NAME = "microsoft/Phi-3.5-mini-instruct"
|
|
| 15 |
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
MODEL_NAME,
|
| 18 |
-
device_map="
|
| 19 |
-
torch_dtype=
|
| 20 |
)
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
|
|
@@ -31,13 +31,17 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
|
| 31 |
# "do_sample": True,
|
| 32 |
# }
|
| 33 |
|
| 34 |
-
# pipe = pipeline(
|
| 35 |
-
# "text-generation",
|
| 36 |
-
# model=model,
|
| 37 |
-
# tokenizer=tokenizer,
|
| 38 |
-
# )
|
| 39 |
model.eval()
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def generate_response(prompt: str, context: str = "", history: list = []) -> str:
|
| 42 |
|
| 43 |
BASE_MESSAGE = [{"role" : "system", "content" : SYSTEM_PROMPT}] + history
|
|
@@ -48,11 +52,12 @@ def generate_response(prompt: str, context: str = "", history: list = []) -> str
|
|
| 48 |
message = BASE_MESSAGE + [{"role" : "user", "content" : f"Context : {context}\nQuery : {prompt}"}]
|
| 49 |
|
| 50 |
# outputs = pipe(message, **generation_args)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
return outputs
|
|
|
|
| 15 |
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
MODEL_NAME,
|
| 18 |
+
device_map="auto",
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
)
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
|
|
|
|
| 31 |
# "do_sample": True,
|
| 32 |
# }
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
model.eval()
|
| 35 |
|
| 36 |
+
pipe = pipeline(
|
| 37 |
+
"text-generation",
|
| 38 |
+
model=model,
|
| 39 |
+
tokenizer=tokenizer,
|
| 40 |
+
max_new_tokens=64,
|
| 41 |
+
temperature=0.4,
|
| 42 |
+
do_sample=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
def generate_response(prompt: str, context: str = "", history: list = []) -> str:
|
| 46 |
|
| 47 |
BASE_MESSAGE = [{"role" : "system", "content" : SYSTEM_PROMPT}] + history
|
|
|
|
| 52 |
message = BASE_MESSAGE + [{"role" : "user", "content" : f"Context : {context}\nQuery : {prompt}"}]
|
| 53 |
|
| 54 |
# outputs = pipe(message, **generation_args)
|
| 55 |
+
outputs = pipe(message)
|
| 56 |
+
# with torch.no_grad():
|
| 57 |
+
# prompt_text = "\n".join([f"{m['role']}: {m['content']}" for m in message])
|
| 58 |
+
# inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
|
| 59 |
+
# outputs = model.generate(**inputs, temperature=0.1, do_sample=True, max_new_tokens=64)
|
| 60 |
+
# outputs = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 61 |
|
| 62 |
+
return outputs[0]["generated_text"]
|
| 63 |
+
# return outputs
|