-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_model.py
76 lines (63 loc) · 2.21 KB
/
test_model.py
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
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
from datetime import datetime
from dotenv import load_dotenv
# Load the environment variables from the .env file
load_dotenv()
# Local path to your saved model
model_path = "./training/models/fine_tuned_model"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
# Use pipeline for text generation
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# model_id = "coloredcow/paani-1b-instruct-marathi"
# pipe = pipeline(
# "text-generation",
# model=model_id,
# torch_dtype=torch.bfloat16,
# device_map="auto",
# )
selected_language = 'en'
language_configs = {
"en": {
"chatbot_instruction": "Please answer the following question in English:\n",
},
"hi": {
"chatbot_instruction": "कृपया निम्नलिखित प्रश्न का उत्तर हिंदी में दें:\n",
},
"mr": {
"chatbot_instruction": "कृपया पुढील प्रश्नाचे उत्तर मराठीत द्या:\n",
},
"bn": {
"chatbot_instruction": "দয়া করে নিচের প্রশ্নের উত্তর দিন মারাঠিতে:\n",
},
}
def get_chatbot_response(input_text, language):
instruction = language_configs[language]['chatbot_instruction']
prompt = instruction + input_text
messages = [{"role": "user", "content": prompt}]
outputs = pipe(
messages,
max_new_tokens=256,
do_sample=False,
)
response = outputs[0]["generated_text"]
return response
# Interactive loop for user input
print("Chatbot is ready! Type 'exit' to quit.\n")
while True:
# Prompt the user for input
user_input = input("You: ")
# Check if the user wants to exit
if user_input.lower() in ["exit", "quit"]:
print("Exiting the chatbot. Goodbye!")
break
# Get and print the chatbot response
response_text = get_chatbot_response(user_input, selected_language)
print("Chatbot:", response_text)