File size: 851 Bytes
dab58a7
 
cfc994d
dab58a7
72ef74b
cfc994d
dab58a7
 
 
 
 
 
 
cfc994d
 
dab58a7
 
 
 
 
cfc994d
dab58a7
 
 
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
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# ✅ Choose a Hugging Face Model
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1"  # Change model if needed

# ✅ Load Tokenizer & Model
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME, 
    torch_dtype=torch.float16,  # Use float16 for efficiency
    device_map="auto"  # Auto-detect GPU/CPU
)

def ask_book(question):
    """Generates a response based on a given book-related question."""
    inputs = tokenizer(question, return_tensors="pt").to(model.device)
    outputs = model.generate(**inputs, max_length=200)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return response

# Example test
if __name__ == "__main__":
    print(ask_book("What is the summary of the book?"))