Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
|
3 |
from flask import Flask, render_template, request, jsonify
|
4 |
import faiss
|
@@ -8,6 +7,7 @@ from sentence_transformers import SentenceTransformer
|
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
from langchain_groq import ChatGroq
|
10 |
import re
|
|
|
11 |
import os
|
12 |
from dotenv import load_dotenv
|
13 |
load_dotenv()
|
@@ -15,11 +15,23 @@ load_dotenv()
|
|
15 |
app = Flask(__name__)
|
16 |
|
17 |
# Load Model and FAISS Index
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
model = SentenceTransformer('./sentence-transformers_all-MiniLM-L6-v2')
|
19 |
index = faiss.read_index("faiss_index.bin")
|
20 |
groq_api_key = os.getenv('GROQ_API_KEY')
|
21 |
model_name = "llama-3.3-70b-versatile"
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
llm = ChatGroq(
|
24 |
temperature=0,
|
25 |
groq_api_key=groq_api_key,
|
@@ -107,7 +119,9 @@ def search_text(query, top_k=2):
|
|
107 |
def home():
|
108 |
return render_template("index.html")
|
109 |
|
|
|
110 |
@app.route("/query", methods=["POST"])
|
|
|
111 |
def query_pdf():
|
112 |
query = request.json.get("query")
|
113 |
|
@@ -134,8 +148,6 @@ def query_pdf():
|
|
134 |
if query_type == "rude":
|
135 |
return jsonify({"text": "I'm here to assist you professionally.", "images": []})
|
136 |
|
137 |
-
|
138 |
-
|
139 |
# Search for relevant PDF content using FAISS
|
140 |
results = search_text(query, top_k=3)
|
141 |
|
@@ -150,15 +162,16 @@ def query_pdf():
|
|
150 |
"""
|
151 |
### YOU ARE AN EXELSYS EASYHR GUIDE ASSISTANT:
|
152 |
### INSTRUCTIONS:
|
153 |
-
-
|
154 |
-
-
|
155 |
-
-
|
|
|
|
|
156 |
- If the user greets you (e.g., "Hello", "Hi", "Good morning"), respond politely but keep it brief.
|
157 |
- If the query is unrelated to Exelsys easyHR, respond with: "I'm here to assist with Exelsys easyHR queries only."
|
158 |
-
-
|
159 |
-
|
160 |
|
161 |
-
|
162 |
### USER QUERY:
|
163 |
{query}
|
164 |
|
@@ -176,20 +189,23 @@ def query_pdf():
|
|
176 |
# Convert response to string
|
177 |
response_text = str(chat_response.content)
|
178 |
|
179 |
-
#
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
183 |
|
184 |
# Final response JSON
|
185 |
response = {
|
186 |
-
"text":
|
187 |
-
|
188 |
}
|
189 |
print(response)
|
190 |
|
191 |
return jsonify(response)
|
192 |
|
|
|
193 |
if __name__ == "__main__":
|
194 |
-
app.run(
|
195 |
|
|
|
|
|
1 |
|
2 |
from flask import Flask, render_template, request, jsonify
|
3 |
import faiss
|
|
|
7 |
from langchain.prompts import PromptTemplate
|
8 |
from langchain_groq import ChatGroq
|
9 |
import re
|
10 |
+
from langchain_openai import AzureChatOpenAI
|
11 |
import os
|
12 |
from dotenv import load_dotenv
|
13 |
load_dotenv()
|
|
|
15 |
app = Flask(__name__)
|
16 |
|
17 |
# Load Model and FAISS Index
|
18 |
+
|
19 |
+
|
20 |
+
# AzureOpenAiEndpoint= os.getenv("AZURE_OPENAI_ENDPOINT")
|
21 |
+
# AzureOpenAiKey= os.getenv("AZURE_OPENAI_KEY")
|
22 |
+
# OpenaiApiVersion= os.getenv("OPENAI_API_VERSION")
|
23 |
+
# DeploymentName= os.getenv("DEPLOYMENT_NAME")
|
24 |
model = SentenceTransformer('./sentence-transformers_all-MiniLM-L6-v2')
|
25 |
index = faiss.read_index("faiss_index.bin")
|
26 |
groq_api_key = os.getenv('GROQ_API_KEY')
|
27 |
model_name = "llama-3.3-70b-versatile"
|
28 |
|
29 |
+
# llm = AzureChatOpenAI(
|
30 |
+
# openai_api_version= OpenaiApiVersion, # Change based on your API version
|
31 |
+
# deployment_name = DeploymentName, # The model deployment name in Azure
|
32 |
+
# openai_api_key= AzureOpenAiKey, # Use the environment variable
|
33 |
+
# azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"], # Use the environment variable
|
34 |
+
# )
|
35 |
llm = ChatGroq(
|
36 |
temperature=0,
|
37 |
groq_api_key=groq_api_key,
|
|
|
119 |
def home():
|
120 |
return render_template("index.html")
|
121 |
|
122 |
+
import re
|
123 |
@app.route("/query", methods=["POST"])
|
124 |
+
|
125 |
def query_pdf():
|
126 |
query = request.json.get("query")
|
127 |
|
|
|
148 |
if query_type == "rude":
|
149 |
return jsonify({"text": "I'm here to assist you professionally.", "images": []})
|
150 |
|
|
|
|
|
151 |
# Search for relevant PDF content using FAISS
|
152 |
results = search_text(query, top_k=3)
|
153 |
|
|
|
162 |
"""
|
163 |
### YOU ARE AN EXELSYS EASYHR GUIDE ASSISTANT:
|
164 |
### INSTRUCTIONS:
|
165 |
+
- Identify the most relevant topic based on the user query.
|
166 |
+
- Provide step-by-step guidance for that topic only.
|
167 |
+
- Base your response **only** on the selected topic's content.
|
168 |
+
- Do not use content from other topics.
|
169 |
+
- If no relevant topic is found, respond with: "Not found."
|
170 |
- If the user greets you (e.g., "Hello", "Hi", "Good morning"), respond politely but keep it brief.
|
171 |
- If the query is unrelated to Exelsys easyHR, respond with: "I'm here to assist with Exelsys easyHR queries only."
|
172 |
+
- Format the response like this:
|
173 |
+
<p>Response text here...</p> \n <a>static/output_images/page_67_img_2.png</a>\n <a>static/output_images/page_45_img_1.jpg</a>
|
174 |
|
|
|
175 |
### USER QUERY:
|
176 |
{query}
|
177 |
|
|
|
189 |
# Convert response to string
|
190 |
response_text = str(chat_response.content)
|
191 |
|
192 |
+
# Extract multiple image paths from response
|
193 |
+
image_pattern = r"static/output_images/[\w\d_]+\.png"
|
194 |
+
extracted_images = re.findall(image_pattern, response_text)
|
195 |
+
|
196 |
+
# Remove image paths from text
|
197 |
+
cleaned_text = re.sub(image_pattern, "", response_text).strip()
|
198 |
|
199 |
# Final response JSON
|
200 |
response = {
|
201 |
+
"text": cleaned_text,
|
202 |
+
"images": extracted_images # Store multiple images in a list
|
203 |
}
|
204 |
print(response)
|
205 |
|
206 |
return jsonify(response)
|
207 |
|
208 |
+
|
209 |
if __name__ == "__main__":
|
210 |
+
app.run(debug=True)
|
211 |
|