Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,80 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv()
|
3 |
+
|
4 |
+
import os
|
5 |
+
from flask import Flask, request, jsonify, render_template
|
6 |
+
from flask_cors import CORS
|
7 |
+
|
8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
9 |
+
from langchain_community.vectorstores import Chroma
|
10 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
11 |
+
from langchain_core.prompts import PromptTemplate
|
12 |
+
from langchain.chains import RetrievalQA
|
13 |
+
|
14 |
+
# === Step 1: Load API Key ===
|
15 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
16 |
+
if not GOOGLE_API_KEY:
|
17 |
+
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
|
18 |
+
|
19 |
+
# === Step 2: Initialize LLM (Gemini) ===
|
20 |
+
llm = ChatGoogleGenerativeAI(
|
21 |
+
model="gemini-2.0-flash-lite",
|
22 |
+
google_api_key=GOOGLE_API_KEY,
|
23 |
+
convert_system_message_to_human=True
|
24 |
+
)
|
25 |
+
|
26 |
+
# === Step 3: Load Chroma Vector Store ===
|
27 |
+
embedding_model = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5")
|
28 |
+
vectordb = Chroma(
|
29 |
+
persist_directory="./chroma_store",
|
30 |
+
embedding_function=embedding_model,
|
31 |
+
collection_name="pdf_search_chroma"
|
32 |
+
)
|
33 |
+
retriever = vectordb.as_retriever(search_kwargs={"k": 6})
|
34 |
+
|
35 |
+
# === Step 4: Custom Prompt Template ===
|
36 |
+
prompt_template = PromptTemplate.from_template("""
|
37 |
+
You are an intelligent assistant for students asking about their university.
|
38 |
+
If answer is not defined or not clearly understood, ask for clarification.
|
39 |
+
Answer clearly and helpfully based on the retrieved context. Do not make up information or suggestions.
|
40 |
+
Context:
|
41 |
+
{context}
|
42 |
+
Question:
|
43 |
+
{question}
|
44 |
+
Answer:
|
45 |
+
""")
|
46 |
+
|
47 |
+
# === Step 5: Create Retrieval-QA Chain ===
|
48 |
+
qa_chain = RetrievalQA.from_chain_type(
|
49 |
+
llm=llm,
|
50 |
+
chain_type="stuff",
|
51 |
+
retriever=retriever,
|
52 |
+
chain_type_kwargs={"prompt": prompt_template}
|
53 |
+
)
|
54 |
+
|
55 |
+
# === Step 6: Flask Setup ===
|
56 |
+
app = Flask(__name__, static_folder="static", template_folder="templates")
|
57 |
+
# === Step 7: Serve Frontend ===
|
58 |
+
|
59 |
+
@app.route("/")
|
60 |
+
def index():
|
61 |
+
return render_template("index.html") # Make sure chat.html exists
|
62 |
+
|
63 |
+
|
64 |
+
@app.route('/get', methods=['POST'])
|
65 |
+
def chat():
|
66 |
+
data = request.json
|
67 |
+
query = data.get('message', '').strip()
|
68 |
+
|
69 |
+
if not query:
|
70 |
+
return jsonify({"error": "No message provided."}), 400
|
71 |
+
|
72 |
+
try:
|
73 |
+
response = qa_chain.run(query)
|
74 |
+
return jsonify({"response": response})
|
75 |
+
except Exception as e:
|
76 |
+
return jsonify({"error": str(e)}), 500
|
77 |
+
|
78 |
+
# === Step 9: Run the App ===
|
79 |
+
if __name__ == '__main__':
|
80 |
+
app.run(debug=False)
|