Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,8 @@ torch.set_num_threads(1)
|
|
| 10 |
print("🤖 Starting Model Loading...")
|
| 11 |
|
| 12 |
try:
|
| 13 |
-
|
|
|
|
| 14 |
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
CHAT_MODEL = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 16 |
print("✅ Model loaded successfully.")
|
|
@@ -31,7 +32,7 @@ def detect_script(text):
|
|
| 31 |
|
| 32 |
|
| 33 |
# ---------------------------------------------------------
|
| 34 |
-
# 3️⃣
|
| 35 |
# ---------------------------------------------------------
|
| 36 |
def generate_reply(user_text):
|
| 37 |
"""Generate Sindhi or Roman Sindhi reply based on input language."""
|
|
@@ -40,20 +41,27 @@ def generate_reply(user_text):
|
|
| 40 |
|
| 41 |
script_type = detect_script(user_text)
|
| 42 |
|
|
|
|
| 43 |
if script_type == "sindhi":
|
| 44 |
-
prompt = f"
|
| 45 |
else:
|
| 46 |
-
prompt = f"
|
| 47 |
|
| 48 |
-
inputs = TOKENIZER(prompt, return_tensors="pt")
|
| 49 |
outputs = CHAT_MODEL.generate(
|
| 50 |
**inputs,
|
| 51 |
max_new_tokens=100,
|
| 52 |
do_sample=True,
|
| 53 |
top_p=0.95,
|
| 54 |
-
top_k=
|
|
|
|
| 55 |
)
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
# ---------------------------------------------------------
|
|
|
|
| 10 |
print("🤖 Starting Model Loading...")
|
| 11 |
|
| 12 |
try:
|
| 13 |
+
# Use small version for faster inference and better generalization
|
| 14 |
+
MODEL_NAME = "google/mt5-small"
|
| 15 |
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 16 |
CHAT_MODEL = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 17 |
print("✅ Model loaded successfully.")
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# ---------------------------------------------------------
|
| 35 |
+
# 3️⃣ Chat Function
|
| 36 |
# ---------------------------------------------------------
|
| 37 |
def generate_reply(user_text):
|
| 38 |
"""Generate Sindhi or Roman Sindhi reply based on input language."""
|
|
|
|
| 41 |
|
| 42 |
script_type = detect_script(user_text)
|
| 43 |
|
| 44 |
+
# Improved instruction-style prompts
|
| 45 |
if script_type == "sindhi":
|
| 46 |
+
prompt = f"توھان جو ڪم ھي آھي ته ھيٺين جملي جو جواب سنڌي ۾ ڏيو:\nسوال: {user_text}\nجواب:"
|
| 47 |
else:
|
| 48 |
+
prompt = f"Tuhanjo kaam aahe ta neeche likhe sawal jo jawab Roman Sindhi mein likho:\nSawaal: {user_text}\nJawab:"
|
| 49 |
|
| 50 |
+
inputs = TOKENIZER(prompt, return_tensors="pt", truncation=True)
|
| 51 |
outputs = CHAT_MODEL.generate(
|
| 52 |
**inputs,
|
| 53 |
max_new_tokens=100,
|
| 54 |
do_sample=True,
|
| 55 |
top_p=0.95,
|
| 56 |
+
top_k=40,
|
| 57 |
+
temperature=0.8
|
| 58 |
)
|
| 59 |
+
|
| 60 |
+
reply = TOKENIZER.decode(outputs[0], skip_special_tokens=True)
|
| 61 |
+
|
| 62 |
+
# Remove unwanted tokens (like <extra_id_0>, etc.)
|
| 63 |
+
reply = re.sub(r"<.*?>", "", reply).strip()
|
| 64 |
+
return reply
|
| 65 |
|
| 66 |
|
| 67 |
# ---------------------------------------------------------
|