Spaces:
Running
on
Zero
Running
on
Zero
zamalali
commited on
Commit
·
5aa268a
1
Parent(s):
cf6cb6b
Enhance conversation function with detailed comments for better clarity and maintainability
Browse files
app.py
CHANGED
@@ -204,6 +204,7 @@ def conversation(
|
|
204 |
hf_token,
|
205 |
model_path,
|
206 |
):
|
|
|
207 |
if hf_token.strip() != "" and model_path.strip() != "":
|
208 |
llm = HuggingFaceEndpoint(
|
209 |
repo_id=model_path,
|
@@ -219,6 +220,7 @@ def conversation(
|
|
219 |
huggingfacehub_api_token=os.getenv("P_HF_TOKEN", "None"),
|
220 |
)
|
221 |
|
|
|
222 |
text_collection = vectordb_client.get_collection(
|
223 |
"text_db", embedding_function=sentence_transformer_ef
|
224 |
)
|
@@ -226,6 +228,7 @@ def conversation(
|
|
226 |
"image_db", embedding_function=sentence_transformer_ef
|
227 |
)
|
228 |
|
|
|
229 |
results = text_collection.query(
|
230 |
query_texts=[msg], include=["documents"], n_results=num_context
|
231 |
)["documents"][0]
|
@@ -236,9 +239,9 @@ def conversation(
|
|
236 |
n_results=img_context,
|
237 |
)
|
238 |
|
|
|
239 |
img_links = similar_images["metadatas"][0] if similar_images["metadatas"] else []
|
240 |
images_and_locs = []
|
241 |
-
|
242 |
for distance, link in zip(similar_images["distances"][0], img_links):
|
243 |
try:
|
244 |
img = Image.open(io.BytesIO(base64.b64decode(link["image"])))
|
@@ -247,12 +250,14 @@ def conversation(
|
|
247 |
except Exception as e:
|
248 |
print(f"Error decoding image: {e}")
|
249 |
|
|
|
250 |
if not images_and_locs:
|
251 |
-
placeholder_path = "assets/placeholder.jpg"
|
252 |
if not os.path.exists(placeholder_path):
|
253 |
raise FileNotFoundError(f"Placeholder image not found at {placeholder_path}")
|
254 |
images_and_locs = [(placeholder_path, "No images found")]
|
255 |
|
|
|
256 |
img_desc = "\n".join(similar_images["documents"][0]) if images_and_locs else "No Images Are Provided"
|
257 |
template = """
|
258 |
Context:
|
@@ -270,8 +275,13 @@ def conversation(
|
|
270 |
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
271 |
context = "\n\n".join(results)
|
272 |
|
|
|
273 |
response = llm(prompt.format(context=context, question=msg, images=img_desc))
|
274 |
|
|
|
|
|
|
|
|
|
275 |
return history + [(msg, response)], results, images_and_locs
|
276 |
|
277 |
|
|
|
204 |
hf_token,
|
205 |
model_path,
|
206 |
):
|
207 |
+
# Initialize LLM
|
208 |
if hf_token.strip() != "" and model_path.strip() != "":
|
209 |
llm = HuggingFaceEndpoint(
|
210 |
repo_id=model_path,
|
|
|
220 |
huggingfacehub_api_token=os.getenv("P_HF_TOKEN", "None"),
|
221 |
)
|
222 |
|
223 |
+
# Get vector database collections
|
224 |
text_collection = vectordb_client.get_collection(
|
225 |
"text_db", embedding_function=sentence_transformer_ef
|
226 |
)
|
|
|
228 |
"image_db", embedding_function=sentence_transformer_ef
|
229 |
)
|
230 |
|
231 |
+
# Query text and image collections
|
232 |
results = text_collection.query(
|
233 |
query_texts=[msg], include=["documents"], n_results=num_context
|
234 |
)["documents"][0]
|
|
|
239 |
n_results=img_context,
|
240 |
)
|
241 |
|
242 |
+
# Process similar images
|
243 |
img_links = similar_images["metadatas"][0] if similar_images["metadatas"] else []
|
244 |
images_and_locs = []
|
|
|
245 |
for distance, link in zip(similar_images["distances"][0], img_links):
|
246 |
try:
|
247 |
img = Image.open(io.BytesIO(base64.b64decode(link["image"])))
|
|
|
250 |
except Exception as e:
|
251 |
print(f"Error decoding image: {e}")
|
252 |
|
253 |
+
# Fallback to placeholder if no images are found
|
254 |
if not images_and_locs:
|
255 |
+
placeholder_path = "assets/placeholder.jpg" # Ensure this exists
|
256 |
if not os.path.exists(placeholder_path):
|
257 |
raise FileNotFoundError(f"Placeholder image not found at {placeholder_path}")
|
258 |
images_and_locs = [(placeholder_path, "No images found")]
|
259 |
|
260 |
+
# Prepare prompt for the LLM
|
261 |
img_desc = "\n".join(similar_images["documents"][0]) if images_and_locs else "No Images Are Provided"
|
262 |
template = """
|
263 |
Context:
|
|
|
275 |
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
276 |
context = "\n\n".join(results)
|
277 |
|
278 |
+
# Generate response
|
279 |
response = llm(prompt.format(context=context, question=msg, images=img_desc))
|
280 |
|
281 |
+
# Validate `images_and_locs` before returning
|
282 |
+
if not all(isinstance(item, tuple) and len(item) == 2 for item in images_and_locs):
|
283 |
+
raise ValueError("ret_images must be a list of (media, caption) tuples.")
|
284 |
+
|
285 |
return history + [(msg, response)], results, images_and_locs
|
286 |
|
287 |
|