Update app.py
Browse files
app.py
CHANGED
@@ -6,45 +6,28 @@ import torch
|
|
6 |
# Load base model
|
7 |
base_model = AutoModelForCausalLM.from_pretrained(
|
8 |
"mistralai/Mistral-7B-Instruct-v0.1",
|
9 |
-
torch_dtype=torch.float16
|
10 |
device_map="auto"
|
11 |
)
|
12 |
|
13 |
-
# Load LoRA
|
14 |
model = PeftModel.from_pretrained(base_model, "gaurav2003/room-service-chatbot")
|
15 |
|
16 |
-
# Load tokenizer
|
17 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
18 |
-
tokenizer.pad_token = tokenizer.eos_token
|
19 |
|
20 |
-
# Chat function
|
21 |
def chat(user_input, history=[]):
|
22 |
-
input_ids = tokenizer(user_input, return_tensors="pt").input_ids.to(model.device)
|
23 |
-
|
24 |
-
|
25 |
-
with torch.no_grad():
|
26 |
-
output_ids = model.generate(
|
27 |
-
input_ids,
|
28 |
-
max_new_tokens=100,
|
29 |
-
pad_token_id=tokenizer.pad_token_id,
|
30 |
-
do_sample=True,
|
31 |
-
top_p=0.95,
|
32 |
-
temperature=0.7
|
33 |
-
)
|
34 |
-
|
35 |
-
# Decode response (remove the prompt part)
|
36 |
-
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
37 |
-
response = response.replace(user_input, "").strip()
|
38 |
-
|
39 |
return response
|
40 |
|
41 |
-
# Gradio UI
|
42 |
iface = gr.Interface(
|
43 |
fn=chat,
|
44 |
-
inputs=gr.Textbox(placeholder="Ask something..."
|
45 |
outputs="text",
|
46 |
-
title="Room Service Chatbot"
|
47 |
-
description="Chat with your fine-tuned hotel assistant!"
|
48 |
)
|
49 |
|
50 |
if __name__ == "__main__":
|
|
|
6 |
# Load base model
|
7 |
base_model = AutoModelForCausalLM.from_pretrained(
|
8 |
"mistralai/Mistral-7B-Instruct-v0.1",
|
9 |
+
torch_dtype=torch.float16,
|
10 |
device_map="auto"
|
11 |
)
|
12 |
|
13 |
+
# Load LoRA Adapter
|
14 |
model = PeftModel.from_pretrained(base_model, "gaurav2003/room-service-chatbot")
|
15 |
|
16 |
+
# Load tokenizer (from base model)
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
18 |
+
tokenizer.pad_token = tokenizer.eos_token
|
19 |
|
|
|
20 |
def chat(user_input, history=[]):
|
21 |
+
input_ids = tokenizer(user_input, return_tensors="pt", padding=True).input_ids.to(model.device)
|
22 |
+
output = model.generate(input_ids, max_new_tokens=150)
|
23 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
return response
|
25 |
|
|
|
26 |
iface = gr.Interface(
|
27 |
fn=chat,
|
28 |
+
inputs=gr.Textbox(placeholder="Ask something..."),
|
29 |
outputs="text",
|
30 |
+
title="Room Service Chatbot"
|
|
|
31 |
)
|
32 |
|
33 |
if __name__ == "__main__":
|