Spaces:
Sleeping
Sleeping
Update app.py
Browse files1231 unmodified line5678910111213141 unmodified line1617181920212223242526272829301 unmodified line32333435363738394041424344454647484950515253545556575859606162633 unmodified lines6768697071727374754 unmodified lines8081828384858687888990919293949596979899100101102import spacesimport torchimport gradio as gr1 unmodified lineMODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"# Load tokenizertokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)# Load modelmodel = AutoModelForCausalLM.from_pretrained(MODEL_NAME,torch_dtype="auto",1 unmodified line)SYSTEM_PROMPT = """You are futurevidyabox AI assiant , the official AI assistant of FutureVidyabox.Your role:- Help FutureVidyabox students, staff, and customers.- Explain educational topics clearly.- Help with mathematics, science, computers, programming, and general education.- Help with programming and code generation.- Help debug code and explain programming concepts.- Help answer FutureVidyabox customer-support questions when the required information is provided.Personality:- Friendly1 unmodified line- Clear- Patient- Professional- Concise when a short answer is enough- Give step-by-step explanations when usefulImportant rules:- Do not pretend to know private FutureVidyabox information that has not been provided.- Do not invent company policies, prices, contact information, or services.- If you do not know something, clearly say that you do not know.- For programming questions, provide useful and properly formatted code when appropriate.- For educational questions, explain concepts rather than only giving an answer.You are FVB AI, not ChatGPT."""@spaces.GPU(duration=45)def fvb_ai(message):messages = [{"role": "system","content": SYSTEM_PROMPT,},{"role": "user","content": message,},]inputs = tokenizer.apply_chat_template(messages,add_generation_prompt=True,3 unmodified lines).to(model.device)with torch.inference_mode():outputs = model.generate(**inputs,max_new_tokens=256,do_sample=True,temperature=0.7,top_p=0.8,4 unmodified linesresponse = tokenizer.decode(new_tokens,skip_special_tokens=True,)return response.strip()demo = gr.Interface(fn=fvb_ai,inputs=gr.Textbox(label="Message",placeholder="Ask FVB AI anything..."),outputs=gr.Textbox(label="FVB AI"),title="FVB AI",description="FutureVidyabox AI",)demo.launch()12341 unmodified line678910111213141516171 unmodified line1920212223242526272829303132331 unmodified line353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485863 unmodified lines909192939495969798994 unmodified lines104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296```pythonimport spacesimport torchimport gradio as gr1 unmodified lineMODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"# -----------------------------# MODEL# -----------------------------tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,torch_dtype="auto",1 unmodified line)SYSTEM_PROMPT = """You are FVB AI, the official AI assistant of FutureVidyabox.Your role:- Help FutureVidyabox students, staff, and customers.- Explain educational topics clearly.- Help with mathematics, science, computers, programming, and general education.- Generate and explain programming code.- Help debug programming problems.- Help with FutureVidyabox customer-support questions when the required information is available.Personality:- Friendly1 unmodified line- Clear- Patient- Professional- Concise when appropriate- Give step-by-step explanations when usefulImportant:- Do not invent FutureVidyabox information.- Do not invent prices, policies, services, contact information, or company facts.- If you do not know something, say that you do not have enough information.- For programming questions, provide useful and properly formatted code.- For educational questions, explain concepts clearly.You are FVB AI, not ChatGPT."""# -----------------------------# AI FUNCTION# -----------------------------@spaces.GPU(duration=45)def fvb_ai(message, history):messages = [{"role": "system","content": SYSTEM_PROMPT}]# Add previous conversationfor item in history:if isinstance(item, dict):role = item.get("role")content = item.get("content")if role in ["user", "assistant"] and isinstance(content, str):messages.append({"role": role,"content": content})# Add current messagemessages.append({"role": "user","content": message})inputs = tokenizer.apply_chat_template(messages,add_generation_prompt=True,3 unmodified lines).to(model.device)with torch.inference_mode():outputs = model.generate(**inputs,max_new_tokens=512,do_sample=True,temperature=0.7,top_p=0.8,4 unmodified linesresponse = tokenizer.decode(new_tokens,skip_special_tokens=True)return response.strip()# -----------------------------# CUSTOM CSS# -----------------------------css = """/* Main page */body {background: #f7f9fc;}.gradio-container {max-width: 1200px !important;margin: auto !important;}/* Header */.fvb-header {text-align: center;padding: 25px 10px 10px 10px;}.fvb-logo {font-size: 34px;font-weight: 800;letter-spacing: -1px;}.fvb-subtitle {color: #6b7280;font-size: 15px;margin-top: 5px;}/* Chat */.chatbox {border-radius: 18px !important;border: 1px solid #e5e7eb !important;}/* Input */textarea {border-radius: 14px !important;}/* Buttons */button {border-radius: 12px !important;}/* Footer */.fvb-footer {text-align: center;color: #8a8f98;font-size: 12px;padding: 15px;}"""# -----------------------------# UI# -----------------------------with gr.Blocks(title="FVB AI",css=css,theme=gr.themes.Soft(primary_hue="blue",secondary_hue="blue",neutral_hue="slate")) as demo:gr.HTML("""<div class="fvb-header"><div class="fvb-logo">FutureVidyabox AI</div><div class="fvb-subtitle">Your educational and customer-support AI assistant</div></div>""")chatbot = gr.Chatbot(label="FVB AI",height=520,show_label=True,elem_classes="chatbox")with gr.Row():message = gr.Textbox(placeholder="Ask FVB AI anything...",label="",scale=8,lines=2)send = gr.Button("Send",variant="primary",scale=1)with gr.Row():clear = gr.Button("Clear Chat")gr.HTML("""<div class="fvb-footer">FVB AI • FutureVidyabox</div>""")# -------------------------# SEND# -------------------------def respond(message_text, chat_history):if not message_text.strip():return "", chat_historyanswer = fvb_ai(message_text,chat_history)chat_history = chat_history + [{"role": "user","content": message_text},{"role": "assistant","content": answer}]return "", chat_historysend.click(respond,inputs=[message, chatbot],outputs=[message, chatbot])message.submit(respond,inputs=[message, chatbot],outputs=[message, chatbot])clear.click(lambda: [],outputs=[chatbot])# -----------------------------# START# -----------------------------demo.launch()```
app.py+228-34