looker01202 commited on
Commit
fc19e53
Β·
1 Parent(s): 37959bf

adding multiple hotels

Browse files
app.py CHANGED
@@ -55,9 +55,7 @@ def chat(message, history):
55
 
56
  # Apply the chat template thereby converting `history` into a single text prompt:
57
  #input_text = tokenizer.apply_chat_template(history, tokenize=False)
58
- #input_text = tokenizer.apply_chat_template(history, tokenize=False,add_generation_prompt=True)
59
- input_text = tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
60
-
61
 
62
  #print("printing templated chat (pre-tokenizes), ready for sending to the model\n")
63
  #print(input_text)
@@ -74,7 +72,7 @@ def chat(message, history):
74
  decoded = tokenizer.decode(outputs[0])
75
 
76
  # Grab the final model response
77
- response = decoded.split("<|start_of_role|>assistant<|end_of_role|>")[-1].split("<|end_of_text|>")[0]
78
 
79
  #print("and here is the response\n")
80
 
@@ -92,6 +90,7 @@ demo = gr.ChatInterface(
92
  chatbot=gr.Chatbot(height=300,type="messages"),
93
  textbox=gr.Textbox(placeholder="Ask me about the hotel", container=False, scale=7),
94
  #description="This is the description",
 
95
 
96
  description="""
97
  ### 🏨 Hotel Chatbot Demo
@@ -101,30 +100,13 @@ demo = gr.ChatInterface(
101
 
102
  ⚠️ **Reminder:**
103
  When you're done demoing, **pause the Space** (top-right menu) to avoid GPU charges.
104
- """,
105
 
106
  theme="ocean",
107
  examples=["Can you help me book a room?", "Do you have a pool?", "Can I check-in at midday?"],
108
  save_history=True,
109
  )
110
 
111
- # gets this:
112
- """
113
- <|start_of_role|>system<|end_of_role|>Knowledge Cutoff Date: April 2024.
114
- Today's Date: April 16, 2025.
115
- You are Granite, developed by IBM. You are a helpful AI assistant.<|end_of_text|>
116
- <|start_of_role|>user<|end_of_role|>hi there!<|end_of_text|>
117
-
118
- Hello! How can I assist you today? Let's chat about anything you'd like. Whether it's general knowledge, explanations on various topics, help with research, or even some light-hearted conversation, I'm here to help. What's on your mind?<|end_of_text|>
119
- """
120
- """
121
- <|start_of_role|>system<|end_of_role|>Knowledge Cutoff Date: April 2024.
122
- Today's Date: April 16, 2025.
123
- You are Granite, developed by IBM. You are a helpful AI assistant.<|end_of_text|>
124
- <|start_of_role|>user<|end_of_role|>Hello<|end_of_text|>
125
- <|start_of_role|>assistant<|end_of_role|>Hello! How can I assist you today? I'm here to provide information, answer questions, or help with various tasks.<|end_of_text|>
126
- """
127
-
128
 
129
  if __name__ == "__main__":
130
- demo.launch()
 
55
 
56
  # Apply the chat template thereby converting `history` into a single text prompt:
57
  #input_text = tokenizer.apply_chat_template(history, tokenize=False)
58
+ input_text = tokenizer.apply_chat_template(history, tokenize=False,add_generation_prompt=True)
 
 
59
 
60
  #print("printing templated chat (pre-tokenizes), ready for sending to the model\n")
61
  #print(input_text)
 
72
  decoded = tokenizer.decode(outputs[0])
73
 
74
  # Grab the final model response
75
+ response = decoded.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0]
76
 
77
  #print("and here is the response\n")
78
 
 
90
  chatbot=gr.Chatbot(height=300,type="messages"),
91
  textbox=gr.Textbox(placeholder="Ask me about the hotel", container=False, scale=7),
92
  #description="This is the description",
93
+ #description="This is the description",
94
 
95
  description="""
96
  ### 🏨 Hotel Chatbot Demo
 
100
 
101
  ⚠️ **Reminder:**
102
  When you're done demoing, **pause the Space** (top-right menu) to avoid GPU charges.
103
+ """
104
 
105
  theme="ocean",
106
  examples=["Can you help me book a room?", "Do you have a pool?", "Can I check-in at midday?"],
107
  save_history=True,
108
  )
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  if __name__ == "__main__":
112
+ demo.launch()
app_old.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ import torch
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ primary_checkpoint = "ibm-granite/granite-3.2-2b-instruct"
10
+ fallback_checkpoint = "Qwen/Qwen2.5-0.5B-Instruct"
11
+
12
+ print(f"Attempting to load model on {device}...")
13
+
14
+
15
+ #checkpoint = "HuggingFaceTB/SmolLM2-135M-Instruct"
16
+ #checkpoint = "ibm-granite/granite-3.2-2b-instruct"
17
+ #checkpoint = "Qwen/Qwen2.5-0.5B-Instruct"
18
+
19
+ #device = "cpu" # "cuda" or "cpu"
20
+ #tokenizer = AutoTokenizer.from_pretrained(checkpoint, force_download=True)
21
+ #AutoTokenizer.from_pretrained(checkpoint, force_download=True)
22
+
23
+ #print("This is the template being used:\n\n")
24
+ #print(tokenizer.chat_template)
25
+
26
+ #model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
27
+
28
+ try:
29
+ tokenizer = AutoTokenizer.from_pretrained(primary_checkpoint)
30
+ model = AutoModelForCausalLM.from_pretrained(primary_checkpoint).to(device)
31
+ model_name = primary_checkpoint
32
+ print(f"βœ… Loaded primary model: {model_name}")
33
+ except Exception as e:
34
+ print(f"⚠️ Failed to load primary model: {e}")
35
+ print(f"πŸ” Falling back to smaller model: {fallback_checkpoint}")
36
+ tokenizer = AutoTokenizer.from_pretrained(fallback_checkpoint)
37
+ model = AutoModelForCausalLM.from_pretrained(fallback_checkpoint).to(device)
38
+ model_name = fallback_checkpoint
39
+ print(f"βœ… Loaded fallback model: {model_name}")
40
+
41
+ def chat(message, history):
42
+ # history looks like:
43
+ # [
44
+ # {"role": "system", "content": ...}, # if you provided a system prompt
45
+ # {"role": "user", "content": ...},
46
+ # {"role": "assistant", "content": ...}
47
+ # ...
48
+ # ]
49
+
50
+ # If we don't have any chat history object yet, then create one ready for sending to the model
51
+ if not history:
52
+ history = [{"role": "user", "content": message}]
53
+ else:
54
+ history = history + [{"role": "user", "content": message}]
55
+
56
+ # Apply the chat template thereby converting `history` into a single text prompt:
57
+ #input_text = tokenizer.apply_chat_template(history, tokenize=False)
58
+ #input_text = tokenizer.apply_chat_template(history, tokenize=False,add_generation_prompt=True)
59
+ input_text = tokenizer.apply_chat_template(history, tokenize=False, add_generation_prompt=True)
60
+
61
+
62
+ #print("printing templated chat (pre-tokenizes), ready for sending to the model\n")
63
+ #print(input_text)
64
+
65
+ # Tokenize the prompt ready for sending to the model
66
+ inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
67
+
68
+ # Send the tokenized prompt to the model and capture the reply in 'outputs'
69
+ # outputs = model.generate(inputs, max_new_tokens=1024, temperature=0.1, top_p=0.9, do_sample=True)
70
+ outputs = model.generate(inputs, max_new_tokens=1024)
71
+
72
+
73
+ # de-tokenize the model the outputs
74
+ decoded = tokenizer.decode(outputs[0])
75
+
76
+ # Grab the final model response
77
+ response = decoded.split("<|start_of_role|>assistant<|end_of_role|>")[-1].split("<|end_of_text|>")[0]
78
+
79
+ #print("and here is the response\n")
80
+
81
+ # print out the reply from the model to the last question we just added
82
+ # print(tokenizer.decode(outputs[0]))
83
+ #print(response)
84
+
85
+ # Send this reponse back to gr.ChatInterface, which will display it to the user as the next assistant message.
86
+ return response
87
+
88
+ demo = gr.ChatInterface(
89
+ fn=chat,
90
+ type="messages",
91
+ title="Hotel chat",
92
+ chatbot=gr.Chatbot(height=300,type="messages"),
93
+ textbox=gr.Textbox(placeholder="Ask me about the hotel", container=False, scale=7),
94
+ #description="This is the description",
95
+
96
+ description="""
97
+ ### 🏨 Hotel Chatbot Demo
98
+ Ask anything about your hotel stay β€” room availability, check-in times, amenities, and more.
99
+
100
+ ---
101
+
102
+ ⚠️ **Reminder:**
103
+ When you're done demoing, **pause the Space** (top-right menu) to avoid GPU charges.
104
+ """,
105
+
106
+ theme="ocean",
107
+ examples=["Can you help me book a room?", "Do you have a pool?", "Can I check-in at midday?"],
108
+ save_history=True,
109
+ )
110
+
111
+ # gets this:
112
+ """
113
+ <|start_of_role|>system<|end_of_role|>Knowledge Cutoff Date: April 2024.
114
+ Today's Date: April 16, 2025.
115
+ You are Granite, developed by IBM. You are a helpful AI assistant.<|end_of_text|>
116
+ <|start_of_role|>user<|end_of_role|>hi there!<|end_of_text|>
117
+
118
+ Hello! How can I assist you today? Let's chat about anything you'd like. Whether it's general knowledge, explanations on various topics, help with research, or even some light-hearted conversation, I'm here to help. What's on your mind?<|end_of_text|>
119
+ """
120
+ """
121
+ <|start_of_role|>system<|end_of_role|>Knowledge Cutoff Date: April 2024.
122
+ Today's Date: April 16, 2025.
123
+ You are Granite, developed by IBM. You are a helpful AI assistant.<|end_of_text|>
124
+ <|start_of_role|>user<|end_of_role|>Hello<|end_of_text|>
125
+ <|start_of_role|>assistant<|end_of_role|>Hello! How can I assist you today? I'm here to provide information, answer questions, or help with various tasks.<|end_of_text|>
126
+ """
127
+
128
+
129
+ if __name__ == "__main__":
130
+ demo.launch()
knowledge/coastal-villa-family.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Coastal Villa is a boutique family-run hotel on the Paphos coastline with 18 rooms and sea views.
2
+ Features a private pool overlooking the Mediterranean, sun loungers, and poolside snack service.
3
+ Each morning we serve a full Cypriot breakfast buffet. Late checkout is available on request. Free parking and airport shuttle are offered.
knowledge/cyprus-guesthouse-family.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ The Cyprus Guesthouse is a family-owned hotel located in the heart of Larnaca. We offer 12 cozy rooms, each decorated with local artisan crafts.
2
+ Amenities include a freshwater swimming pool open from 8 AM to 8 PM, complimentary breakfast featuring traditional Cypriot halloumi and olives, free Wi-Fi, and 24-hour front desk service.
3
+ Guests can enjoy afternoon herb tea in our courtyard garden, and we provide complimentary bicycles for exploring nearby beaches.
knowledge/village-inn-family.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Village Inn is a charming family-owned inn in the Troodos foothills, with 20 rooms set around a central pool and garden.
2
+ Our pool is heated from April through October. We host a nightly mezze and wine tasting in our traditional stone courtyard.
3
+ All rooms include kitchenette facilities, and local village maps. Complimentary parking and pet-friendly rooms are available.
test_splitter.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ text_stream = """<|start_of_role|>system<|end_of_role|>Knowledge Cutoff Date: April 2024.
2
+ Today's Date: April 16, 2025.
3
+ You are Granite, developed by IBM. You are a helpful AI assistant.<|end_of_text|>
4
+ <|start_of_role|>user<|end_of_role|>Hello<|end_of_text|>
5
+ <|start_of_role|>assistant<|end_of_role|>Hello! How can I assist you today? I'm here to provide information, answer questions, or help with various tasks.<|end_of_text|>"""
6
+
7
+ answer = text_stream.split("<|start_of_role|>assistant<|end_of_role|>")[-1].split("<|end_of_text|>")[0]
8
+ print(answer)