Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import torch
|
| 2 |
import time
|
| 3 |
import re
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
# Load tokenizer and model
|
|
@@ -9,12 +9,23 @@ model_path = "supp03/E3L-AI-CHATBOT"
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, device_map="auto")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
# Simple context memory
|
| 13 |
context_memory = {
|
| 14 |
"last_question": None,
|
| 15 |
"last_answer": None
|
| 16 |
}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def generate_response(user_input):
|
| 19 |
try:
|
| 20 |
if not user_input.strip():
|
|
@@ -44,25 +55,35 @@ def generate_response(user_input):
|
|
| 44 |
else:
|
| 45 |
style_instruction = "Write in a natural, conversational paragraph form."
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"
|
| 50 |
-
"Avoid sounding robotic or generic. Respond with empathy, clarity, and human warmth.\n\n"
|
| 51 |
f"{style_instruction}\n\n"
|
| 52 |
f"{context}Question: {user_input.strip()}\n\nAnswer:"
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Tokenize
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
start_time = time.time()
|
| 59 |
-
with torch.
|
| 60 |
output_ids = model.generate(
|
| 61 |
input_ids,
|
| 62 |
-
max_new_tokens=
|
| 63 |
-
do_sample=False,
|
| 64 |
-
|
| 65 |
-
pad_token_id=tokenizer.eos_token_id
|
|
|
|
| 66 |
)
|
| 67 |
elapsed_time = time.time() - start_time
|
| 68 |
|
|
@@ -82,7 +103,7 @@ def generate_response(user_input):
|
|
| 82 |
context_memory["last_question"] = user_input
|
| 83 |
context_memory["last_answer"] = model_answer
|
| 84 |
|
| 85 |
-
# Timing
|
| 86 |
minutes = int(elapsed_time // 60)
|
| 87 |
seconds = round(elapsed_time % 60, 1)
|
| 88 |
time_display = f"{minutes}m {seconds}s" if minutes > 0 else f"{seconds}s"
|
|
@@ -96,13 +117,13 @@ def generate_response(user_input):
|
|
| 96 |
interface = gr.Interface(
|
| 97 |
fn=generate_response,
|
| 98 |
inputs=gr.Textbox(lines=5, placeholder="Ask your question here...", label="Ask your question"),
|
| 99 |
-
outputs=gr.Textbox(lines=
|
| 100 |
title="Gemma Mentor Chatbot",
|
| 101 |
description="Ask questions and get warm, supportive, emotionally intelligent responses. Type 'exit' to reset.",
|
| 102 |
allow_flagging="never",
|
| 103 |
theme="default",
|
| 104 |
-
live=False,
|
| 105 |
-
api_name="/predict"
|
| 106 |
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import torch
|
| 2 |
import time
|
| 3 |
import re
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
# Load tokenizer and model
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, device_map="auto")
|
| 11 |
|
| 12 |
+
# Enable use_cache explicitly for faster generation
|
| 13 |
+
model.config.use_cache = True
|
| 14 |
+
|
| 15 |
# Simple context memory
|
| 16 |
context_memory = {
|
| 17 |
"last_question": None,
|
| 18 |
"last_answer": None
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Cache the static prompt prefix tokenized once to save time
|
| 22 |
+
static_prompt = (
|
| 23 |
+
"You are a warm, thoughtful, and emotionally intelligent mentor. "
|
| 24 |
+
"Your job is to provide deeply personalized, practical, and supportive guidance. "
|
| 25 |
+
"Avoid sounding robotic or generic. Respond with empathy, clarity, and human warmth.\n\n"
|
| 26 |
+
)
|
| 27 |
+
cached_static_prompt_ids = tokenizer(static_prompt, return_tensors="pt").input_ids.to(model.device)
|
| 28 |
+
|
| 29 |
def generate_response(user_input):
|
| 30 |
try:
|
| 31 |
if not user_input.strip():
|
|
|
|
| 55 |
else:
|
| 56 |
style_instruction = "Write in a natural, conversational paragraph form."
|
| 57 |
|
| 58 |
+
# Combine full prompt text
|
| 59 |
+
full_prompt = (
|
| 60 |
+
f"{static_prompt}"
|
|
|
|
| 61 |
f"{style_instruction}\n\n"
|
| 62 |
f"{context}Question: {user_input.strip()}\n\nAnswer:"
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Tokenize only the user-dependent part to append to cached prompt tokens
|
| 66 |
+
user_prompt_ids = tokenizer(
|
| 67 |
+
full_prompt[len(static_prompt):], # remove static part
|
| 68 |
+
return_tensors="pt"
|
| 69 |
+
).input_ids.to(model.device)
|
| 70 |
+
|
| 71 |
+
# Concatenate cached static prompt tokens with user prompt tokens (skip overlapping BOS token if any)
|
| 72 |
+
# Avoid duplicate BOS token by slicing user_prompt_ids if it starts with tokenizer.bos_token_id
|
| 73 |
+
if user_prompt_ids[0, 0] == tokenizer.bos_token_id:
|
| 74 |
+
user_prompt_ids = user_prompt_ids[:, 1:]
|
| 75 |
+
|
| 76 |
+
input_ids = torch.cat([cached_static_prompt_ids, user_prompt_ids], dim=-1)
|
| 77 |
|
| 78 |
start_time = time.time()
|
| 79 |
+
with torch.inference_mode():
|
| 80 |
output_ids = model.generate(
|
| 81 |
input_ids,
|
| 82 |
+
max_new_tokens=100,
|
| 83 |
+
do_sample=False,
|
| 84 |
+
num_beams=1,
|
| 85 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 86 |
+
use_cache=True
|
| 87 |
)
|
| 88 |
elapsed_time = time.time() - start_time
|
| 89 |
|
|
|
|
| 103 |
context_memory["last_question"] = user_input
|
| 104 |
context_memory["last_answer"] = model_answer
|
| 105 |
|
| 106 |
+
# Timing display
|
| 107 |
minutes = int(elapsed_time // 60)
|
| 108 |
seconds = round(elapsed_time % 60, 1)
|
| 109 |
time_display = f"{minutes}m {seconds}s" if minutes > 0 else f"{seconds}s"
|
|
|
|
| 117 |
interface = gr.Interface(
|
| 118 |
fn=generate_response,
|
| 119 |
inputs=gr.Textbox(lines=5, placeholder="Ask your question here...", label="Ask your question"),
|
| 120 |
+
outputs=gr.Textbox(lines=2, label="Response"),
|
| 121 |
title="Gemma Mentor Chatbot",
|
| 122 |
description="Ask questions and get warm, supportive, emotionally intelligent responses. Type 'exit' to reset.",
|
| 123 |
allow_flagging="never",
|
| 124 |
theme="default",
|
| 125 |
+
live=False,
|
| 126 |
+
api_name="/predict"
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|