Spaces:
Running
on
Zero
Running
on
Zero
zamalali
commited on
Commit
·
a993b94
1
Parent(s):
55c120e
Refactor conversation function to improve code clarity and add validation for image data format
Browse files
app.py
CHANGED
@@ -205,7 +205,7 @@ def conversation(
|
|
205 |
model_path,
|
206 |
):
|
207 |
# Initialize LLM
|
208 |
-
if hf_token.strip()
|
209 |
llm = HuggingFaceEndpoint(
|
210 |
repo_id=model_path,
|
211 |
temperature=temperature,
|
@@ -220,7 +220,7 @@ def conversation(
|
|
220 |
huggingfacehub_api_token=os.getenv("P_HF_TOKEN", "None"),
|
221 |
)
|
222 |
|
223 |
-
# Get vector database
|
224 |
text_collection = vectordb_client.get_collection(
|
225 |
"text_db", embedding_function=sentence_transformer_ef
|
226 |
)
|
@@ -239,9 +239,10 @@ def conversation(
|
|
239 |
n_results=img_context,
|
240 |
)
|
241 |
|
242 |
-
# Process
|
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,43 +251,46 @@ def conversation(
|
|
250 |
except Exception as e:
|
251 |
print(f"Error decoding image: {e}")
|
252 |
|
253 |
-
# Fallback to placeholder if no images are
|
|
|
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 |
-
#
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
Included Images:
|
267 |
-
{images}
|
268 |
-
|
269 |
-
Question:
|
270 |
-
{question}
|
271 |
|
272 |
-
|
273 |
-
|
274 |
-
"""
|
275 |
-
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
276 |
context = "\n\n".join(results)
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
response = llm(prompt.format(context=context, question=msg, images=img_desc))
|
280 |
|
281 |
-
|
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 |
|
288 |
|
289 |
-
|
290 |
def check_validity_and_llm(session_states):
|
291 |
if session_states.get("processed", False) == True:
|
292 |
return gr.Tabs(selected=2)
|
|
|
205 |
model_path,
|
206 |
):
|
207 |
# Initialize LLM
|
208 |
+
if hf_token.strip() and model_path.strip():
|
209 |
llm = HuggingFaceEndpoint(
|
210 |
repo_id=model_path,
|
211 |
temperature=temperature,
|
|
|
220 |
huggingfacehub_api_token=os.getenv("P_HF_TOKEN", "None"),
|
221 |
)
|
222 |
|
223 |
+
# Get collections from the vector database
|
224 |
text_collection = vectordb_client.get_collection(
|
225 |
"text_db", embedding_function=sentence_transformer_ef
|
226 |
)
|
|
|
239 |
n_results=img_context,
|
240 |
)
|
241 |
|
242 |
+
# Process image results
|
243 |
img_links = similar_images["metadatas"][0] if similar_images["metadatas"] else []
|
244 |
images_and_locs = []
|
245 |
+
|
246 |
for distance, link in zip(similar_images["distances"][0], img_links):
|
247 |
try:
|
248 |
img = Image.open(io.BytesIO(base64.b64decode(link["image"])))
|
|
|
251 |
except Exception as e:
|
252 |
print(f"Error decoding image: {e}")
|
253 |
|
254 |
+
# Fallback to placeholder if no images are valid
|
255 |
+
placeholder_path = "assets/placeholder.jpg"
|
256 |
if not images_and_locs:
|
|
|
257 |
if not os.path.exists(placeholder_path):
|
258 |
raise FileNotFoundError(f"Placeholder image not found at {placeholder_path}")
|
259 |
images_and_locs = [(placeholder_path, "No images found")]
|
260 |
|
261 |
+
# Validate data format
|
262 |
+
if not all(
|
263 |
+
isinstance(item, tuple) and len(item) == 2 and isinstance(item[0], (str, Image.Image)) and isinstance(item[1], str)
|
264 |
+
for item in images_and_locs
|
265 |
+
):
|
266 |
+
raise ValueError("images_and_locs must be a list of (image, caption) tuples.")
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
+
# Prepare LLM prompt
|
269 |
+
img_desc = "\n".join(similar_images["documents"][0]) if images_and_locs else "No Images Are Provided"
|
|
|
|
|
270 |
context = "\n\n".join(results)
|
271 |
+
prompt = PromptTemplate(
|
272 |
+
template="""
|
273 |
+
Context:
|
274 |
+
{context}
|
275 |
+
|
276 |
+
Included Images:
|
277 |
+
{images}
|
278 |
+
|
279 |
+
Question:
|
280 |
+
{question}
|
281 |
+
|
282 |
+
Answer:
|
283 |
+
""",
|
284 |
+
input_variables=["context", "question"],
|
285 |
+
)
|
286 |
response = llm(prompt.format(context=context, question=msg, images=img_desc))
|
287 |
|
288 |
+
print("Images and Locations:", images_and_locs) # Debug output
|
|
|
|
|
289 |
|
290 |
return history + [(msg, response)], results, images_and_locs
|
291 |
|
292 |
|
293 |
|
|
|
294 |
def check_validity_and_llm(session_states):
|
295 |
if session_states.get("processed", False) == True:
|
296 |
return gr.Tabs(selected=2)
|