Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,44 @@
|
|
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 |
-
{question}
|
46 |
-
|
47 |
-
Answer:
|
48 |
-
""")
|
49 |
-
|
50 |
-
# === Step 5: Create Retrieval-QA Chain ===
|
51 |
-
qa_chain = RetrievalQA.from_chain_type(
|
52 |
-
llm=llm,
|
53 |
-
chain_type="stuff",
|
54 |
-
retriever=retriever,
|
55 |
-
chain_type_kwargs={"prompt": prompt_template}
|
56 |
-
)
|
57 |
-
|
58 |
-
# === Step 6: Flask Setup ===
|
59 |
-
app = Flask(__name__)
|
60 |
-
# === Step 7: Serve Frontend ===
|
61 |
-
|
62 |
-
@app.route("/")
|
63 |
-
def index():
|
64 |
-
return render_template("index.html") # Make sure chat.html exists
|
65 |
-
|
66 |
-
|
67 |
-
@app.route('/get', methods=['POST'])
|
68 |
-
def chat():
|
69 |
-
data = request.json
|
70 |
-
query = data.get('message', '').strip()
|
71 |
-
|
72 |
-
if not query:
|
73 |
-
return jsonify({"error": "No message provided."}), 400
|
74 |
-
|
75 |
-
try:
|
76 |
-
response = qa_chain.run(query)
|
77 |
-
return jsonify({"response": response})
|
78 |
-
except Exception as e:
|
79 |
-
return jsonify({"error": str(e)}), 500
|
80 |
-
|
81 |
-
# === Step 9: Run the App ===
|
82 |
-
if __name__ == '__main__':
|
83 |
-
app.run(debug=False, host='0.0.0.0', port=7860)
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
# Create a non-root user
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
USER user
|
6 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
7 |
+
|
8 |
+
# Set working directory
|
9 |
+
WORKDIR /app
|
10 |
+
|
11 |
+
# Copy and install Python dependencies
|
12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
14 |
+
|
15 |
+
# Install system dependencies and build tools
|
16 |
+
USER root
|
17 |
+
RUN apt-get update && apt-get install -y \
|
18 |
+
python3 \
|
19 |
+
python3-pip \
|
20 |
+
python3-venv \
|
21 |
+
wget \
|
22 |
+
build-essential
|
23 |
+
|
24 |
+
# Install newer SQLite version
|
25 |
+
WORKDIR /tmp
|
26 |
+
RUN wget https://www.sqlite.org/2023/sqlite-autoconf-3410200.tar.gz \
|
27 |
+
&& tar -xvf sqlite-autoconf-3410200.tar.gz \
|
28 |
+
&& cd sqlite-autoconf-3410200 \
|
29 |
+
&& ./configure \
|
30 |
+
&& make \
|
31 |
+
&& make install
|
32 |
+
|
33 |
+
# Update library path to use the new SQLite
|
34 |
+
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
|
35 |
+
|
36 |
+
# Go back to non-root user and app directory
|
37 |
+
USER user
|
38 |
+
WORKDIR /app
|
39 |
+
|
40 |
+
# Copy the app source code
|
41 |
+
COPY --chown=user . /app
|
42 |
+
|
43 |
+
# Start the app
|
44 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app", "--preload"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|