Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
# ๐ฅ ุชุญุฏูุฏ ุงูุฌูุงุฒ ุงูู
ูุงุณุจ
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
-
# ๐ฅ ุชุญู
ูู ุงูู
ูุฏูู ุจุทุฑููุฉ
|
| 10 |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
model_name,
|
| 13 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
-
device_map="auto",
|
| 15 |
trust_remote_code=True
|
| 16 |
).eval()
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
|
| 21 |
-
# ๐ฅ
|
| 22 |
def chatbot(user_input):
|
| 23 |
if not user_input.strip():
|
| 24 |
return "Please enter a message."
|
| 25 |
|
| 26 |
-
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
| 27 |
|
| 28 |
with torch.no_grad():
|
| 29 |
output = model.generate(
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
do_sample=True,
|
| 35 |
early_stopping=True,
|
| 36 |
-
num_return_sequences=1,
|
| 37 |
pad_token_id=tokenizer.eos_token_id
|
| 38 |
)
|
| 39 |
|
| 40 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 41 |
return response
|
| 42 |
|
| 43 |
-
# ๐ฅ ุชุดุบูู ุงููุงุฌูุฉ
|
| 44 |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Octagon 2.0 Chatbot")
|
| 45 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
# ๐ฅ ุชุญุฏูุฏ ุงูุฌูุงุฒ ุงูู
ูุงุณุจ
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
|
| 8 |
+
# ๐ฅ ุชุญู
ูู ุงูู
ูุฏูู ุจุทุฑููุฉ ู
ุญุณูุฉ
|
| 9 |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_name,
|
| 12 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 13 |
+
device_map="auto" if torch.cuda.is_available() else None, # โ
ุถุจุท ุงูู device_map ุจุดูู ุฃุฏู
|
| 14 |
trust_remote_code=True
|
| 15 |
).eval()
|
| 16 |
|
| 17 |
+
# ๐ฅ ุชุญู
ูู ุงูุชููููุฒุฑ
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 19 |
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
|
| 21 |
+
# ๐ฅ ุฅุนุฏุงุฏ ุงูุฏุงูุฉ ุงูุฎุงุตุฉ ุจุงูู Chatbot
|
| 22 |
def chatbot(user_input):
|
| 23 |
if not user_input.strip():
|
| 24 |
return "Please enter a message."
|
| 25 |
|
| 26 |
+
inputs = tokenizer(user_input, return_tensors="pt", padding=True).to(device)
|
| 27 |
|
| 28 |
with torch.no_grad():
|
| 29 |
output = model.generate(
|
| 30 |
+
input_ids=inputs["input_ids"],
|
| 31 |
+
attention_mask=inputs["attention_mask"], # โ
ุฅุตูุงุญ ู
ุดููุฉ ุงูู
ุงุณู
|
| 32 |
+
max_new_tokens=50, # โ
ุงุณุชุฎุฏุงู
max_new_tokens ุจุฏูุงู ู
ู max_length
|
| 33 |
+
temperature=0.6,
|
| 34 |
+
top_p=0.8,
|
| 35 |
do_sample=True,
|
| 36 |
early_stopping=True,
|
|
|
|
| 37 |
pad_token_id=tokenizer.eos_token_id
|
| 38 |
)
|
| 39 |
|
| 40 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 41 |
return response
|
| 42 |
|
| 43 |
+
# ๐ฅ ุชุดุบูู ุงููุงุฌูุฉ ุจุฏูู share=True
|
| 44 |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Octagon 2.0 Chatbot")
|
| 45 |
+
|
| 46 |
+
# โ
ุฅุตูุงุญ ู
ุดููุฉ POST method not allowed
|
| 47 |
+
iface.launch(ssl=False, debug=True)
|