├── .env
├── .gitignore
├── .idea
├── .gitignore
├── customuserprompt.iml
├── dbnavigator.xml
├── inspectionProfiles
│ ├── Project_Default.xml
│ └── profiles_settings.xml
├── misc.xml
├── modules.xml
└── vcs.xml
├── README.md
├── app
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-311.pyc
│ ├── auth.cpython-311.pyc
│ ├── db.cpython-311.pyc
│ ├── models.cpython-311.pyc
│ ├── routes.cpython-311.pyc
│ ├── schemas.cpython-311.pyc
│ └── security.cpython-311.pyc
├── auth.py
├── db.py
├── models.py
├── routes.py
├── schemas.py
└── security.py
└── main.py
/.env:
--------------------------------------------------------------------------------
1 | OPENAI_API_KEY=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
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .env
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/customuserprompt.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/dbnavigator.xml:
--------------------------------------------------------------------------------
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 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
47 |
48 |
49 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Personal Fitness ChatBot (Version 1)
2 |
3 | ## Description
4 |
5 | This project is a FastAPI-based web application. It is designed to provide a secure conversation platform for registered users. The main functionality of this version is to authenticate the user and return a static conversation message.
6 |
7 | ## Setup
8 |
9 | This project uses FastAPI and SQLAlchemy, and it requires Python 3.7+.
10 |
11 | To start the application, navigate to the project folder and execute the command:
12 |
13 | python main.py
14 |
15 | The application will be available at `http://localhost:5555`.
16 |
17 | ## Key Features
18 |
19 | - User registration and authentication system with OAuth2.
20 | - Secure conversation endpoint that returns a static message and the authenticated user's username.
21 | - SQLite database for user data persistence.
22 |
23 | ## Environment Variables
24 |
25 | To run this project, you will need to add the following environment variable:
26 |
27 | - `SECRET_KEY`: Your secret key for encoding and decoding the JWT tokens.
28 |
29 | Please use a `.env` file for setting this environment variable. The `.env` file is ignored by git to ensure sensitive items are not accidentally published.
30 |
--------------------------------------------------------------------------------
/app/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__init__.py
--------------------------------------------------------------------------------
/app/__pycache__/__init__.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/__init__.cpython-311.pyc
--------------------------------------------------------------------------------
/app/__pycache__/auth.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/auth.cpython-311.pyc
--------------------------------------------------------------------------------
/app/__pycache__/db.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/db.cpython-311.pyc
--------------------------------------------------------------------------------
/app/__pycache__/models.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/models.cpython-311.pyc
--------------------------------------------------------------------------------
/app/__pycache__/routes.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/routes.cpython-311.pyc
--------------------------------------------------------------------------------
/app/__pycache__/schemas.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/schemas.cpython-311.pyc
--------------------------------------------------------------------------------
/app/__pycache__/security.cpython-311.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coding-Crashkurse/Private-ChatBot/7df588d1dc8d536f7000c3efce1f0d2c3dd03e16/app/__pycache__/security.cpython-311.pyc
--------------------------------------------------------------------------------
/app/auth.py:
--------------------------------------------------------------------------------
1 | from fastapi import Depends, HTTPException, status
2 | from fastapi.security import OAuth2PasswordBearer
3 | from jose import JWTError, jwt
4 | from sqlalchemy.orm import Session
5 |
6 | from app import models, schemas, security
7 | from app.db import get_db
8 |
9 | oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
10 |
11 |
12 | def get_user(db: Session, username: str):
13 | return db.query(models.User).filter(models.User.username == username).first()
14 |
15 |
16 | async def get_current_user(
17 | token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
18 | ):
19 | credentials_exception = HTTPException(
20 | status_code=status.HTTP_401_UNAUTHORIZED,
21 | detail="Could not validate credentials",
22 | headers={"WWW-Authenticate": "Bearer"},
23 | )
24 | try:
25 | payload = jwt.decode(
26 | token, security.SECRET_KEY, algorithms=[security.ALGORITHM]
27 | )
28 | username: str = payload.get("sub")
29 | if username is None:
30 | raise credentials_exception
31 | token_data = schemas.TokenData(username=username)
32 | except JWTError:
33 | raise credentials_exception
34 | user = get_user(db, username=token_data.username)
35 | if user is None:
36 | raise credentials_exception
37 | return user
38 |
--------------------------------------------------------------------------------
/app/db.py:
--------------------------------------------------------------------------------
1 | from contextlib import contextmanager
2 |
3 | from sqlalchemy import create_engine
4 | from sqlalchemy.ext.declarative import declarative_base
5 | from sqlalchemy.orm import sessionmaker
6 |
7 | SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
8 |
9 | engine = create_engine(
10 | SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
11 | )
12 | SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
13 |
14 | Base = declarative_base()
15 |
16 | def get_db():
17 | db = SessionLocal()
18 | try:
19 | yield db
20 | finally:
21 | db.close()
22 |
--------------------------------------------------------------------------------
/app/models.py:
--------------------------------------------------------------------------------
1 | from sqlalchemy import Column
2 | from sqlalchemy import Enum as SQLEnum
3 | from sqlalchemy import Integer, String
4 |
5 | from app.db import Base
6 | from app.schemas import UserLevel # corrected import statement
7 |
8 |
9 | class User(Base):
10 | __tablename__ = "users"
11 |
12 | id = Column(Integer, primary_key=True, index=True)
13 | username = Column(String, unique=True, index=True)
14 | email = Column(String, unique=True, index=True)
15 | hashed_password = Column(String)
16 | age = Column(Integer)
17 | level = Column(SQLEnum(UserLevel))
18 |
--------------------------------------------------------------------------------
/app/routes.py:
--------------------------------------------------------------------------------
1 | from datetime import timedelta
2 |
3 | from fastapi import APIRouter, Depends, HTTPException, status
4 | from fastapi.security import OAuth2PasswordRequestForm
5 | from sqlalchemy.orm import Session
6 |
7 | from app import auth, models, schemas, security
8 | from app.db import get_db
9 |
10 | router = APIRouter()
11 |
12 |
13 | @router.post("/register/", response_model=schemas.UserInDBBase)
14 | async def register(user_in: schemas.UserIn, db: Session = Depends(get_db)):
15 | db_user = auth.get_user(db, username=user_in.username)
16 | if db_user:
17 | raise HTTPException(status_code=400, detail="Username already registered")
18 | db_user = db.query(models.User).filter(models.User.email == user_in.email).first()
19 | if db_user:
20 | raise HTTPException(status_code=400, detail="Email already registered")
21 |
22 | hashed_password = security.get_password_hash(user_in.password)
23 | db_user = models.User(**user_in.dict(exclude={"password"}), hashed_password=hashed_password)
24 | db.add(db_user)
25 | db.commit()
26 | db.refresh(db_user)
27 | return db_user
28 |
29 |
30 | @router.post("/token", response_model=schemas.Token)
31 | async def login_for_access_token(
32 | form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)
33 | ):
34 | user = auth.get_user(db, username=form_data.username)
35 | if not user or not security.pwd_context.verify(
36 | form_data.password, user.hashed_password
37 | ):
38 | raise HTTPException(
39 | status_code=status.HTTP_401_UNAUTHORIZED,
40 | detail="Incorrect username or password",
41 | headers={"WWW-Authenticate": "Bearer"},
42 | )
43 | access_token_expires = timedelta(minutes=security.ACCESS_TOKEN_EXPIRE_MINUTES)
44 | access_token = security.create_access_token(
45 | data={"sub": user.username}, expires_delta=access_token_expires
46 | )
47 | return {"access_token": access_token, "token_type": "bearer"}
48 |
49 |
50 | @router.get("/conversation/")
51 | async def read_conversation(
52 | current_user: schemas.UserInDB = Depends(auth.get_current_user),
53 | ):
54 | return {
55 | "conversation": "This is a secure conversation!",
56 | "current_user": current_user.username,
57 | }
58 |
--------------------------------------------------------------------------------
/app/schemas.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 | from typing import Optional
3 | from enum import Enum
4 |
5 |
6 | class UserLevel(str, Enum):
7 | beginner = "beginner"
8 | intermediate = "intermediate"
9 | expert = "expert"
10 |
11 |
12 | class UserBase(BaseModel):
13 | email: str
14 | username: str
15 | age: Optional[int] = None
16 | level: UserLevel = UserLevel.beginner
17 |
18 |
19 | class UserIn(UserBase):
20 | password: str
21 |
22 |
23 | class UserInDBBase(UserBase):
24 | id: int
25 |
26 | class Config:
27 | orm_mode = True
28 |
29 |
30 | class UserInDB(UserInDBBase):
31 | hashed_password: str
32 |
33 |
34 | class TokenData(BaseModel):
35 | username: Optional[str] = None
36 |
37 |
38 | class Token(BaseModel):
39 | access_token: str
40 | token_type: str
41 |
--------------------------------------------------------------------------------
/app/security.py:
--------------------------------------------------------------------------------
1 | from datetime import datetime, timedelta
2 | from typing import Optional
3 |
4 | from jose import JWTError, jwt
5 | from passlib.context import CryptContext
6 |
7 | SECRET_KEY = "YOUR_SECRET_KEY"
8 | ALGORITHM = "HS256"
9 | ACCESS_TOKEN_EXPIRE_MINUTES = 30
10 |
11 | pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
12 |
13 |
14 | def get_password_hash(password: str):
15 | return pwd_context.hash(password)
16 |
17 |
18 | def create_access_token(*, data: dict, expires_delta: Optional[timedelta] = None):
19 | to_encode = data.copy()
20 | if expires_delta:
21 | expire = datetime.utcnow() + expires_delta
22 | else:
23 | expire = datetime.utcnow() + timedelta(minutes=15)
24 | to_encode.update({"exp": expire})
25 | encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
26 | return encoded_jwt
27 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from fastapi import FastAPI
2 | import uvicorn
3 | from app import models
4 | from app.db import engine
5 | from app.routes import router
6 |
7 | app = FastAPI()
8 |
9 | models.Base.metadata.create_all(bind=engine)
10 |
11 | app.include_router(router)
12 |
13 | if __name__ == "__main__":
14 | uvicorn.run(app=app, host="127.0.0.1", port=5555)
15 |
--------------------------------------------------------------------------------