File size: 26,895 Bytes
0b599b4
 
 
9f88041
0b599b4
 
 
 
 
 
 
 
 
 
 
 
9f88041
0b599b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# Complete Malayalam Hospital Booking Chatbot using Llama 3.1-8B-Instruct
# with HuggingFace Transformers Library in Google Colab


import json
import gradio as gr
import torch
import datetime
import pytz
import uuid
import re
import time
import random
from transformers import AutoModelForCausalLM, AutoTokenizer
from google.colab import auth
from googleapiclient.discovery import build
import os

# Set up timezone for India
IST = pytz.timezone('Asia/Kolkata')

# ===== CONFIGURATION =====

# Path to store the model locally (to avoid re-downloading)
MODEL_PATH = "/content/llama-3.1-8b-instruct"

# Replace with your actual Hugging Face token
HF_TOKEN = ""  # Will be set via Colab input

# Google Calendar API scopes
SCOPES = ['https://www.googleapis.com/auth/calendar']

# Available doctors and departments for booking
available_doctors = {
  "cardiology": ["Dr. Anoop Menon", "Dr. Priya Nair"],
  "orthopedics": ["Dr. Rajesh Kumar", "Dr. Meera Pillai"],
  "neurology": ["Dr. Vinod Thomas", "Dr. Lakshmi Nair"],
  "pediatrics": ["Dr. Suresh Babu", "Dr. Anjali Krishnan"],
  "general": ["Dr. Joseph Mathew", "Dr. Deepa Varma"]
}

# Hospital database simulation
appointments_db = {}

# ===== FUNCTION DEFINITIONS =====

function_definitions = [
  {
    "name": "check_doctor_availability",
    "description": "Check which doctors are available in a specific department",
    "parameters": {
      "type": "object",
      "properties": {
        "department": {
          "type": "string",
          "description": "The hospital department (cardiology, orthopedics, neurology, pediatrics, general)"
        }
      },
      "required": ["department"]
    }
  },
  {
    "name": "check_time_slots",
    "description": "Check available time slots for a specific doctor on a specific date",
    "parameters": {
      "type": "object",
      "properties": {
        "doctor_name": {
          "type": "string",
          "description": "The name of the doctor"
        },
        "date": {
          "type": "string",
          "description": "The date in YYYY-MM-DD format"
        }
      },
      "required": ["doctor_name", "date"]
    }
  },
  {
    "name": "book_appointment",
    "description": "Book an appointment with a doctor and add it to Google Calendar",
    "parameters": {
      "type": "object",
      "properties": {
        "patient_name": {
          "type": "string",
          "description": "The name of the patient"
        },
        "patient_phone": {
          "type": "string",
          "description": "The phone number of the patient"
        },
        "doctor_name": {
          "type": "string",
          "description": "The name of the doctor"
        },
        "department": {
          "type": "string",
          "description": "The hospital department"
        },
        "date": {
          "type": "string",
          "description": "The date in YYYY-MM-DD format"
        },
        "time": {
          "type": "string",
          "description": "The time of the appointment (e.g., '10:00 AM')"
        },
        "description": {
          "type": "string",
          "description": "Brief description of the medical issue"
        }
      },
      "required": ["patient_name", "patient_phone", "doctor_name", "department", "date", "time"]
    }
  },
  {
    "name": "cancel_appointment",
    "description": "Cancel an existing appointment",
    "parameters": {
      "type": "object",
      "properties": {
        "appointment_id": {
          "type": "string",
          "description": "The ID of the appointment to cancel"
        },
        "patient_phone": {
          "type": "string",
          "description": "The phone number of the patient for verification"
        }
      },
      "required": ["appointment_id", "patient_phone"]
    }
  }
]

# ===== FUNCTION IMPLEMENTATIONS =====

def check_doctor_availability(department):
    """Check which doctors are available in a specific department"""
    if department.lower() in available_doctors:
        return {
            "available": True,
            "doctors": available_doctors[department.lower()]
        }
    else:
        return {
            "available": False,
            "message": "Department not found",
            "available_departments": list(available_doctors.keys())
        }

def check_time_slots(doctor_name, date):
    """Check available time slots for a specific doctor on a specific date"""
    # Available time slots
    all_slots = [
        "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM",
        "11:00 AM", "11:30 AM", "12:00 PM", "02:00 PM",
        "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM"
    ]

    # In a real implementation, this would check a database
    # For this example, we'll simulate some slots being taken
    taken_slots = random.sample(all_slots, 3)  # Randomly mark 3 slots as taken

    available_slots = [slot for slot in all_slots if slot not in taken_slots]

    return {
        "date": date,
        "doctor": doctor_name,
        "available_slots": available_slots
    }

def book_appointment(appointment_details, calendar_service):
    """Book an appointment with a doctor and add it to Google Calendar"""
    try:
        # Validate the appointment details first
        doctor_exists = False
        for dept_doctors in available_doctors.values():
            if appointment_details["doctor_name"] in dept_doctors:
                doctor_exists = True
                break

        if not doctor_exists:
            return {
                "success": False,
                "message": "Doctor not found"
            }

        # Parse date and time
        date_parts = appointment_details["date"].split('-')
        year, month, day = int(date_parts[0]), int(date_parts[1]), int(date_parts[2])

        time_parts = appointment_details["time"].split(' ')
        time = time_parts[0]
        meridian = time_parts[1]

        hours, minutes = map(int, time.split(':'))

        if meridian == 'PM' and hours != 12:
            hours += 12
        if meridian == 'AM' and hours == 12:
            hours = 0

        start_datetime = datetime.datetime(year, month, day, hours, minutes, 0, tzinfo=IST)
        end_datetime = start_datetime + datetime.timedelta(minutes=30)  # 30 minutes appointment

        # Create the calendar event
        event = {
            'summary': f"Medical appointment with {appointment_details['doctor_name']}",
            'location': 'City Hospital, Kochi, Kerala',
            'description': appointment_details.get('description', 'Regular checkup'),
            'start': {
                'dateTime': start_datetime.isoformat(),
                'timeZone': 'Asia/Kolkata',
            },
            'end': {
                'dateTime': end_datetime.isoformat(),
                'timeZone': 'Asia/Kolkata',
            },
            'attendees': [
                {'email': 'doctor@cityhospital.com'},
                {'email': 'patient@example.com'}  # In a real app, use actual email
            ],
            'reminders': {
                'useDefault': False,
                'overrides': [
                    {'method': 'email', 'minutes': 24 * 60},
                    {'method': 'popup', 'minutes': 60},
                ],
            },
        }

        # Add to Google Calendar
        if calendar_service:
            try:
                event = calendar_service.events().insert(calendarId='primary', body=event).execute()
                appointment_id = event['id']
            except Exception as e:
                print(f"Calendar service error: {e}")
                # Generate a mock ID if calendar service fails
                appointment_id = str(uuid.uuid4())
        else:
            # If no calendar service, generate a mock ID
            appointment_id = str(uuid.uuid4())

        # Store in local database
        appointments_db[appointment_id] = {
            "patient_name": appointment_details["patient_name"],
            "patient_phone": appointment_details["patient_phone"],
            "doctor_name": appointment_details["doctor_name"],
            "department": appointment_details["department"],
            "date": appointment_details["date"],
            "time": appointment_details["time"],
            "description": appointment_details.get("description", ""),
        }

        return {
            "success": True,
            "appointment_id": appointment_id,
            "message": "Appointment successfully booked",
            "details": {
                "doctor": appointment_details["doctor_name"],
                "department": appointment_details["department"],
                "date": appointment_details["date"],
                "time": appointment_details["time"],
                "location": 'City Hospital, Kochi, Kerala'
            }
        }
    except Exception as e:
        print(f"Error in book_appointment: {e}")
        return {
            "success": False,
            "message": f"Failed to book appointment: {str(e)}"
        }

def cancel_appointment(appointment_id, patient_phone, calendar_service):
    """Cancel an existing appointment"""
    try:
        # Check if appointment exists in our database
        if appointment_id not in appointments_db:
            return {
                "success": False,
                "message": "Appointment not found"
            }

        # Verify patient phone
        if appointments_db[appointment_id]["patient_phone"] != patient_phone:
            return {
                "success": False,
                "message": "Patient phone number does not match our records"
            }

        # Delete from Google Calendar
        if calendar_service:
            try:
                calendar_service.events().delete(calendarId='primary', eventId=appointment_id).execute()
            except Exception as e:
                print(f"Error deleting from calendar: {e}")
                # Continue anyway to delete from local database

        # Remove from local database
        del appointments_db[appointment_id]

        return {
            "success": True,
            "message": "Appointment successfully cancelled"
        }
    except Exception as e:
        return {
            "success": False,
            "message": f"Failed to cancel appointment: {str(e)}"
        }

# ===== GOOGLE CALENDAR AUTHENTICATION =====

def get_calendar_service():
    """Authenticate and return the Google Calendar service"""
    creds = None

    try:
        # Authenticate using Colab's auth helper
        auth.authenticate_user()

        # Get credentials from the authenticated Colab user
        from google.auth import default
        creds, _ = default()

        # Build and return the service
        service = build('calendar', 'v3', credentials=creds)
        return service
    except Exception as e:
        print(f"Error authenticating with Google Calendar: {e}")
        print("Continuing without Google Calendar integration.")
        return None

# ===== LLAMA 3.1 MODEL SETUP =====

def load_llama_model():
    """Load the Llama 3.1 model and tokenizer"""
    model_name = "meta-llama/Meta-Llama-3.1-8B-Instruct"

    print("Loading Llama 3.1 model and tokenizer...")

    try:
        # Check if model is already downloaded
        if os.path.exists(MODEL_PATH):
            print(f"Loading model from local path: {MODEL_PATH}")
            tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
            model = AutoModelForCausalLM.from_pretrained(
                MODEL_PATH,
                torch_dtype=torch.bfloat16,
                device_map="auto",
                low_cpu_mem_usage=True
            )
        else:
            print(f"Downloading model from Hugging Face Hub")
            tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN)
            model = AutoModelForCausalLM.from_pretrained(
                model_name,
                torch_dtype=torch.bfloat16,
                device_map="auto",
                low_cpu_mem_usage=True,
                token=HF_TOKEN
            )

            # Save model locally to avoid re-downloading
            print(f"Saving model to: {MODEL_PATH}")
            tokenizer.save_pretrained(MODEL_PATH)
            model.save_pretrained(MODEL_PATH)

        print("Model loaded successfully!")
        return model, tokenizer

    except Exception as e:
        print(f"Error loading model: {e}")
        return None, None

# ===== CHAT PROCESSING =====

def format_prompt_with_functions(messages, system_prompt):
    """Format the prompt for Llama 3.1 with function definitions"""
    # Add function definitions to system prompt
    full_system_prompt = system_prompt + "\n\n"
    full_system_prompt += "You have access to the following functions:\n"

    for func in function_definitions:
        full_system_prompt += f"- {func['name']}: {func['description']}\n"
        full_system_prompt += "  Parameters:\n"
        for param_name, param_info in func['parameters']['properties'].items():
            required = "required" if param_name in func['parameters'].get('required', []) else "optional"
            full_system_prompt += f"    - {param_name} ({required}): {param_info.get('description', '')}\n"

    full_system_prompt += "\nIf the user's request can be addressed by calling one of these functions, respond in the following JSON format:\n"
    full_system_prompt += '```json\n{"function_call": {"name": "function_name", "arguments": {"arg1": "value1", "arg2": "value2"}}}\n```\n'
    full_system_prompt += "Otherwise, respond conversationally."

    # Format conversation history
    formatted_messages = [
        {"role": "system", "content": full_system_prompt}
    ]

    # Add conversation history
    for message in messages:
        if message["role"] == "function":
            # Convert function results to assistant format for Llama 3.1
            formatted_messages.append({
                "role": "assistant",
                "content": f"I'll process the function result: {message['content']}"
            })
        else:
            formatted_messages.append(message)

    return formatted_messages

def extract_function_call(response_text):
    """Extract function call from model response"""
    # Look for JSON block in the response
    json_pattern = r'```json\s*(.*?)\s*```'
    json_matches = re.findall(json_pattern, response_text, re.DOTALL)

    if not json_matches:
        # Try alternative pattern without markdown
        json_pattern = r'({.*"function_call".*})'
        json_matches = re.findall(json_pattern, response_text, re.DOTALL)

    if json_matches:
        try:
            for json_str in json_matches:
                parsed_json = json.loads(json_str.strip())
                if "function_call" in parsed_json:
                    function_call = parsed_json["function_call"]
                    return {
                        "id": str(uuid.uuid4()),
                        "name": function_call["name"],
                        "arguments": function_call["arguments"]
                    }
        except json.JSONDecodeError:
            print(f"Failed to parse JSON: {json_matches[0]}")

    return None

def process_chat(message, chat_history, language, model_tokenizer_calendar):
    """Process a chat message, calling functions when necessary"""
    model, tokenizer, calendar_service = model_tokenizer_calendar

    try:
        # Create system prompt based on language preference
        system_prompt = f"""You are a hospital booking assistant for City Hospital in Kerala. You can understand and respond fluently in Malayalam and English.

For Malayalam speakers, introduce yourself as: "ഹലോ, ഞാൻ സിറ്റി ഹോസ്പിറ്റലിന്റെ ഓൺലൈൻ അസിസ്റ്റന്റ് ആണ്. എങ്ങനെ സഹായിക്കാൻ കഴിയും?"

Be polite and helpful. You can assist with checking doctor availability, booking appointments, and answering general questions about the hospital services.

For medical questions that require diagnosis, always advise patients to consult a doctor directly.

When booking appointments, collect all necessary information: patient name, phone number, department, doctor, date, and time.

Current language preference: {language}"""

        # Build message history from chat history
        messages = []
        for user_msg, bot_msg in chat_history:
            messages.append({"role": "user", "content": user_msg})
            messages.append({"role": "assistant", "content": bot_msg})

        # Add current message
        messages.append({"role": "user", "content": message})

        # Format messages with function calling info
        formatted_messages = format_prompt_with_functions(messages, system_prompt)

        # Generate model response
        inputs = tokenizer.apply_chat_template(
            formatted_messages,
            tokenize=True,
            add_generation_prompt=True,
            return_tensors="pt"
        ).to(model.device)

        outputs = model.generate(
            inputs,
            max_new_tokens=1024,
            temperature=0.7,
            top_p=0.9,
            do_sample=True
        )

        response_text = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)

        # Check if response contains a function call
        function_call = extract_function_call(response_text)

        if function_call:
            # Extract non-JSON response part for context (if any)
            response_context = response_text.split("```")[0].strip() if "```" in response_text else ""

            # Execute the appropriate function
            function_name = function_call["name"]
            function_args = function_call["arguments"]

            function_result = None
            if function_name == "check_doctor_availability" and "department" in function_args:
                function_result = check_doctor_availability(function_args["department"])
            elif function_name == "check_time_slots" and "doctor_name" in function_args and "date" in function_args:
                function_result = check_time_slots(function_args["doctor_name"], function_args["date"])
            elif function_name == "book_appointment":
                function_result = book_appointment(function_args, calendar_service)
            elif function_name == "cancel_appointment" and "appointment_id" in function_args and "patient_phone" in function_args:
                function_result = cancel_appointment(function_args["appointment_id"], function_args["patient_phone"], calendar_service)
            else:
                function_result = {"error": "Invalid function call or missing parameters"}

            # Add the function result to messages
            messages.append({
                "role": "assistant",
                "content": response_context,
            })

            messages.append({
                "role": "function",
                "name": function_name,
                "content": json.dumps(function_result)
            })

            # Format messages for second call
            formatted_messages = format_prompt_with_functions(messages, system_prompt)

            # Generate second response
            inputs = tokenizer.apply_chat_template(
                formatted_messages,
                tokenize=True,
                add_generation_prompt=True,
                return_tensors="pt"
            ).to(model.device)

            outputs = model.generate(
                inputs,
                max_new_tokens=1024,
                temperature=0.7,
                top_p=0.9,
                do_sample=True
            )

            second_response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)

            # Update chat history
            new_chat_history = chat_history + [(message, second_response)]

            return second_response, new_chat_history
        else:
            # No function call, just return the response
            # Update chat history
            new_chat_history = chat_history + [(message, response_text)]

            return response_text, new_chat_history

    except Exception as e:
        print(f"Error in process_chat: {e}")
        error_msg = f"Sorry, I encountered an error. Please try again. (Error: {str(e)})"
        return error_msg, chat_history + [(message, error_msg)]

# ===== GRADIO INTERFACE =====

def create_gradio_interface(model, tokenizer, calendar_service):
    """Create the Gradio interface for the chatbot"""

    with gr.Blocks(css="""
        .gradio-container {max-width: 800px !important}
        .chat-window {height: 600px !important; overflow-y: auto}
        .language-selector {text-align: right; margin-bottom: 10px}
    """) as demo:
        gr.Markdown("# City Hospital - Hospital Booking Assistant")
        gr.Markdown("### മലയാളത്തിലും ഇംഗ്ലീഷിലും സംസാരിക്കുന്ന ആശുപത്രി ബുക്കിംഗ് സഹായി")

        with gr.Row():
            with gr.Column():
                language = gr.Radio(
                    ["English", "Malayalam"],
                    label="Select Language",
                    value="English",
                    interactive=True
                )

        chatbot = gr.Chatbot(
            [],
            elem_id="chatbot",
            label="Chat with Hospital Assistant",
            height=500
        )

        with gr.Row():
            msg = gr.Textbox(
                show_label=False,
                placeholder="Type your message here...",
                container=False
            )
            submit = gr.Button("Send")

        with gr.Row():
            clear = gr.Button("Clear Conversation")

        # Provide instructions
        with gr.Accordion("Instructions", open=False):
            gr.Markdown("""
            ## How to use this hospital booking assistant:

            1. You can chat in English or Malayalam - select your preferred language above.
            2. Ask about doctor availability in different departments.
            3. Check available time slots for appointments.
            4. Book appointments by providing patient details.
            5. Cancel existing appointments if needed.

            ### Example questions in English:
            - Which doctors are available in the cardiology department?
            - I need an appointment with Dr. Priya Nair tomorrow.
            - What are your hospital visiting hours?

            ### Example questions in Malayalam:
            - കാർഡിയോളജി വിഭാഗത്തിൽ ഏതൊക്കെ ഡോക്ടർമാർ ലഭ്യമാണ്?
            - എനിക്ക് നാളെ ഡോ. പ്രിയ നായരുമായി ഒരു അപ്പോയിന്റ്മെന്റ് വേണം.
            - നിങ്ങളുടെ ആശുപത്രി സന്ദർശന സമയങ്ങൾ എന്തൊക്കെയാണ്?
            """)

        chat_history = gr.State([])

        # Set up event handlers
        submit.click(
            process_chat,
            inputs=[msg, chat_history, language, gr.State((model, tokenizer, calendar_service))],
            outputs=[chatbot, chat_history]
        ).then(
            lambda: "",
            None,
            msg
        )

        msg.submit(
            process_chat,
            inputs=[msg, chat_history, language, gr.State((model, tokenizer, calendar_service))],
            outputs=[chatbot, chat_history]
        ).then(
            lambda: "",
            None,
            msg
        )

        clear.click(
            lambda: ([], []),
            inputs=None,
            outputs=[chatbot, chat_history]
        )

        # When language changes, add a system message
        def on_language_change(lang, history):
            if lang == "Malayalam":
                welcome = "ഹലോ, ഞാൻ സിറ്റി ഹോസ്പിറ്റലിന്റെ ഓൺലൈൻ അസിസ്റ്റന്റ് ആണ്. എങ്ങനെ സഹായിക്കാൻ കഴിയും?"
            else:
                welcome = "Hello! I'm the online assistant for City Hospital. How can I help you today?"

            if not history or history[-1][1] != welcome:
                return history + [("", welcome)]
            return history

        language.change(
            on_language_change,
            inputs=[language, chat_history],
            outputs=[chat_history]
        ).then(
            lambda history: (history, history),
            inputs=[chat_history],
            outputs=[chatbot, chat_history]
        )

        # Initial welcome message
        demo.load(
            lambda: ([("", "Hello! I'm the online assistant for City Hospital. How can I help you today?")],
                    [("", "Hello! I'm the online assistant for City Hospital. How can I help you today?")]),
            inputs=None,
            outputs=[chatbot, chat_history]
        )

    return demo

# ===== MAIN EXECUTION =====

def main():
    global HF_TOKEN

    print("===== Malayalam Hospital Booking Chatbot =====")
    print("Using Llama 3.1-8B-Instruct with Google Calendar integration")

    # Install required packages in Colab
    try:
        import IPython
        print("Installing required packages...")
        IPython.get_ipython().system('pip install transformers>=4.37.0')
        IPython.get_ipython().system('pip install accelerate>=0.25.0')
        IPython.get_ipython().system('pip install bitsandbytes>=0.41.0')
        IPython.get_ipython().system('pip install sentencepiece>=0.1.99')
        IPython.get_ipython().system('pip install gradio==3.50.2')
        IPython.get_ipython().system('pip install google-auth google-auth-oauthlib google-auth-httplib2')
        IPython.get_ipython().system('pip install google-api-python-client')
        IPython.get_ipython().system('pip install pytz')
        print("All packages installed successfully!")
    except:
        print("Not running in IPython environment or packages already installed.")

    # Get HF token from user input
    HF_TOKEN = input("Enter your Hugging Face token with access to meta-llama models: ")

    # Load the Llama model and tokenizer
    model, tokenizer = load_llama_model()

    if model is None or tokenizer is None:
        print("Failed to load the model. Please check your Hugging Face token and try again.")
        return

    # Get calendar service
    calendar_service = get_calendar_service()

    # Create and launch the Gradio interface
    demo = create_gradio_interface(model, tokenizer, calendar_service)
    demo.launch(share=True, debug=True)

if __name__ == "__main__":
    main()