Spaces:
Sleeping
Sleeping
File size: 4,221 Bytes
21eae3f dadc389 e8a259b c22fc67 21eae3f e8a259b dadc389 21eae3f e8a259b 42ab218 e8a259b 42ab218 e8a259b 42ab218 fbeee9f dadc389 e8a259b dadc389 e8a259b 21eae3f dadc389 e8a259b dadc389 e8a259b dadc389 21eae3f dadc389 21eae3f e8a259b dadc389 e8a259b 21eae3f dadc389 21eae3f e8a259b fbeee9f dadc389 e8a259b 21eae3f fbeee9f f5300db e8a259b f5300db 21eae3f c22fc67 ba077d3 e8a259b ba077d3 | 1 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import torch
import time
import re
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import gradio as gr
# Load tokenizer and model
model_path = "supp03/E3L-AI-CHATBOT"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, device_map="auto")
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
pad_token_id=tokenizer.eos_token_id
)
# Simple context memory
context_memory = {
"last_question": None,
"last_answer": None
}
def generate_response(user_input):
try:
if not user_input.strip():
return "Please enter a question."
if user_input.strip().lower() == "exit":
context_memory["last_question"] = None
context_memory["last_answer"] = None
return "Session reset. You can ask a new question now."
refer_prev = any(phrase in user_input.lower() for phrase in [
"previous question", "previous answer", "elaborate", "detail", "more info", "explain more", "expand on that"
])
context = ""
if refer_prev and context_memory["last_question"] and context_memory["last_answer"]:
context = (
f"Previous Question: {context_memory['last_question']}\n"
f"Previous Answer: {context_memory['last_answer']}\n\n"
"Please elaborate on the previous answer with more details, examples, and explanations.\n\n"
)
if "steps" in user_input.lower() or "step by step" in user_input.lower():
style_instruction = "Organize your response into a clearly numbered step-by-step list (1., 2., 3., ...)."
elif "how" in user_input.lower() or "ways" in user_input.lower():
style_instruction = "Organize your response into practical bullet points."
else:
style_instruction = "Write in a natural, conversational paragraph form."
generated_prompt = (
"You are a warm, thoughtful, and emotionally intelligent mentor. "
"Your job is to provide deeply personalized, practical, and supportive guidance. "
"Avoid sounding robotic or generic. Respond with empathy, clarity, and human warmth.\n\n"
f"{style_instruction}\n\n"
f"{context}"
f"Question: {user_input.strip()}\n\nAnswer:"
)
start_time = time.time()
with torch.no_grad():
generated = pipe(
generated_prompt,
max_new_tokens=300,
do_sample=True,
top_p=0.9,
temperature=0.7
)
elapsed_time = time.time() - start_time
output_text = generated[0]["generated_text"]
match = re.search(r"Answer:\s*(.*)", output_text, re.DOTALL)
model_answer = match.group(1).strip() if match else output_text.strip()
for kw in ["your students", "may also want", "in order to"]:
if kw in model_answer:
model_answer = model_answer.split(kw)[0].strip()
break
context_memory["last_question"] = user_input
context_memory["last_answer"] = model_answer
minutes = int(elapsed_time // 60)
seconds = round(elapsed_time % 60, 1)
time_display = f"{minutes}m {seconds}s" if minutes > 0 else f"{seconds}s"
return f"[Generated in {time_display}]\n\n{model_answer}"
except Exception as e:
return f"An error occurred:\n\n{e}"
# Gradio UI
interface = gr.Interface(
fn=generate_response,
inputs=gr.Textbox(lines=5, placeholder="Ask your question here...", label="Ask your question"),
outputs=gr.Textbox(lines=1, label="Response"),
title="Gemma Mentor Chatbot",
description="Ask questions and get warm, supportive, emotionally intelligent responses. Type 'exit' to reset.",
flagging_mode="never", # ✅ updated from deprecated allow_flagging
theme="default",
live=False,
api_name="/predict"
)
# Correct launch block for Gradio 4+ / 5+
if __name__ == "__main__":
interface.queue() # ✅ enable queuing system
interface.launch(share=False)
|