Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
)
|
| 62 |
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py inside your Hugging Face Space
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 8 |
+
|
| 9 |
+
# 1. SeaLLM model focused on SEA languages (includes Lao)
|
| 10 |
+
MODEL_NAME = "SeaLLMs/SeaLLMs-v3-1.5B-Chat" # 1.5B chat model:contentReference[oaicite:6]{index=6}
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_NAME,
|
| 15 |
+
torch_dtype=torch.float32, # CPU on free tier → float32
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# 2. Load Laos history knowledge
|
| 19 |
+
DATA_PATH = "data/laos_history.txt"
|
| 20 |
+
if os.path.exists(DATA_PATH):
|
| 21 |
+
with open(DATA_PATH, "r", encoding="utf-8") as f:
|
| 22 |
+
RAW_KNOWLEDGE = f.read()
|
| 23 |
+
else:
|
| 24 |
+
RAW_KNOWLEDGE = "ຍັງບໍ່ມີຂໍ້ມູນປະຫວັດສາດຖືກໂຫຼດ."
|
| 25 |
+
|
| 26 |
+
# Split into paragraphs for simple retrieval
|
| 27 |
+
PARAGRAPHS = [p.strip() for p in RAW_KNOWLEDGE.split("\n\n") if p.strip()]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def retrieve_context(question: str, max_paragraphs: int = 5) -> str:
|
| 31 |
"""
|
| 32 |
+
VERY simple keyword-based retrieval over PARAGRAPHS.
|
| 33 |
+
Good enough for first prototype; later you can replace with embeddings.
|
| 34 |
"""
|
| 35 |
+
if not PARAGRAPHS:
|
| 36 |
+
return RAW_KNOWLEDGE
|
| 37 |
+
|
| 38 |
+
# tokenize question into simple lowercased words
|
| 39 |
+
terms = [w for w in re.split(r"\s+", question.lower()) if len(w) > 2]
|
| 40 |
+
if not terms:
|
| 41 |
+
return "\n\n".join(PARAGRAPHS[:max_paragraphs])
|
| 42 |
+
|
| 43 |
+
scored = []
|
| 44 |
+
for p in PARAGRAPHS:
|
| 45 |
+
p_lower = p.lower()
|
| 46 |
+
score = sum(p_lower.count(t) for t in terms)
|
| 47 |
+
if score > 0:
|
| 48 |
+
scored.append((score, p))
|
| 49 |
+
|
| 50 |
+
scored.sort(key=lambda x: x[0], reverse=True)
|
| 51 |
+
top = [p for _, p in scored[:max_paragraphs]]
|
| 52 |
+
if not top:
|
| 53 |
+
top = PARAGRAPHS[:max_paragraphs]
|
| 54 |
+
|
| 55 |
+
return "\n\n".join(top)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
SYSTEM_PROMPT = (
|
| 59 |
+
"ທ່ານແມ່ນຜູ້ຊ່ວຍເຫຼືອດ້ານປະຫວັດສາດຂອງປະເທດລາວ. "
|
| 60 |
+
"ຕອບແຕ່ພາສາລາວ, ອະທິບາຍໃຫ້ເຂົ້າໃຈງ່າຍ ແລະສັ້ນກະທັດຮັດ. "
|
| 61 |
+
"ໃຫ້ອີງຈາກຂໍ້ມູນຂ້າງລຸ່ມນີ້ເທົ່ານັ້ນ. "
|
| 62 |
+
"ຖ້າຂໍ້ມູນບໍ່ພຽງພໍ ຫຼືບໍ່ຊັດເຈນ ໃຫ້ບອກວ່າບໍ່ແນ່ໃຈ."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
def build_prompt(question: str) -> str:
|
| 67 |
+
context = retrieve_context(question)
|
| 68 |
+
prompt = f"""{SYSTEM_PROMPT}
|
| 69 |
+
|
| 70 |
+
ຂໍ້ມູນອ້າງອີງ:
|
| 71 |
+
{context}
|
| 72 |
+
|
| 73 |
+
ຄຳຖາມ: {question}
|
| 74 |
+
|
| 75 |
+
ຄຳຕອບດ້ວຍພາສາລາວ:"""
|
| 76 |
+
return prompt
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def generate_answer(question: str) -> str:
|
| 80 |
+
prompt = build_prompt(question)
|
| 81 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 82 |
+
with torch.no_grad():
|
| 83 |
+
outputs = model.generate(
|
| 84 |
+
**inputs,
|
| 85 |
+
max_new_tokens=256,
|
| 86 |
+
do_sample=True,
|
| 87 |
+
temperature=0.7,
|
| 88 |
+
top_p=0.9,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# slice off the prompt part
|
| 92 |
+
generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 93 |
+
answer = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 94 |
+
# clean up a bit
|
| 95 |
+
return answer.strip()
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# 3. Gradio chat function
|
| 99 |
+
def laos_history_bot(message: str, history: list):
|
| 100 |
+
"""
|
| 101 |
+
Gradio ChatInterface expects (message, history) and returns a string.
|
| 102 |
+
We ignore history for now (you can later use it in the prompt).
|
| 103 |
+
"""
|
| 104 |
+
if not message.strip():
|
| 105 |
+
return "ກະລຸນາພິມຄຳຖາມກ່ອນ."
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
answer = generate_answer(message)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
# simple fallback if something goes wrong
|
| 111 |
+
return f"ລະບົບມີບັນຫາ: {e}"
|
| 112 |
+
|
| 113 |
+
return answer
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
demo = gr.ChatInterface(
|
| 117 |
+
fn=laos_history_bot,
|
| 118 |
+
title="Laos History Chatbot (Lao language)",
|
| 119 |
+
description="ຖາມຂໍ້ມູນກ່ຽວກັບປະຫວັດສາດຂອງປະເທດລາວ",
|
| 120 |
+
examples=[
|
| 121 |
+
"ອານາຈັກລ້ານຊ້າງເກີດຂຶ້ນໃນປີໃດ?",
|
| 122 |
+
"ເມືອງຫວຽງຈັນເຄີຍເປັນນະຄອນຫຼວງຂອງອານາຈັກໃດ?",
|
| 123 |
+
],
|
| 124 |
+
)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
demo.launch()
|