Update app.py
Browse files272 unmodified lines27327427527627727827928028128228328410 unmodified lines29529629729829930030130230330430530616 unmodified lines3233243253263273283 unmodified lines33233333433533633778 unmodified lines416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447272 unmodified linestry:ranks_str = generate_chunked_response(model, ranking_prompt)ranks = [float(rank.strip()) for rank in ranks_str.split(',') if rank.strip()]# Check if we have the correct number of ranksif len(ranks) != len(titles):print(f"Warning: Number of ranks ({len(ranks)}) does not match number of titles ({len(titles)})")print(f"Model output: {ranks_str}")return list(range(1, len(titles) + 1))return ranks10 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()# Check if the FAISS database existsif os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)else:database = Noneif web_search:search_results = google_search(question)16 unmodified linesif not processed_results:return "No valid search results found."# Rank the resultstitles = [r["title"] for r in processed_results]summaries = [r["summary"] for r in processed_results]3 unmodified linesprint(f"Error in ranking results: {str(e)}. Using default ranking.")ranks = list(range(1, len(processed_results) + 1))# Update Vector DBcurrent_date = datetime.now().strftime("%Y-%m-%d")update_vector_db_with_search_results(processed_results, ranks, current_date)78 unmodified linesreturn f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files."def update_vector_db_with_search_results(search_results, summaries, ranks):embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True) if os.path.exists("faiss_database") else FAISS.from_documents([], embed)current_date = datetime.now().strftime("%Y-%m-%d")documents = []for result, summary, rank in zip(search_results, summaries, ranks):if summary: # Only create a document if there's a summarydoc = Document(page_content=summary,metadata={"search_date": current_date,"search_title": result["title"],"search_content": result["text"],"search_summary": summary,"rank": rank})documents.append(doc)if documents: # Only update the database if there are documents to adddatabase.add_documents(documents)database.save_local("faiss_database")else:print("No valid documents to add to the database.")def export_vector_db_to_excel():embed = get_embeddings()272 unmodified lines27327427527627727827928028128228328428528628728810 unmodified lines29930030130230330416 unmodified lines3213223233243253263273283 unmodified lines33233333433533633733833978 unmodified lines418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462272 unmodified linestry:ranks_str = generate_chunked_response(model, ranking_prompt)print(f"Model output for ranking: {ranks_str}")if not ranks_str.strip():print("Model returned an empty string for ranking.")return list(range(1, len(titles) + 1))ranks = [float(rank.strip()) for rank in ranks_str.split(',') if rank.strip()]if len(ranks) != len(titles):print(f"Warning: Number of ranks ({len(ranks)}) does not match number of titles ({len(titles)})")return list(range(1, len(titles) + 1))return ranks10 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()if web_search:search_results = google_search(question)16 unmodified linesif not processed_results:return "No valid search results found."print(f"Number of processed results: {len(processed_results)}")# Rank the resultstitles = [r["title"] for r in processed_results]summaries = [r["summary"] for r in processed_results]3 unmodified linesprint(f"Error in ranking results: {str(e)}. Using default ranking.")ranks = list(range(1, len(processed_results) + 1))print(f"Number of ranks: {len(ranks)}")# Update Vector DBcurrent_date = datetime.now().strftime("%Y-%m-%d")update_vector_db_with_search_results(processed_results, ranks, current_date)78 unmodified linesreturn f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files."def update_vector_db_with_search_results(search_results, ranks, current_date):embed = get_embeddings()documents = []for result, rank in zip(search_results, ranks):if result.get("summary"):doc = Document(page_content=result["summary"],metadata={"search_date": current_date,"search_title": result.get("title", ""),"search_content": result.get("content", ""),"search_summary": result["summary"],"rank": rank})documents.append(doc)if not documents:print("No valid documents to add to the database.")returntexts = [doc.page_content for doc in documents]metadatas = [doc.metadata for doc in documents]print(f"Number of documents to embed: {len(texts)}")print(f"First document text: {texts[0][:100]}...") # Print first 100 characters of the first documenttry:if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)database.add_texts(texts, metadatas=metadatas)else:database = FAISS.from_texts(texts, embed, metadatas=metadatas)database.save_local("faiss_database")print("Database updated successfully.")except Exception as e:print(f"Error updating database: {str(e)}")def export_vector_db_to_excel():embed = get_embeddings()
app.py+37-22