Compare commits

..

1 Commits

Author SHA1 Message Date
owusu bf2379e39d Merge pull request 'prod deploy version' (#1) from dev into main
Reviewed-on: #1
2025-06-11 16:58:47 +00:00
7 changed files with 19 additions and 128 deletions
+3 -52
View File
@@ -1,6 +1,6 @@
import os import os
from typing import Optional from typing import Optional
from fastapi import FastAPI, HTTPException, Security, Depends, Request from fastapi import FastAPI, HTTPException, Security, Depends
from fastapi.security import APIKeyHeader from fastapi.security import APIKeyHeader
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
@@ -27,23 +27,9 @@ import os
from PyPDF2 import PdfReader from PyPDF2 import PdfReader
from config import QUIZ_TYPES from config import QUIZ_TYPES
from config import Config from config import Config
import logging
import time
# Load environment variables # Load environment variables
load_dotenv() load_dotenv()
from config import Config API_KEY = os.getenv("API_KEY_ACCESS")
API_KEY = Config.API_KEY_ACCESS
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Also configure uvicorn logger
uvicorn_logger = logging.getLogger("uvicorn.access")
uvicorn_logger.setLevel(logging.INFO)
base_path = os.path.join("data", "config_files") base_path = os.path.join("data", "config_files")
QUESTIONS_PATH = os.path.join(base_path, "questions.json") QUESTIONS_PATH = os.path.join(base_path, "questions.json")
@@ -60,40 +46,6 @@ app = FastAPI(
version="1.0.0" version="1.0.0"
) )
# Add request logging middleware
@app.middleware("http")
async def log_requests(request: Request, call_next):
start_time = time.time()
# Log incoming request (using both logger and print for visibility)
log_msg = f"🔥 INCOMING REQUEST: {request.method} {request.url}"
logger.info(log_msg)
print(log_msg)
# Get request body for POST requests
if request.method == "POST":
body = await request.body()
body_msg = f"🔥 Request Body: {body.decode('utf-8') if body else 'Empty'}"
logger.info(body_msg)
print(body_msg)
# Re-create request with body for downstream processing
async def receive():
return {"type": "http.request", "body": body}
request._receive = receive
response = await call_next(request)
# Log response
process_time = time.time() - start_time
response_msg = f"🔥 RESPONSE: {response.status_code} - Time: {process_time:.4f}s"
logger.info(response_msg)
print(response_msg)
return response
# Add CORS middleware # Add CORS middleware
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
@@ -116,7 +68,6 @@ async def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
) )
token = api_key_header.split(' ')[1] token = api_key_header.split(' ')[1]
print(f"Token : {token}")
if token != API_KEY: if token != API_KEY:
raise HTTPException( raise HTTPException(
status_code=401, status_code=401,
@@ -337,7 +288,7 @@ async def generate_pdf_endpoint(
if request.form_id: if request.form_id:
print(f"Fetching form response for form_id: {request.form_id}") # Debugging print print(f"Fetching form response for form_id: {request.form_id}") # Debugging print
try: try:
x_api_key = Config.BACKEND_XAPI_KEY x_api_key = os.getenv("BACKEND_XAPI_KEY")
url = f"{backend_base_url}/v3/api/custom/theme-document/answer/{request.form_id}?x-project={x_api_key}" url = f"{backend_base_url}/v3/api/custom/theme-document/answer/{request.form_id}?x-project={x_api_key}"
result = requests.get(url) result = requests.get(url)
result.raise_for_status() # Ensure we raise an error for bad responses result.raise_for_status() # Ensure we raise an error for bad responses
-3
View File
@@ -1,7 +1,4 @@
import os import os
from dotenv import load_dotenv
load_dotenv()
QUIZ_TYPES = { QUIZ_TYPES = {
1: { 1: {
"name": "Single Line Text Inputs", "name": "Single Line Text Inputs",
+1
View File
@@ -0,0 +1 @@
18759
-4
View File
@@ -25,7 +25,3 @@ reportlab==4.3.1
python-docx==1.1.2 python-docx==1.1.2
unstructured==0.17.2 unstructured==0.17.2
pypdf==5.4.0 pypdf==5.4.0
gunicorn==23.0.0
python-dotenv
Executable → Regular
+9 -20
View File
@@ -2,7 +2,7 @@
# Configuration # Configuration
REPO_URL="http://owusu:890eccfcea010beb94a0adba246aaf9258330b70@23.29.118.76:3000/owusu/ds-fire-fighter.git" REPO_URL="http://owusu:890eccfcea010beb94a0adba246aaf9258330b70@23.29.118.76:3000/owusu/ds-fire-fighter.git"
APP_DIR="/home/ec2-user/ds-fire-fighter" APP_DIR="/home/owusu/ds-fire-fighter"
BRANCH="main" BRANCH="main"
PYTHON_VERSION="3.11" PYTHON_VERSION="3.11"
WORKERS=4 WORKERS=4
@@ -61,22 +61,11 @@ command_exists() {
# Function to install Python 3.11 # Function to install Python 3.11
install_python() { install_python() {
log "INFO" "Installing Python ${PYTHON_VERSION}..." log "INFO" "Installing Python ${PYTHON_VERSION}..."
sudo yum update -y sudo apt-get update
sudo yum groupinstall -y "Development Tools" sudo apt-get install -y software-properties-common
sudo yum install -y openssl-devel bzip2-devel libffi-devel xz-devel sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt-get update
# Install Python 3.11 from source sudo apt-get install -y python${PYTHON_VERSION} python${PYTHON_VERSION}-venv python${PYTHON_VERSION}-dev
cd /tmp
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar xzf Python-3.11.0.tgz
cd Python-3.11.0
./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall
# Create symlink for python3.11
sudo ln -sf /usr/local/bin/python3.11 /usr/bin/python3.11
sudo ln -sf /usr/local/bin/pip3.11 /usr/bin/pip3.11
} }
# Function to setup git repository # Function to setup git repository
@@ -97,7 +86,7 @@ setup_repo() {
setup_venv() { setup_venv() {
log "INFO" "Setting up virtual environment..." log "INFO" "Setting up virtual environment..."
if [ ! -d "$APP_DIR/venv" ]; then if [ ! -d "$APP_DIR/venv" ]; then
python3.11 -m venv $APP_DIR/venv python${PYTHON_VERSION} -m venv $APP_DIR/venv
fi fi
source $APP_DIR/venv/bin/activate source $APP_DIR/venv/bin/activate
pip install --upgrade pip pip install --upgrade pip
@@ -165,7 +154,7 @@ Description=Fire Fighter Interview API
After=network.target After=network.target
[Service] [Service]
User=ec2-user User=$USER
WorkingDirectory=$APP_DIR WorkingDirectory=$APP_DIR
Environment="PATH=$APP_DIR/venv/bin" Environment="PATH=$APP_DIR/venv/bin"
Environment="PYTHONPATH=$APP_DIR" Environment="PYTHONPATH=$APP_DIR"
@@ -187,7 +176,7 @@ main() {
log "INFO" "Starting deployment process..." log "INFO" "Starting deployment process..."
# Check and install Python if needed # Check and install Python if needed
if ! command_exists python3.11; then if ! command_exists python${PYTHON_VERSION}; then
install_python install_python
fi fi
+6 -46
View File
@@ -62,8 +62,6 @@ def setup_prompt_template(theme: int, resume: str,full_history=None,form_respons
("system", chat_prompt(theme, resume,full_history,form_response,generate_theme)), ("system", chat_prompt(theme, resume,full_history,form_response,generate_theme)),
MessagesPlaceholder(variable_name="messages") MessagesPlaceholder(variable_name="messages")
]) ])
def fetch_conversation_history(conversation_id: str) -> List[Message]: def fetch_conversation_history(conversation_id: str) -> List[Message]:
""" """
Fetch conversation history from the API using the conversation ID. Fetch conversation history from the API using the conversation ID.
@@ -76,54 +74,18 @@ def fetch_conversation_history(conversation_id: str) -> List[Message]:
response = requests.get(url) response = requests.get(url)
response.raise_for_status() # Raise an error for bad responses response.raise_for_status() # Raise an error for bad responses
data = response.json()["data"] # First JSON parse data = response.json()["data"] # First JSON parse
data = json.loads(json.loads(data))
if isinstance(data, str):
print("Data is a string, parsing as JSON...")
data = json.loads(data)
# Parse the API response into Message objects # Parse the API response into Message objects
messages = [] messages = []
for item in data:
# Check if data exists and is a list role = item.get("role", "unknown")
if data and isinstance(data, list): content = item.get("content", "")
for item in data: timestamp = datetime.now().isoformat() # Use current timestamp if not provided
messages.append(Message(role=role, content=content))
# Check if item is a dictionary
if isinstance(item, dict):
role = item.get("role", "unknown")
content = item.get("content", "")
timestamp = datetime.now().isoformat() # Use current timestamp if not provided
messages.append(Message(role=role, content=content))
elif isinstance(item, str):
# If item is a string, it might be JSON that needs parsing
try:
parsed_item = json.loads(item)
if isinstance(parsed_item, dict):
role = parsed_item.get("role", "unknown")
content = parsed_item.get("content", "")
messages.append(Message(role=role, content=content))
else:
print(f"Parsed item is not a dict: {parsed_item}")
except json.JSONDecodeError as json_err:
print(f"Failed to parse JSON string: {item}, error: {json_err}")
else:
print(f"Unexpected item type: {type(item)} for item: {item}")
else:
print(f"No data or data is not a list. Data: {data}")
return messages return messages
except requests.RequestException as e: except requests.RequestException as e:
print(f"Error fetching conversation history: {e}") print(f"Error fetching conversation history: {e}")
return [] return []
except KeyError as e:
print(f"Expected key not found in response: {e}")
return []
except Exception as e:
print(f"Unexpected error: {e}")
return []
def convert_to_langchain_messages(messages: List[Message]) -> List[HumanMessage | AIMessage]: def convert_to_langchain_messages(messages: List[Message]) -> List[HumanMessage | AIMessage]:
@@ -168,8 +130,6 @@ def ai_chat(query: str, conversation_id: str, theme_id: int, resume: str, full_h
# Fetch conversation history from the API # Fetch conversation history from the API
history = fetch_conversation_history(conversation_id) history = fetch_conversation_history(conversation_id)
print(history)
config = {"configurable": {"thread_id": conversation_id}} config = {"configurable": {"thread_id": conversation_id}}
language = "English" language = "English"
-3
View File
@@ -3,8 +3,5 @@
# Create logs directory if it doesn't exist # Create logs directory if it doesn't exist
mkdir -p logs mkdir -p logs
# Activate virtual environment
source venv/bin/activate
# Start Gunicorn with the configuration # Start Gunicorn with the configuration
gunicorn -c gunicorn_config.py app:app gunicorn -c gunicorn_config.py app:app