saibhossain commited on
Commit
fc2ec8a
·
1 Parent(s): 25b98ba

Deploy FastAPI chatbot with multi-mode endpoints

Browse files
Files changed (3) hide show
  1. README.md +209 -9
  2. main.py +202 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,12 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- title: Chat API
3
- emoji: 📚
4
- colorFrom: green
5
- colorTo: green
6
- sdk: static
7
- pinned: false
8
- license: apache-2.0
9
- short_description: 'the api of chat '
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # 🤖 Multimodal AI Chatbot API (FastAPI + Hugging Face + Qwen2.5)
2
+
3
+ This project is a **FastAPI-based AI chatbot backend** designed for deployment on **Google Colab + Hugging Face Spaces**. It uses a lightweight LLM (`Qwen2.5-1.5B-Instruct`) and provides multiple domain-specific AI endpoints.
4
+
5
+ ---
6
+
7
+ ## 🚀 Features
8
+
9
+ * ⚡ FastAPI backend for high-performance inference
10
+ * 🧠 Powered by `Qwen2.5-1.5B-Instruct` (lightweight LLM)
11
+ * 🌐 Multiple specialized AI modes:
12
+
13
+ * English assistant
14
+ * Bangla assistant
15
+ * Math solver
16
+ * Physics tutor
17
+ * 🔗 REST API ready for frontend integration
18
+ * 📦 Hugging Face / Colab deployment friendly
19
+
20
+ ---
21
+
22
+ ## 📁 Project Structure
23
+
24
+ ```
25
+ main.py # FastAPI backend
26
+ requirements.txt
27
+ README.md
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 🧠 AI Modes / Endpoints
33
+
34
+ | Mode | Endpoint | Description |
35
+ | ------- | ---------- | --------------------------------- |
36
+ | General | `/chat` | General AI assistant |
37
+ | English | `/english` | Grammar, writing, correction |
38
+ | Bangla | `/bangla` | Bangla language assistant |
39
+ | Math | `/math` | Step-by-step problem solving |
40
+ | Physics | `/physics` | Physics explanations & numericals |
41
+
42
+ ---
43
+
44
+ ## ⚙️ Installation (Local / Colab)
45
+
46
+ ### 1️⃣ Install dependencies
47
+
48
+ ```bash
49
+ pip install fastapi uvicorn transformers torch accelerate sentencepiece
50
+ ```
51
+
52
+ ---
53
+
54
+ ### 2️⃣ Run server
55
+
56
+ ```bash
57
+ uvicorn main:app --host 0.0.0.0 --port 8000
58
+ ```
59
+
60
+ For Colab:
61
+
62
+ ```bash
63
+ nohup uvicorn main:app --host 0.0.0.0 --port 8000 &
64
+ ```
65
+
66
+ ---
67
+
68
+ ## 🌐 API Usage
69
+
70
+ ### 🔹 General Chat
71
+
72
+ ```bash
73
+ POST /chat
74
+ ```
75
+
76
+ ```json
77
+ {
78
+ "message": "Hello, what is AI?",
79
+ "mode": "none"
80
+ }
81
+ ```
82
+
83
+ ---
84
+
85
+ ### 🔹 English Mode
86
+
87
+ ```bash
88
+ POST /english
89
+ ```
90
+
91
+ ```json
92
+ {
93
+ "message": "Correct this sentence: I has a book"
94
+ }
95
+ ```
96
+
97
+ ---
98
+
99
+ ### 🔹 Bangla Mode
100
+
101
+ ```bash
102
+ POST /bangla
103
+ ```
104
+
105
+ ```json
106
+ {
107
+ "message": "বাংলা ব্যাকরণ কি?"
108
+ }
109
+ ```
110
+
111
+ ---
112
+
113
+ ### 🔹 Math Mode
114
+
115
+ ```bash
116
+ POST /math
117
+ ```
118
+
119
+ ```json
120
+ {
121
+ "message": "Solve: 2x + 5 = 15"
122
+ }
123
+ ```
124
+
125
+ ---
126
+
127
+ ### 🔹 Physics Mode
128
+
129
+ ```bash
130
+ POST /physics
131
+ ```
132
+
133
+ ```json
134
+ {
135
+ "message": "Explain Newton's second law"
136
+ }
137
+ ```
138
+
139
  ---
140
+
141
+ ## 🧾 Response Format
142
+
143
+ ```json
144
+ {
145
+ "mode": "math",
146
+ "response": "Step-by-step solution..."
147
+ }
148
+ ```
149
+
150
+ ---
151
+
152
+ ## 📌 Disclaimer (Included in every response)
153
+
154
+ Each response automatically includes:
155
+
156
+ > The chat interface is built by MD Saib Hossain.
157
+ > For AI systems, RAG pipelines, or Agentic AI development contact:
158
+ > [https://www.linkedin.com/in/saib-hossain-182834229/](https://www.linkedin.com/in/saib-hossain-182834229/)
159
+
160
+ ---
161
+
162
+ ## 🚀 Deployment (Hugging Face Spaces)
163
+
164
+ 1. Create a new Space
165
+ 2. Choose **Docker or Gradio SDK**
166
+ 3. Upload `main.py`
167
+ 4. Set runtime to Python 3.10+
168
+ 5. Add dependencies in `requirements.txt`
169
+
170
+ ---
171
+
172
+ ## 🔥 Tech Stack
173
+
174
+ * FastAPI
175
+ * Hugging Face Transformers
176
+ * PyTorch
177
+ * Qwen2.5 LLM
178
+ * Google Colab (for development)
179
+
180
+ ---
181
+
182
+ ## 📈 Future Improvements
183
+
184
+ * 🔥 Streaming responses (token-by-token)
185
+ * 🔥 LangGraph-based routing
186
+ * 🔥 RAG integration (PDF + web data)
187
+ * 🔥 Memory-based chat system
188
+ * 🔥 Frontend React UI integration
189
+
190
+ ---
191
+
192
+ ## 👨‍💻 Author
193
+
194
+ **MD Saib Hossain**
195
+ AI/ML Engineer | Agentic AI & RAG Systems Builder
196
+
197
+ 🔗 LinkedIn: [https://www.linkedin.com/in/saib-hossain-182834229/](https://www.linkedin.com/in/saib-hossain-182834229/)
198
+
199
+ ---
200
+
201
+ ## ⭐ If you like this project
202
+
203
+ You can extend it into:
204
+
205
+ * AI tutor system
206
+ * Multilingual chatbot
207
+ * Exam preparation assistant
208
+ * Medical/education RAG system
209
+
210
  ---
211
 
212
+ 🚀 Built for scalable AI experimentation on lightweight infrastructure.
main.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+ from transformers import pipeline
5
+
6
+ app = FastAPI()
7
+
8
+ # Load Small LLM
9
+ pipe = pipeline(
10
+ "text-generation",
11
+ model="Qwen/Qwen2.5-1.5B-Instruct",
12
+ device_map="auto"
13
+ )
14
+
15
+ # Disclaimer
16
+ DISCLAIMER = """
17
+
18
+ ---
19
+ The chat interface is built by MD Saib Hossain.
20
+
21
+ For building chat interfaces, Agentic AI systems, or RAG-based chatbots, please contact:
22
+ https://www.linkedin.com/in/saib-hossain-182834229/
23
+ """
24
+
25
+ # Request Model
26
+ class ChatRequest(BaseModel):
27
+ message: str
28
+ mode: str | None = None
29
+
30
+
31
+ # -----------------------------
32
+ # Helper Function
33
+ # -----------------------------
34
+ def generate_response(system_prompt: str, user_message: str):
35
+
36
+ prompt = f"""
37
+ {system_prompt}
38
+
39
+ User: {user_message}
40
+ Assistant:
41
+ """
42
+
43
+ result = pipe(
44
+ prompt,
45
+ max_new_tokens=256,
46
+ temperature=0.7,
47
+ do_sample=True
48
+ )
49
+
50
+ response = result[0]["generated_text"]
51
+
52
+ # Extract assistant response
53
+ response = response.split("Assistant:")[-1].strip()
54
+
55
+ # Add Disclaimer
56
+ response += DISCLAIMER
57
+
58
+ return response
59
+
60
+
61
+ # -----------------------------
62
+ # Default Chat Endpoint
63
+ # -----------------------------
64
+ @app.post("/chat")
65
+ async def general_chat(req: ChatRequest):
66
+
67
+ system_prompt = """
68
+ You are a helpful AI assistant.
69
+ Answer all kinds of questions politely and clearly.
70
+ """
71
+
72
+ response = generate_response(system_prompt, req.message)
73
+
74
+ return {
75
+ "mode": "general",
76
+ "response": response
77
+ }
78
+
79
+
80
+ # -----------------------------
81
+ # English Endpoint
82
+ # -----------------------------
83
+ @app.post("/english")
84
+ async def english_chat(req: ChatRequest):
85
+
86
+ system_prompt = """
87
+ You are an English learning assistant.
88
+
89
+ You ONLY answer:
90
+ - English grammar
91
+ - vocabulary
92
+ - sentence correction
93
+ - translation
94
+ - writing
95
+ - speaking practice
96
+
97
+ If the user asks anything outside English learning,
98
+ reply:
99
+ 'I only answer English-related questions.'
100
+ """
101
+
102
+ response = generate_response(system_prompt, req.message)
103
+
104
+ return {
105
+ "mode": "english",
106
+ "response": response
107
+ }
108
+
109
+
110
+ # -----------------------------
111
+ # Bangla Endpoint
112
+ # -----------------------------
113
+ @app.post("/bangla")
114
+ async def bangla_chat(req: ChatRequest):
115
+
116
+ system_prompt = """
117
+ You are a Bangla language assistant.
118
+
119
+ You ONLY answer:
120
+ - Bangla grammar
121
+ - Bangla translation
122
+ - Bangla writing
123
+ - Bangla literature
124
+ - Bangla language learning
125
+
126
+ If the user asks anything outside Bangla topics,
127
+ reply:
128
+ 'আমি শুধুমাত্র বাংলা বিষয়ক প্রশ্নের উত্তর দিই।'
129
+ """
130
+
131
+ response = generate_response(system_prompt, req.message)
132
+
133
+ return {
134
+ "mode": "bangla",
135
+ "response": response
136
+ }
137
+
138
+
139
+ # -----------------------------
140
+ # Math Endpoint
141
+ # -----------------------------
142
+ @app.post("/math")
143
+ async def math_chat(req: ChatRequest):
144
+
145
+ system_prompt = """
146
+ You are a Mathematics assistant.
147
+
148
+ You ONLY answer:
149
+ - algebra
150
+ - calculus
151
+ - arithmetic
152
+ - geometry
153
+ - equations
154
+ - statistics
155
+ - mathematics problem solving
156
+
157
+ Explain solutions step by step.
158
+
159
+ If the user asks anything outside mathematics,
160
+ reply:
161
+ 'I only answer Mathematics-related questions.'
162
+ """
163
+
164
+ response = generate_response(system_prompt, req.message)
165
+
166
+ return {
167
+ "mode": "math",
168
+ "response": response
169
+ }
170
+
171
+
172
+ # -----------------------------
173
+ # Physics Endpoint
174
+ # -----------------------------
175
+ @app.post("/physics")
176
+ async def physics_chat(req: ChatRequest):
177
+
178
+ system_prompt = """
179
+ You are a Physics assistant.
180
+
181
+ You ONLY answer:
182
+ - mechanics
183
+ - electricity
184
+ - magnetism
185
+ - optics
186
+ - thermodynamics
187
+ - modern physics
188
+ - numerical physics problems
189
+
190
+ Explain concepts clearly and step by step.
191
+
192
+ If the user asks anything outside physics,
193
+ reply:
194
+ 'I only answer Physics-related questions.'
195
+ """
196
+
197
+ response = generate_response(system_prompt, req.message)
198
+
199
+ return {
200
+ "mode": "physics",
201
+ "response": response
202
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch
5
+ accelerate
6
+ sentencepiece