Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
from huggingface_hub import login
|
| 4 |
import torch
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
-
|
| 10 |
-
# ๐น ุชุณุฌูู ุงูุฏุฎูู ุฅูู Hugging Face
|
| 11 |
-
if hf_token:
|
| 12 |
-
login(token=hf_token)
|
| 13 |
-
else:
|
| 14 |
-
raise ValueError("Hugging Face token is missing. Please check your secrets.")
|
| 15 |
-
|
| 16 |
-
# ๐น ุงุณุชุฎุฏุงู
ูู
ูุฐุฌ TinyLlama ุงูุฃุฎู
|
| 17 |
-
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 18 |
-
|
| 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 |
-
).eval()
|
| 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.",
|
| 39 |
-
"who programmed you?": "My programmer is George.",
|
| 40 |
-
"what is your name": "I am Octagon 2.0.",
|
| 41 |
-
"do you have an owner?": "I am owned by George.",
|
| 42 |
-
"what is your purpose": "My purpose is to assist and answer questions in the best way.",
|
| 43 |
-
"ไป็ปไฝ ่ชๅทฑ": "ๆๆฏ Octagon 2.0ใ",
|
| 44 |
-
"ไฝ ๆฏ่ฐๅ้ ็๏ผ": "ๆๆฏ็ฑ 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 |
-
|
| 54 |
-
|
| 55 |
-
# ุงูุชุญูู ู
ู ุงูุฑุฏูุฏ ุงูู
ุฎุตุตุฉ ุฃููุงู
|
| 56 |
-
for question, answer in custom_responses.items():
|
| 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.
|
| 68 |
-
top_p=0.
|
| 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 |
-
|
| 76 |
return response
|
| 77 |
|
| 78 |
-
#
|
| 79 |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Octagon 2.0 Chatbot")
|
| 80 |
iface.launch(share=True)
|
|
|
|
| 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 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
|
|
| 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 |
**inputs,
|
| 31 |
+
max_length=50, # ๐ข ุชูููู ุงูุทูู ูุชุณุฑูุน ุงูุชูููุฏ
|
| 32 |
+
temperature=0.6, # ๐ข ุชูููู ุงูุนุดูุงุฆูุฉ
|
| 33 |
+
top_p=0.8, # ๐ข ุงุฎุชูุงุฑ ุงูููู
ุงุช ุงูุฃูุซุฑ ุงุญุชู
ุงููุฉ
|
| 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 |
iface.launch(share=True)
|