File size: 5,155 Bytes
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbe673
 
 
 
fc2ec8a
 
 
adbe673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbe673
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbe673
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbe673
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbe673
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbe673
fc2ec8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

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 = """
<div style="text-align: center; font-size: 0.9em; color: #666; margin-bottom: 20px;">
  <strong>⚠️ AI Study Assistant Notice</strong><br>
  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. <br><em>For your privacy, please do not share personal information or student IDs in this chat.</em>
</div>

---

### 👨‍💻 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:

<p align="left">
  <a href="mailto:saibhossain5@gmail.com">
    <img src="https://img.shields.io/badge/Email-saibhossain5%40gmail.com-red?style=flat&logo=gmail" alt="Email">
  </a>
  <a href="https://linkedin.com/in/saib-hossain-182834229">
    <img src="https://img.shields.io/badge/LinkedIn-Connect-0A66C2?style=flat&logo=linkedin" alt="LinkedIn">
  </a>
  <a href="https://github.com/Saibhossain">
    <img src="https://img.shields.io/badge/GitHub-Profile-black?style=flat&logo=github" alt="GitHub">
  </a>
  <a href="https://saibhossain.github.io/">
    <img src="https://img.shields.io/badge/Portfolio-Visit-blue?style=flat&logo=google-chrome" alt="Portfolio">
  </a>
</p>
"""

# 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
    }