Views
No views yet
!pip install unsloth1from unsloth import FastLanguageModel
2import torch
3
4# Define the Alpaca prompt template
5alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
6### Instruction:
7{instruction}
8### Input:
9{input_text}
10### Response:
11{output}"""
12
13# Load your model
14model, tokenizer = FastLanguageModel.from_pretrained(
15 model_name="Subh775/mistral-7b-medical-o1-ft",
16 max_seq_length=2048,
17 load_in_4bit=True
18)
19
20# Enable optimized inference mode for faster generation
21FastLanguageModel.for_inference(model)1# Function to handle the chat loop with memory
2
3def chat():
4 print("Chat with mistral-7b-medical-o1-ft! Type '\\q' or 'quit' to stop.\n")
5
6 chat_history = "" # Store the conversation history
7
8 while True:
9 # Get user input
10 user_input = input("➤ ")
11
12 # Exit condition
13 if user_input.lower() in ['\\q', 'quit']:
14 print("\nExiting the chat. Goodbye 🩺👍!")
15 print("✨" + "=" * 27 + "✨\n")
16 break
17
18 # Append the current input to chat history with instruction formatting
19 prompt = alpaca_prompt.format(
20 instruction="Please answer the following medical question.",
21 input_text=user_input,
22 output=""
23 )
24 chat_history += prompt + "\n"
25
26 # Tokenize combined history and move to GPU
27 inputs = tokenizer([chat_history], return_tensors="pt").to("cuda")
28
29 # Generate output with configured parameters
30 outputs = model.generate(
31 **inputs,
32 max_new_tokens=256,
33 temperature=0.7,
34 top_p=0.9,
35 num_return_sequences=1,
36 do_sample=True,
37 no_repeat_ngram_size=2
38 )
39
40 # Decode and clean the model's response
41 decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)
42 clean_output = decoded_output[0].split('### Response:')[-1].strip()
43
44 # Add the response to chat history
45 chat_history += f": {clean_output}\n"
46
47 # Display the response
48 print(f"\n🧑⚕️: {clean_output}\n")
49
50# Start the chat
51chat()
52
531@misc{mistral-7b-medical-o1-ft,
2 author = {Subh775},
3 title = {Mistral-7B Medical QA Model},
4 year = {2025},
5 publisher = {HuggingFace},
6 journal = {HuggingFace Repository},
7 howpublished = {\url{https://huggingface.co/Subh775/mistral-7b-medical-o1-ft}}
8}