# Use the official Python 3.9 image as the base image
FROM python:3.9

# Expose the port
EXPOSE 7860

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Set the PYNGROK_CONFIG environment variable
ENV PYNGROK_CONFIG /tmp/pyngrok.yml

# Set the NGROK_PATH environment variable to a writable location
ENV NGROK_PATH /tmp/ngrok

# Copy requirements.txt into the container
COPY requirements.txt .

# RUN apt-get update 
# RUN apt-get install -y wget
# RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# RUN apt-get install ./google-chrome-stable_current_amd64.deb -y


# RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# RUN apt-get -y update
# RUN apt-get install -y google-chrome-stable

# # install chromedriver
# RUN apt-get install -yqq unzip
# RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
# RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/





# RUN apt install wget -y
# RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux64.tar.gz
# RUN tar -xzvf geckodriver-v0.32.0-linux64.tar.gz -C /usr/local/bin
# RUN chmod +x /usr/local/bin/geckodriver
# RUN geckodriver -V


# RUN apt install firefox-esr -y
# RUN apt-get install firefox-geckodriver

# Upgrade pip and install the required packages
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

# Install sudo and create the necessary directories before copying the files
RUN apt-get update && \
    apt-get install -y sudo && \
    mkdir -p /code/image

# Creates a non-root user with an explicit UID and adds permission to access the /code folder
RUN adduser -u 5678 --disabled-password --gecos "" appuser && \
    usermod -aG sudo appuser && \
    usermod -aG root appuser && \
    chown -R appuser:appuser /code

# Create the pyngrok bin directory and set the ownership and permissions for appuser
RUN mkdir -p /usr/local/lib/python3.9/site-packages/pyngrok/bin && \
    chown -R appuser:appuser /usr/local/lib/python3.9/site-packages/pyngrok/bin && \
    chmod -R 777 /usr/local/lib/python3.9/site-packages/pyngrok/bin

RUN mkdir -p /.ngrok2 && \
    chown -R appuser:appuser /.ngrok2 && \
    chmod -R 777 /.ngrok2

RUN apt-get update && \
    apt-get install -y curl

RUN echo "deb http://deb.debian.org/debian/ unstable main contrib non-free" >> /etc/apt/sources.list.d/debian.list


# RUN apt install firefox-esr && \
#     apt install geckodriver

# Set the working directory and copy the files
WORKDIR /code

# Set the ownership and permissions for the /code directory and its contents
RUN chown -R appuser:appuser /code && \
    chmod -R 777 /code

COPY . /code

# RUN chown -R appuser:appuser /code/data.csv && \
#     chmod -R 777 /code/data.csv

# Copy the pyngrok.yml configuration file
COPY pyngrok.yml /tmp/pyngrok.yml

# Set the TRANSFORMERS_CACHE environment variable to a cache directory inside /tmp
ENV TRANSFORMERS_CACHE /tmp/transformers_cache
ENV TORCH_HOME /tmp/torch_cache

USER appuser


RUN git clone https://github.com/rphrp1985/gpt4f
# WORKDIR /gpt4f
# COPY . /gpt4f
# RUN cd gpt4f
# RUN ls

# cp -R / /root/dest-folder
RUN cp -R gpt4f/* /code
RUN ls
CMD python run.py







# Start the application using pyngrok
# CMD python main.py
# Get the public IP address and display it
# RUN curl -s https://api.ipify.org | xargs echo "Public IP:"
RUN pip install gunicorn

# Start the Uvicorn server
# ENTRYPOINT ["python", "main.py"]
# CMD ["sh", "-c", "python main.py & sleep infinity"]
CMD ["gunicorn", "--bind",  "0.0.0.0:7860","run:app"]