Chat_API / app.py
saibhossain's picture
Deploy FastAPI chatbot with multi-mode endpoints
adbe673
Raw History Blame Contribute Delete
5.16 kB
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
}