Spaces:
Runtime error
Runtime error
Soutrik
commited on
Commit
·
b29d073
1
Parent(s):
6a81b1b
removed stiffs related to fast api app as this repo will only have lightning and pytorch
Browse files- app/api/__init__.py +0 -0
- app/api/chat.py +0 -45
- app/core/__init__.py +0 -0
- app/core/__pycache__/__init__.cpython-310.pyc +0 -0
- app/core/__pycache__/config.cpython-310.pyc +0 -0
- app/core/celery_app.py +0 -8
- app/core/config.py +0 -42
- app/crud/__init__.py +0 -0
- app/crud/chat_crud.py +0 -14
- app/db/__init__.py +0 -0
- app/db/database.py +0 -17
- app/db/models.py +0 -12
- app/schemas/__init__.py +0 -0
- app/schemas/chat.py +0 -12
- app/tasks/__init__.py +0 -0
- app/tasks/chat_task.py +0 -6
- src/__pycache__/__init__.cpython-310.pyc +0 -0
- src/__pycache__/test_infra.cpython-310.pyc +0 -0
- src/number_manipulation.py +0 -7
- src/test_infra.py +0 -36
- tests/test_db_connection.py +0 -17
app/api/__init__.py
DELETED
File without changes
|
app/api/chat.py
DELETED
@@ -1,45 +0,0 @@
|
|
1 |
-
from fastapi import APIRouter, Depends
|
2 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
3 |
-
from app.schemas.chat import ChatMessage, ChatResponse
|
4 |
-
from app.crud.chat_crud import create_chat_message
|
5 |
-
from app.tasks.chat_task import process_chat_message
|
6 |
-
from src.number_manipulation import add_random_number
|
7 |
-
from app.db.database import get_db
|
8 |
-
from aiocache import caches
|
9 |
-
|
10 |
-
router = APIRouter()
|
11 |
-
|
12 |
-
|
13 |
-
@router.post("/chat", response_model=ChatResponse)
|
14 |
-
async def chat(message: ChatMessage, db: AsyncSession = Depends(get_db)):
|
15 |
-
cache = caches.get("default")
|
16 |
-
cache_key = f"chat:{message.user_input}:{message.content}"
|
17 |
-
|
18 |
-
# Attempt to retrieve the cached response
|
19 |
-
cached_response = await cache.get(cache_key)
|
20 |
-
if cached_response:
|
21 |
-
return ChatResponse(**cached_response)
|
22 |
-
|
23 |
-
# Process user input if not cached
|
24 |
-
processed_value = add_random_number(message.user_input)
|
25 |
-
message_id = await create_chat_message(
|
26 |
-
db=db,
|
27 |
-
content=message.content,
|
28 |
-
user_input=message.user_input,
|
29 |
-
processed_value=processed_value,
|
30 |
-
)
|
31 |
-
|
32 |
-
# Trigger background task
|
33 |
-
process_chat_message.delay(message.content)
|
34 |
-
|
35 |
-
# Prepare the response data
|
36 |
-
response_data = {
|
37 |
-
"message_id": message_id,
|
38 |
-
"status": "Message received",
|
39 |
-
"processed_value": processed_value,
|
40 |
-
}
|
41 |
-
|
42 |
-
# Cache the response data
|
43 |
-
await cache.set(cache_key, response_data, ttl=300) # Cache for 5 minutes
|
44 |
-
|
45 |
-
return ChatResponse(**response_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/__init__.py
DELETED
File without changes
|
app/core/__pycache__/__init__.cpython-310.pyc
DELETED
Binary file (226 Bytes)
|
|
app/core/__pycache__/config.cpython-310.pyc
DELETED
Binary file (1.82 kB)
|
|
app/core/celery_app.py
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
from celery import Celery
|
2 |
-
from app.core.config import settings
|
3 |
-
|
4 |
-
celery_app = Celery("chat_tasks", broker=settings.redis_url, backend=settings.redis_url)
|
5 |
-
|
6 |
-
celery_app.conf.update(
|
7 |
-
task_serializer="json", result_serializer="json", accept_content=["json"]
|
8 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/config.py
DELETED
@@ -1,42 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from pydantic.v1 import BaseSettings, Field
|
3 |
-
from loguru import logger
|
4 |
-
|
5 |
-
|
6 |
-
class Settings(BaseSettings):
|
7 |
-
POSTGRES_DB: str = Field("test_db", env="POSTGRES_DB")
|
8 |
-
POSTGRES_USER: str = Field("test_user", env="POSTGRES_USER")
|
9 |
-
POSTGRES_PASSWORD: str = Field("test_pass", env="POSTGRES_PASSWORD")
|
10 |
-
|
11 |
-
is_docker: bool = Field(default_factory=lambda: os.environ.get("DOCKER_ENV") == "1")
|
12 |
-
|
13 |
-
database_url: str = Field(..., env="DATABASE_URL")
|
14 |
-
redis_url: str = Field(..., env="REDIS_URL")
|
15 |
-
flower_basic_auth: str = Field(..., env="FLOWER_BASIC_AUTH")
|
16 |
-
broker_url: str = Field(..., env="BROKER_URL")
|
17 |
-
cache_backend: str = "aiocache.SimpleMemoryCache"
|
18 |
-
|
19 |
-
class Config:
|
20 |
-
env_file = ".env"
|
21 |
-
|
22 |
-
@classmethod
|
23 |
-
def create(cls):
|
24 |
-
"""Create instance and dynamically set database and redis URLs."""
|
25 |
-
instance = cls()
|
26 |
-
# Set the correct database URL without the +asyncpg
|
27 |
-
instance.database_url = (
|
28 |
-
f"postgresql+asyncpg://{instance.POSTGRES_USER}:{instance.POSTGRES_PASSWORD}@"
|
29 |
-
f"{'localhost' if not instance.is_docker else 'postgres'}:5432/{instance.POSTGRES_DB}"
|
30 |
-
)
|
31 |
-
instance.redis_url = (
|
32 |
-
f"redis://{'localhost' if not instance.is_docker else 'redis'}:6379/0"
|
33 |
-
)
|
34 |
-
instance.broker_url = instance.redis_url
|
35 |
-
return instance
|
36 |
-
|
37 |
-
|
38 |
-
# Instantiate Settings
|
39 |
-
settings = Settings.create()
|
40 |
-
|
41 |
-
if __name__ == "__main__":
|
42 |
-
logger.info(f"Settings: {settings.dict()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/crud/__init__.py
DELETED
File without changes
|
app/crud/chat_crud.py
DELETED
@@ -1,14 +0,0 @@
|
|
1 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
2 |
-
from app.db.models import ChatMessageModel
|
3 |
-
|
4 |
-
|
5 |
-
async def create_chat_message(
|
6 |
-
db: AsyncSession, content: str, user_input: int, processed_value: int
|
7 |
-
):
|
8 |
-
new_message = ChatMessageModel(
|
9 |
-
content=content, user_input=user_input, processed_value=processed_value
|
10 |
-
)
|
11 |
-
db.add(new_message)
|
12 |
-
await db.commit()
|
13 |
-
await db.refresh(new_message) # Fetches the latest state after commit
|
14 |
-
return new_message.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/db/__init__.py
DELETED
File without changes
|
app/db/database.py
DELETED
@@ -1,17 +0,0 @@
|
|
1 |
-
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
2 |
-
from sqlalchemy.orm import sessionmaker, declarative_base
|
3 |
-
from app.core.config import settings
|
4 |
-
|
5 |
-
# Create an async engine
|
6 |
-
engine = create_async_engine(settings.database_url, echo=True)
|
7 |
-
|
8 |
-
# Async session factory
|
9 |
-
SessionLocal = sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
|
10 |
-
|
11 |
-
Base = declarative_base()
|
12 |
-
|
13 |
-
|
14 |
-
# Dependency for asynchronous database session
|
15 |
-
async def get_db():
|
16 |
-
async with SessionLocal() as session:
|
17 |
-
yield session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/db/models.py
DELETED
@@ -1,12 +0,0 @@
|
|
1 |
-
from sqlalchemy import Column, Integer, String, DateTime, func
|
2 |
-
from app.db.database import Base
|
3 |
-
|
4 |
-
|
5 |
-
class ChatMessageModel(Base):
|
6 |
-
__tablename__ = "chat_messages"
|
7 |
-
|
8 |
-
id = Column(Integer, primary_key=True, index=True)
|
9 |
-
content = Column(String, nullable=False)
|
10 |
-
user_input = Column(Integer, nullable=False)
|
11 |
-
processed_value = Column(Integer, nullable=False)
|
12 |
-
timestamp = Column(DateTime, server_default=func.now())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/schemas/__init__.py
DELETED
File without changes
|
app/schemas/chat.py
DELETED
@@ -1,12 +0,0 @@
|
|
1 |
-
from pydantic import BaseModel
|
2 |
-
|
3 |
-
|
4 |
-
class ChatMessage(BaseModel):
|
5 |
-
content: str
|
6 |
-
user_input: int
|
7 |
-
|
8 |
-
|
9 |
-
class ChatResponse(BaseModel):
|
10 |
-
message_id: int
|
11 |
-
status: str
|
12 |
-
processed_value: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/tasks/__init__.py
DELETED
File without changes
|
app/tasks/chat_task.py
DELETED
@@ -1,6 +0,0 @@
|
|
1 |
-
from app.core.celery_app import celery_app
|
2 |
-
|
3 |
-
|
4 |
-
@celery_app.task
|
5 |
-
def process_chat_message(content: str):
|
6 |
-
print(f"Processing message: {content}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/__pycache__/__init__.cpython-310.pyc
DELETED
Binary file (221 Bytes)
|
|
src/__pycache__/test_infra.cpython-310.pyc
DELETED
Binary file (1.44 kB)
|
|
src/number_manipulation.py
DELETED
@@ -1,7 +0,0 @@
|
|
1 |
-
import random
|
2 |
-
|
3 |
-
|
4 |
-
def add_random_number(user_input: int) -> int:
|
5 |
-
random_addition = random.randint(1, 100) # Add a random number between 1 and 100
|
6 |
-
result = user_input + random_addition
|
7 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/test_infra.py
DELETED
@@ -1,36 +0,0 @@
|
|
1 |
-
import asyncpg
|
2 |
-
import aioredis
|
3 |
-
from loguru import logger
|
4 |
-
from app.core.config import settings
|
5 |
-
|
6 |
-
|
7 |
-
async def test_postgres_connection(database_url: str):
|
8 |
-
try:
|
9 |
-
conn = await asyncpg.connect(database_url)
|
10 |
-
logger.info("Successfully connected to PostgreSQL!")
|
11 |
-
await conn.close()
|
12 |
-
except Exception as e:
|
13 |
-
logger.error(f"Failed to connect to PostgreSQL: {e}")
|
14 |
-
|
15 |
-
|
16 |
-
async def test_redis_connection(redis_url: str):
|
17 |
-
try:
|
18 |
-
redis = await aioredis.from_url(redis_url)
|
19 |
-
await redis.ping() # Send a ping to check connection
|
20 |
-
logger.info("Successfully connected to Redis!")
|
21 |
-
await redis.close()
|
22 |
-
except Exception as e:
|
23 |
-
logger.error(f"Failed to connect to Redis: {e}")
|
24 |
-
|
25 |
-
|
26 |
-
async def main():
|
27 |
-
logger.info(f"Settings: {settings.dict()}")
|
28 |
-
|
29 |
-
await test_postgres_connection(settings.database_url.replace("+asyncpg", ""))
|
30 |
-
await test_redis_connection(settings.redis_url)
|
31 |
-
|
32 |
-
|
33 |
-
if __name__ == "__main__":
|
34 |
-
import asyncio
|
35 |
-
|
36 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_db_connection.py
DELETED
@@ -1,17 +0,0 @@
|
|
1 |
-
import pytest
|
2 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
3 |
-
from sqlalchemy import text
|
4 |
-
from app.db.database import get_db
|
5 |
-
|
6 |
-
|
7 |
-
@pytest.mark.asyncio
|
8 |
-
async def test_database_connection():
|
9 |
-
# Use the get_db dependency directly for testing
|
10 |
-
async for session in get_db():
|
11 |
-
assert isinstance(
|
12 |
-
session, AsyncSession
|
13 |
-
), "Session is not an instance of AsyncSession"
|
14 |
-
|
15 |
-
# Check if the session can execute a simple query
|
16 |
-
result = await session.execute(text("SELECT 1"))
|
17 |
-
assert result.scalar() == 1, "Database did not return expected result"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|