from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline app = FastAPI() # Load Small LLM pipe = pipeline( "text-generation", model="Qwen/Qwen2.5-1.5B-Instruct", device_map="auto" ) # Disclaimer DISCLAIMER = """
⚠️ AI Study Assistant Notice
This AI chatbot is here to help you study and learn! However, AI can sometimes make mistakes. Always double-check important facts and deadlines with your teacher or course materials.
For your privacy, please do not share personal information or student IDs in this chat.
--- ### 👨‍💻 Author **Md Saib Hossain** *AI Engineer • AI Safety Researcher • Agentic AI Architect* *Designing safe, scalable, and human-centered intelligent systems.* Looking to build custom chat interfaces, educational AI systems, or RAG-based chatbots? Let's connect:

Email LinkedIn GitHub Portfolio

""" # Request Model class ChatRequest(BaseModel): message: str mode: str | None = None # ----------------------------- # Helper Function # ----------------------------- def generate_response(system_prompt: str, user_message: str): prompt = f""" {system_prompt} User: {user_message} Assistant: """ result = pipe( prompt, max_new_tokens=256, temperature=0.7, do_sample=True ) response = result[0]["generated_text"] # Extract assistant response response = response.split("Assistant:")[-1].strip() # Add Disclaimer response += DISCLAIMER return response # ----------------------------- # Default Chat Endpoint # ----------------------------- @app.post("/api/chat") async def general_chat(req: ChatRequest): system_prompt = """ You are a helpful AI assistant. Answer all kinds of questions politely and clearly. """ response = generate_response(system_prompt, req.message) return { "mode": "general", "response": response } # ----------------------------- # English Endpoint # ----------------------------- @app.post("/api/english") async def english_chat(req: ChatRequest): system_prompt = """ You are an English learning assistant. You ONLY answer: - English grammar - vocabulary - sentence correction - translation - writing - speaking practice If the user asks anything outside English learning, reply: 'I only answer English-related questions.' """ response = generate_response(system_prompt, req.message) return { "mode": "english", "response": response } # ----------------------------- # Bangla Endpoint # ----------------------------- @app.post("/api/bangla") async def bangla_chat(req: ChatRequest): system_prompt = """ You are a Bangla language assistant. You ONLY answer: - Bangla grammar - Bangla translation - Bangla writing - Bangla literature - Bangla language learning If the user asks anything outside Bangla topics, reply: 'আমি শুধুমাত্র বাংলা বিষয়ক প্রশ্নের উত্তর দিই।' """ response = generate_response(system_prompt, req.message) return { "mode": "bangla", "response": response } # ----------------------------- # Math Endpoint # ----------------------------- @app.post("/api/math") async def math_chat(req: ChatRequest): system_prompt = """ You are a Mathematics assistant. You ONLY answer: - algebra - calculus - arithmetic - geometry - equations - statistics - mathematics problem solving Explain solutions step by step. If the user asks anything outside mathematics, reply: 'I only answer Mathematics-related questions.' """ response = generate_response(system_prompt, req.message) return { "mode": "math", "response": response } # ----------------------------- # Physics Endpoint # ----------------------------- @app.post("/api/physics") async def physics_chat(req: ChatRequest): system_prompt = """ You are a Physics assistant. You ONLY answer: - mechanics - electricity - magnetism - optics - thermodynamics - modern physics - numerical physics problems Explain concepts clearly and step by step. If the user asks anything outside physics, reply: 'I only answer Physics-related questions.' """ response = generate_response(system_prompt, req.message) return { "mode": "physics", "response": response }