Spaces:
Sleeping
Sleeping
Commit ·
959e7c3
1
Parent(s): 119ed2d
fix: Update Dockerfile and application configuration for improved performance and compatibility
Browse files- Dockerfile +22 -0
- app.py +9 -3
- backend/Dockerfile +0 -44
- backend/functions.py +8 -8
- backend/main.py +1 -1
- backend/models.py +1 -1
- backend/tools.py +1 -1
- backend/tts.py +1 -0
- backend/utils.py +1 -1
- requirements.txt +1 -1
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 5 |
+
PYTHONUNBUFFERED=1
|
| 6 |
+
|
| 7 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
+
ffmpeg \
|
| 9 |
+
libsndfile1 \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade pip \
|
| 14 |
+
&& pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
COPY . .
|
| 17 |
+
|
| 18 |
+
# Gradio defaults to 7860; change if you override `demo.launch(server_port=...)`
|
| 19 |
+
ENV PORT=7860
|
| 20 |
+
EXPOSE 7860
|
| 21 |
+
|
| 22 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -15,7 +15,7 @@ import json
|
|
| 15 |
from pydub import AudioSegment
|
| 16 |
import ast
|
| 17 |
from backend.utils import get_device
|
| 18 |
-
if get_device() == "
|
| 19 |
load_dotenv(override=True)
|
| 20 |
|
| 21 |
|
|
@@ -476,5 +476,11 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="orange", second
|
|
| 476 |
show_progress="hidden"
|
| 477 |
)
|
| 478 |
|
| 479 |
-
demo.queue(default_concurrency_limit=
|
| 480 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from pydub import AudioSegment
|
| 16 |
import ast
|
| 17 |
from backend.utils import get_device
|
| 18 |
+
if get_device() == "cpu":
|
| 19 |
load_dotenv(override=True)
|
| 20 |
|
| 21 |
|
|
|
|
| 476 |
show_progress="hidden"
|
| 477 |
)
|
| 478 |
|
| 479 |
+
demo.queue(default_concurrency_limit=100)
|
| 480 |
+
demo.launch(
|
| 481 |
+
debug=True,
|
| 482 |
+
show_error=True,
|
| 483 |
+
share=False,
|
| 484 |
+
server_name="0.0.0.0",
|
| 485 |
+
server_port=int(os.getenv("PORT", 7860)),
|
| 486 |
+
)
|
backend/Dockerfile
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
# Use an official Python runtime as a parent image
|
| 2 |
-
FROM python:3.10-slim
|
| 3 |
-
|
| 4 |
-
# Set the working directory in the container
|
| 5 |
-
WORKDIR /app
|
| 6 |
-
|
| 7 |
-
# Prevent python from writing pyc files to disc
|
| 8 |
-
ENV PYTHONDONTWRITEBYTECODE 1
|
| 9 |
-
# Ensure python output is sent straight to terminal (useful for logs)
|
| 10 |
-
ENV PYTHONUNBUFFERED 1
|
| 11 |
-
|
| 12 |
-
# Install system dependencies if any (e.g., if specific libraries needed them)
|
| 13 |
-
# RUN apt-get update && apt-get install -y --no-install-recommends some-package && rm -rf /var/lib/apt/lists/*
|
| 14 |
-
|
| 15 |
-
# Copy the requirements file into the container at /app
|
| 16 |
-
COPY requirements.txt .
|
| 17 |
-
# Install any needed packages specified in requirements.txt
|
| 18 |
-
# Using --no-cache-dir to reduce image size
|
| 19 |
-
RUN pip install --no-cache-dir --upgrade pip && \
|
| 20 |
-
pip install --no-cache-dir -r requirements.txt
|
| 21 |
-
|
| 22 |
-
# Copy the rest of the backend application code into the container at /app
|
| 23 |
-
COPY . .
|
| 24 |
-
|
| 25 |
-
# Make port 8000 available to the world outside this container
|
| 26 |
-
EXPOSE 8000
|
| 27 |
-
|
| 28 |
-
# Define environment variables (placeholders, will be overridden by compose/env_file)
|
| 29 |
-
ENV MONGO_URL=""
|
| 30 |
-
ENV OPENAI_API_KEY=""
|
| 31 |
-
ENV JAI_API_KEY=""
|
| 32 |
-
ENV JAI_BASE_URL=""
|
| 33 |
-
ENV TYPHOON_API_KEY=""
|
| 34 |
-
ENV TYPHOON_BASE_URL=""
|
| 35 |
-
ENV GEMMA_API_KEY=""
|
| 36 |
-
ENV GEMMA_BASE_URL=""
|
| 37 |
-
ENV LANGFUSE_SECRET_KEY=""
|
| 38 |
-
ENV LANGFUSE_PUBLIC_KEY=""
|
| 39 |
-
ENV LANGFUSE_HOST=""
|
| 40 |
-
ENV PORT=8000
|
| 41 |
-
ENV HOST=0.0.0.0
|
| 42 |
-
ENV GEMINI_API_KEY=""
|
| 43 |
-
# Run uvicorn server when the container launches
|
| 44 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/functions.py
CHANGED
|
@@ -14,7 +14,7 @@ from utils import get_device
|
|
| 14 |
# from transformers import AutoTokenizer # No longer needed for reranker
|
| 15 |
|
| 16 |
# Load environment variables
|
| 17 |
-
if get_device() == "
|
| 18 |
load_dotenv(override=True)
|
| 19 |
|
| 20 |
# Set up logging
|
|
@@ -34,13 +34,13 @@ class MongoHybridSearch:
|
|
| 34 |
Initialize MongoDB connection and embedder.
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
-
self.client = AsyncIOMotorClient(mongo_uri)
|
| 38 |
-
self.database = self.client[database_name]
|
| 39 |
-
# Consider making collection name configurable
|
| 40 |
-
self.collection = self.database["homeshopping"]
|
| 41 |
-
# self.collection_fact = self.database["SCG_financial_report_jai"]
|
| 42 |
-
self.llm_analyzer = models.LLMFinanceAnalyzer()
|
| 43 |
-
self.embedder = models.Embedder() # Instantiate Embedder class from models
|
| 44 |
logger.info("MongoHybridSearch initialized successfully.")
|
| 45 |
except Exception as e:
|
| 46 |
logger.error(f"Failed to initialize MongoHybridSearch: {e}")
|
|
|
|
| 14 |
# from transformers import AutoTokenizer # No longer needed for reranker
|
| 15 |
|
| 16 |
# Load environment variables
|
| 17 |
+
if get_device() == "cpu":
|
| 18 |
load_dotenv(override=True)
|
| 19 |
|
| 20 |
# Set up logging
|
|
|
|
| 34 |
Initialize MongoDB connection and embedder.
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
+
# self.client = AsyncIOMotorClient(mongo_uri)
|
| 38 |
+
# self.database = self.client[database_name]
|
| 39 |
+
# # Consider making collection name configurable
|
| 40 |
+
# self.collection = self.database["homeshopping"]
|
| 41 |
+
# # self.collection_fact = self.database["SCG_financial_report_jai"]
|
| 42 |
+
# self.llm_analyzer = models.LLMFinanceAnalyzer()
|
| 43 |
+
# self.embedder = models.Embedder() # Instantiate Embedder class from models
|
| 44 |
logger.info("MongoHybridSearch initialized successfully.")
|
| 45 |
except Exception as e:
|
| 46 |
logger.error(f"Failed to initialize MongoHybridSearch: {e}")
|
backend/main.py
CHANGED
|
@@ -17,7 +17,7 @@ sys.path.append(os.path.abspath('./backend'))
|
|
| 17 |
from models import LLMFinanceAnalyzer
|
| 18 |
from functions import MongoHybridSearch
|
| 19 |
from utils import get_device
|
| 20 |
-
if get_device() == "
|
| 21 |
load_dotenv(override=True)
|
| 22 |
|
| 23 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
|
|
| 17 |
from models import LLMFinanceAnalyzer
|
| 18 |
from functions import MongoHybridSearch
|
| 19 |
from utils import get_device
|
| 20 |
+
if get_device() == "cpu":
|
| 21 |
load_dotenv(override=True)
|
| 22 |
|
| 23 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
backend/models.py
CHANGED
|
@@ -20,7 +20,7 @@ from systemprompt import (
|
|
| 20 |
get_non_rag_prompt,
|
| 21 |
)
|
| 22 |
from utils import get_device
|
| 23 |
-
if get_device() == "
|
| 24 |
load_dotenv(override=True)
|
| 25 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 26 |
logger = logging.getLogger(__name__)
|
|
|
|
| 20 |
get_non_rag_prompt,
|
| 21 |
)
|
| 22 |
from utils import get_device
|
| 23 |
+
if get_device() == "cpu":
|
| 24 |
load_dotenv(override=True)
|
| 25 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 26 |
logger = logging.getLogger(__name__)
|
backend/tools.py
CHANGED
|
@@ -4,7 +4,7 @@ from dotenv import load_dotenv
|
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
from utils import get_device
|
| 7 |
-
if get_device() == "
|
| 8 |
load_dotenv(override=True)
|
| 9 |
|
| 10 |
|
|
|
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
from utils import get_device
|
| 7 |
+
if get_device() == "cpu":
|
| 8 |
load_dotenv(override=True)
|
| 9 |
|
| 10 |
|
backend/tts.py
CHANGED
|
@@ -67,6 +67,7 @@ def synthesize_text(text: str, lang = 'th' , speed = 2.0):
|
|
| 67 |
if first_chunk:
|
| 68 |
samples = samples[600:] # Optionally drop start of first chunk
|
| 69 |
first_chunk = False
|
|
|
|
| 70 |
yield (24000, samples)
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Error during TTS synthesis for text '{text}': {e}")
|
|
|
|
| 67 |
if first_chunk:
|
| 68 |
samples = samples[600:] # Optionally drop start of first chunk
|
| 69 |
first_chunk = False
|
| 70 |
+
|
| 71 |
yield (24000, samples)
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error during TTS synthesis for text '{text}': {e}")
|
backend/utils.py
CHANGED
|
@@ -28,7 +28,7 @@ def get_device():
|
|
| 28 |
return "mps"
|
| 29 |
else:
|
| 30 |
return "cpu"
|
| 31 |
-
if get_device() == "
|
| 32 |
load_dotenv(override=True)
|
| 33 |
|
| 34 |
device = get_device()
|
|
|
|
| 28 |
return "mps"
|
| 29 |
else:
|
| 30 |
return "cpu"
|
| 31 |
+
if get_device() == "cpu":
|
| 32 |
load_dotenv(override=True)
|
| 33 |
|
| 34 |
device = get_device()
|
requirements.txt
CHANGED
|
@@ -27,7 +27,7 @@ google-cloud-speech
|
|
| 27 |
# sentence-transformers==3.4.1
|
| 28 |
https://huggingface.co/jts-ai-team/modifiedfastrtc/resolve/main/fastrtc-0.0.24-py3-none-any.whl
|
| 29 |
onnxruntime==1.21.0
|
| 30 |
-
gradio==5.
|
| 31 |
|
| 32 |
|
| 33 |
nemo_toolkit==2.4.0
|
|
|
|
| 27 |
# sentence-transformers==3.4.1
|
| 28 |
https://huggingface.co/jts-ai-team/modifiedfastrtc/resolve/main/fastrtc-0.0.24-py3-none-any.whl
|
| 29 |
onnxruntime==1.21.0
|
| 30 |
+
gradio==5.44.1
|
| 31 |
|
| 32 |
|
| 33 |
nemo_toolkit==2.4.0
|