Update app.py
Browse files
app.py
CHANGED
|
@@ -19,20 +19,20 @@ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
| 19 |
# ๐น ุชุญุฏูุฏ ุงูุฌูุงุฒ ุงูู
ูุงุณุจ (GPU ุฃู CPU)
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
|
| 22 |
-
# ๐น ุชุญู
ูู ุงููู
ูุฐุฌ ูุงูู
ุญูู
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
model_name,
|
| 25 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 26 |
-
device_map="
|
| 27 |
trust_remote_code=True
|
| 28 |
-
)
|
| 29 |
|
| 30 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 31 |
|
| 32 |
# ๐น ุญู ู
ุดููุฉ ุงูู padding
|
| 33 |
tokenizer.pad_token = tokenizer.eos_token
|
| 34 |
|
| 35 |
-
# ๐น ุฑุฏูุฏ ู
ุฎุตุตุฉ
|
| 36 |
custom_responses = {
|
| 37 |
"ู
ุง ูู ูุฏููุ": "ูุฏูู ูู ุชูุฏูู
ุงูู
ุณุงุนุฏุฉ ูุงูุฅุฌุงุจุฉ ุนูู ุฃุณุฆูุชู ุจุฃูุถู ุทุฑููุฉ ู
ู
ููุฉ.",
|
| 38 |
"who created you?": "I was created by George.",
|
|
@@ -45,9 +45,9 @@ custom_responses = {
|
|
| 45 |
"ไฝ ็็ผ็จ่
ๆฏ่ฐ๏ผ": "ๆ็็ผ็จ่
ๆฏ Georgeใ",
|
| 46 |
}
|
| 47 |
|
| 48 |
-
# ๐น ุฏุงูุฉ ุงูุฏุฑุฏุดุฉ
|
| 49 |
def chatbot(user_input):
|
| 50 |
-
if not user_input.strip():
|
| 51 |
return "Please enter a message."
|
| 52 |
|
| 53 |
user_input = user_input.lower()
|
|
@@ -57,11 +57,19 @@ def chatbot(user_input):
|
|
| 57 |
if question in user_input:
|
| 58 |
return answer
|
| 59 |
|
| 60 |
-
#
|
| 61 |
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 62 |
|
| 63 |
-
with torch.no_grad(): # ุชุญุณูู ุงู
|
| 64 |
-
output = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 67 |
|
|
|
|
| 19 |
# ๐น ุชุญุฏูุฏ ุงูุฌูุงุฒ ุงูู
ูุงุณุจ (GPU ุฃู CPU)
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
|
| 22 |
+
# ๐น ุชุญู
ูู ุงููู
ูุฐุฌ ูุงูู
ุญูู ู
ุน ุชุญุณูู ุงูุฃุฏุงุก
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
model_name,
|
| 25 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 26 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu",
|
| 27 |
trust_remote_code=True
|
| 28 |
+
).eval() # ๐ฅ ุชุญุณูู ุงูุณุฑุนุฉ ุจุชุนุทูู ุงูุชุฏุฑูุจ
|
| 29 |
|
| 30 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 31 |
|
| 32 |
# ๐น ุญู ู
ุดููุฉ ุงูู padding
|
| 33 |
tokenizer.pad_token = tokenizer.eos_token
|
| 34 |
|
| 35 |
+
# ๐น ุฑุฏูุฏ ู
ุฎุตุตุฉ (Instant Responses)
|
| 36 |
custom_responses = {
|
| 37 |
"ู
ุง ูู ูุฏููุ": "ูุฏูู ูู ุชูุฏูู
ุงูู
ุณุงุนุฏุฉ ูุงูุฅุฌุงุจุฉ ุนูู ุฃุณุฆูุชู ุจุฃูุถู ุทุฑููุฉ ู
ู
ููุฉ.",
|
| 38 |
"who created you?": "I was created by George.",
|
|
|
|
| 45 |
"ไฝ ็็ผ็จ่
ๆฏ่ฐ๏ผ": "ๆ็็ผ็จ่
ๆฏ Georgeใ",
|
| 46 |
}
|
| 47 |
|
| 48 |
+
# ๐น ุฏุงูุฉ ุงูุฏุฑุฏุดุฉ ุงูุณุฑูุนุฉ
|
| 49 |
def chatbot(user_input):
|
| 50 |
+
if not user_input.strip():
|
| 51 |
return "Please enter a message."
|
| 52 |
|
| 53 |
user_input = user_input.lower()
|
|
|
|
| 57 |
if question in user_input:
|
| 58 |
return answer
|
| 59 |
|
| 60 |
+
# ๐ฅ ุชุญุณูู ุงูุฃุฏุงุก ุจุงุณุชุฎุฏุงู
ุงูู
ุนูู
ุงุช ุงูุชุงููุฉ:
|
| 61 |
inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 62 |
|
| 63 |
+
with torch.no_grad(): # ุชุญุณูู ุงูุณุฑุนุฉ
|
| 64 |
+
output = model.generate(
|
| 65 |
+
**inputs,
|
| 66 |
+
max_length=50, # ๐น ุชูููู ุงูุทูู ุงูุฃูุตู ูุชุณุฑูุน ุงูุงุณุชุฌุงุจุฉ
|
| 67 |
+
temperature=0.7, # ๐น ุถุจุท ุงูุญุฑุงุฑุฉ ูุฌุนู ุงูุฑุฏูุฏ ุฃุณุฑุน ูุฃูุถู
|
| 68 |
+
top_p=0.9, # ๐น ุชูููู ุงูุงุญุชู
ุงูุงุช ุบูุฑ ุงูู
ููุฏุฉ
|
| 69 |
+
do_sample=True, # ๐น ุงูุณู
ุงุญ ุจุงูุชูููุฏ ุงูุนุดูุงุฆู ูุชุฌูุจ ุงูุชูุฑุงุฑ
|
| 70 |
+
early_stopping=True, # ๐น ุฅููุงุก ุงูุชูููุฏ ุนูุฏู
ุง ูููู ูุงูููุง
|
| 71 |
+
pad_token_id=tokenizer.eos_token_id
|
| 72 |
+
)
|
| 73 |
|
| 74 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 75 |
|