Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
-
from peft import PeftModel
|
| 7 |
|
| 8 |
-
# ---------
|
| 9 |
-
|
| 10 |
-
LORA_MODEL_ID = "nitya001/autotrain-4n1y9-5ekvs" # <-- change if your repo name differs
|
| 11 |
|
|
|
|
| 12 |
MAX_NEW_TOKENS = 128
|
| 13 |
TEMPERATURE = 0.7
|
| 14 |
TOP_P = 0.9
|
|
@@ -17,84 +15,68 @@ SYSTEM_PROMPT = (
|
|
| 17 |
"You are a helpful banking and loan support assistant. "
|
| 18 |
"You answer short, clear, and factual responses about UTRs, EMIs, "
|
| 19 |
"loan summaries, and payment issues based ONLY on the given question. "
|
| 20 |
-
"If you don't know something
|
| 21 |
-
"cannot access real-time systems and answer generically."
|
| 22 |
)
|
| 23 |
|
| 24 |
# --------- LOAD MODEL ---------
|
| 25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 29 |
|
| 30 |
-
# TinyLlama uses eos_token as pad_token sometimes; ensure it's set
|
| 31 |
if tokenizer.pad_token is None:
|
| 32 |
tokenizer.pad_token = tokenizer.eos_token
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
torch_dtype=torch.float32,
|
| 37 |
-
device_map=None,
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
print(f"Loading LoRA adapter: {LORA_MODEL_ID}...")
|
| 41 |
-
model = PeftModel.from_pretrained(
|
| 42 |
-
base_model,
|
| 43 |
-
LORA_MODEL_ID,
|
| 44 |
)
|
| 45 |
model.to(device)
|
| 46 |
model.eval()
|
| 47 |
|
| 48 |
# --------- CHAT LOGIC ---------
|
| 49 |
-
def
|
| 50 |
-
"""
|
| 51 |
-
Convert chat history + new user message into a single prompt string.
|
| 52 |
-
For now, we keep it simple: a system prompt + last few turns.
|
| 53 |
-
"""
|
| 54 |
parts = [f"System: {SYSTEM_PROMPT}"]
|
| 55 |
-
for
|
| 56 |
-
parts.append(f"User: {
|
| 57 |
-
parts.append(f"Assistant: {
|
| 58 |
-
parts.append(f"User: {
|
| 59 |
parts.append("Assistant:")
|
| 60 |
return "\n".join(parts)
|
| 61 |
|
| 62 |
|
| 63 |
-
def generate_reply(
|
| 64 |
-
if not
|
| 65 |
return history
|
| 66 |
|
| 67 |
-
|
| 68 |
-
prompt = format_chat_history(history, user_message)
|
| 69 |
|
| 70 |
inputs = tokenizer(
|
| 71 |
prompt,
|
| 72 |
return_tensors="pt",
|
| 73 |
truncation=True,
|
| 74 |
-
max_length=
|
| 75 |
).to(device)
|
| 76 |
|
| 77 |
with torch.no_grad():
|
| 78 |
output_ids = model.generate(
|
| 79 |
**inputs,
|
| 80 |
max_new_tokens=MAX_NEW_TOKENS,
|
| 81 |
-
do_sample=True,
|
| 82 |
temperature=TEMPERATURE,
|
| 83 |
top_p=TOP_P,
|
|
|
|
| 84 |
pad_token_id=tokenizer.pad_token_id,
|
| 85 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 86 |
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
full_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
if "Assistant:" in
|
| 93 |
-
bot_reply =
|
| 94 |
else:
|
| 95 |
-
bot_reply =
|
| 96 |
|
| 97 |
-
history.append((
|
| 98 |
return history
|
| 99 |
|
| 100 |
|
|
@@ -102,46 +84,21 @@ def generate_reply(user_message, history):
|
|
| 102 |
with gr.Blocks(title="TinyLoan Assistant") as demo:
|
| 103 |
gr.Markdown(
|
| 104 |
"""
|
| 105 |
-
# 💬 TinyLoan Assistant
|
| 106 |
-
Ask about UTRs, EMIs,
|
| 107 |
-
|
| 108 |
-
> **Note:** This demo does not access real bank systems.
|
| 109 |
-
> It answers based on patterns learned from example data.
|
| 110 |
"""
|
| 111 |
)
|
| 112 |
|
| 113 |
-
chatbot = gr.Chatbot(
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
-
user_input = gr.Textbox(
|
| 121 |
-
show_label=False,
|
| 122 |
-
placeholder="Type your question, e.g 'What is my latest UTR?'",
|
| 123 |
-
scale=4,
|
| 124 |
-
)
|
| 125 |
-
send_btn = gr.Button("Send", scale=1)
|
| 126 |
-
|
| 127 |
-
clear_btn = gr.Button("Clear chat")
|
| 128 |
-
|
| 129 |
-
def respond(message, chat_history):
|
| 130 |
-
if chat_history is None:
|
| 131 |
-
chat_history = []
|
| 132 |
-
return generate_reply(message, chat_history)
|
| 133 |
-
|
| 134 |
-
send_btn.click(
|
| 135 |
-
respond,
|
| 136 |
-
inputs=[user_input, chatbot],
|
| 137 |
-
outputs=[chatbot],
|
| 138 |
-
)
|
| 139 |
-
user_input.submit(
|
| 140 |
-
respond,
|
| 141 |
-
inputs=[user_input, chatbot],
|
| 142 |
-
outputs=[chatbot],
|
| 143 |
)
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 5 |
|
| 6 |
+
# --------- MODEL ID ---------
|
| 7 |
+
MODEL_ID = "nitya001/autotrain-4n1y9-5ekvs" # final trained model
|
|
|
|
| 8 |
|
| 9 |
+
# --------- GENERATION CONFIG ---------
|
| 10 |
MAX_NEW_TOKENS = 128
|
| 11 |
TEMPERATURE = 0.7
|
| 12 |
TOP_P = 0.9
|
|
|
|
| 15 |
"You are a helpful banking and loan support assistant. "
|
| 16 |
"You answer short, clear, and factual responses about UTRs, EMIs, "
|
| 17 |
"loan summaries, and payment issues based ONLY on the given question. "
|
| 18 |
+
"If you don't know something, respond generically."
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# --------- LOAD MODEL ---------
|
| 22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
print(f"Loading FULL model: {MODEL_ID} on {device}...")
|
| 24 |
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
| 26 |
|
|
|
|
| 27 |
if tokenizer.pad_token is None:
|
| 28 |
tokenizer.pad_token = tokenizer.eos_token
|
| 29 |
|
| 30 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
+
MODEL_ID,
|
| 32 |
torch_dtype=torch.float32,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
model.to(device)
|
| 35 |
model.eval()
|
| 36 |
|
| 37 |
# --------- CHAT LOGIC ---------
|
| 38 |
+
def format_chat(history, user_msg):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
parts = [f"System: {SYSTEM_PROMPT}"]
|
| 40 |
+
for u, a in history:
|
| 41 |
+
parts.append(f"User: {u}")
|
| 42 |
+
parts.append(f"Assistant: {a}")
|
| 43 |
+
parts.append(f"User: {user_msg}")
|
| 44 |
parts.append("Assistant:")
|
| 45 |
return "\n".join(parts)
|
| 46 |
|
| 47 |
|
| 48 |
+
def generate_reply(user_msg, history):
|
| 49 |
+
if not user_msg.strip():
|
| 50 |
return history
|
| 51 |
|
| 52 |
+
prompt = format_chat(history, user_msg)
|
|
|
|
| 53 |
|
| 54 |
inputs = tokenizer(
|
| 55 |
prompt,
|
| 56 |
return_tensors="pt",
|
| 57 |
truncation=True,
|
| 58 |
+
max_length=768,
|
| 59 |
).to(device)
|
| 60 |
|
| 61 |
with torch.no_grad():
|
| 62 |
output_ids = model.generate(
|
| 63 |
**inputs,
|
| 64 |
max_new_tokens=MAX_NEW_TOKENS,
|
|
|
|
| 65 |
temperature=TEMPERATURE,
|
| 66 |
top_p=TOP_P,
|
| 67 |
+
do_sample=True,
|
| 68 |
pad_token_id=tokenizer.pad_token_id,
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
+
text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
|
|
|
| 72 |
|
| 73 |
+
# Extract assistant response
|
| 74 |
+
if "Assistant:" in text:
|
| 75 |
+
bot_reply = text.split("Assistant:")[-1].strip()
|
| 76 |
else:
|
| 77 |
+
bot_reply = text.strip()
|
| 78 |
|
| 79 |
+
history.append((user_msg, bot_reply))
|
| 80 |
return history
|
| 81 |
|
| 82 |
|
|
|
|
| 84 |
with gr.Blocks(title="TinyLoan Assistant") as demo:
|
| 85 |
gr.Markdown(
|
| 86 |
"""
|
| 87 |
+
# 💬 TinyLoan Assistant
|
| 88 |
+
Ask about UTRs, EMIs, transactions, payment issues, etc.
|
|
|
|
|
|
|
|
|
|
| 89 |
"""
|
| 90 |
)
|
| 91 |
|
| 92 |
+
chatbot = gr.Chatbot(type="pairs", height=400)
|
| 93 |
+
user_input = gr.Textbox(
|
| 94 |
+
placeholder="Ask your question…",
|
| 95 |
+
show_label=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
+
send = gr.Button("Send")
|
| 98 |
+
clear = gr.Button("Clear")
|
| 99 |
|
| 100 |
+
send.click(generate_reply, [user_input, chatbot], [chatbot])
|
| 101 |
+
user_input.submit(generate_reply, [user_input, chatbot], [chatbot])
|
| 102 |
+
clear.click(lambda: [], None, [chatbot])
|
| 103 |
|
| 104 |
demo.launch()
|