Update app.py
Browse files351 unmodified lines35235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838928 unmodified lines4184194204214224234248 unmodified lines433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472351 unmodified linesexcept Exception as e:return f"An error occurred during summarization: {str(e)}"def get_response_from_excel(query, model, context, num_calls=3, temperature=0.2):logging.info(f"Getting response from Excel using model: {model}")messages = [{"role": "system", "content": "You are a highly specialized data analyst with expertise in analyzing and summarizing Excel spreadsheets. Your goal is to provide accurate, detailed, and precise information on the data provided. Focus on identifying key metrics, trends, and significant details relevant to the query. Avoid making assumptions or adding information that is not explicitly supported by the data."},{"role": "user", "content": f"Using the following data extracted from Excel spreadsheets:\n{context}\n\nPlease analyze this data and provide a comprehensive answer to the following question: '{query}'. If appropriate, suggest visualizations that could help illustrate the findings."}]if model.startswith("duckduckgo/"):# Use DuckDuckGo chat with contextreturn get_response_from_duckduckgo(query, model, context, num_calls, temperature)elif model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIreturn get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="excel")else:# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)response = ""for i in range(num_calls):logging.info(f"API call {i+1}/{num_calls}")for message in client.chat_completion(messages=messages,max_tokens=20000,temperature=temperature,stream=True,top_p=0.8,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentresponse += chunkyield response # Yield partial responselogging.info("Finished generating response for Excel data")28 unmodified linesyield "Unable to generate a response. Please try a different query."else:try:embed = get_embeddings()pdf_database = Noneoffice_database = None8 unmodified linesyield "No documents available. Please upload documents to answer questions."returnall_relevant_docs = []if pdf_database:pdf_retriever = pdf_database.as_retriever(search_kwargs={"k": 10})all_relevant_docs.extend(pdf_retriever.get_relevant_documents(message))if office_database:office_retriever = office_database.as_retriever(search_kwargs={"k": 10})all_relevant_docs.extend(office_retriever.get_relevant_documents(message))relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]if not relevant_docs:yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."return# Separate Excel documents from othersexcel_docs = [doc for doc in relevant_docs if doc.metadata["source"].lower().endswith(('.xlsx', '.xls'))]other_docs = [doc for doc in relevant_docs if not doc.metadata["source"].lower().endswith(('.xlsx', '.xls'))]excel_context = "\n".join([doc.page_content for doc in excel_docs])other_context = "\n".join([doc.page_content for doc in other_docs])logging.info(f"Excel context length: {len(excel_context)}")logging.info(f"Other context length: {len(other_context)}")# Process Excel documentsif excel_docs:for response in get_response_from_excel(message, model, excel_context, num_calls, temperature):yield response# Process other documents (PDF, Word)if other_docs:if model == "@cf/meta/llama-3.1-8b-instruct":for response in get_response_from_cloudflare(prompt="", context=other_context, query=message, num_calls=num_calls, temperature=temperature, search_type="document"):yield responseelse:for response in get_response_from_pdf(message, model, selected_docs, num_calls, temperature):351 unmodified lines35235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041128 unmodified lines4404414424434444454468 unmodified lines455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488351 unmodified linesexcept Exception as e:return f"An error occurred during summarization: {str(e)}"def get_response_from_excel(query, model, context, num_calls=1, temperature=0.2):logging.info(f"Getting response from Excel using model: {model}")# Use embeddings to find the most relevant Excel dataembed = get_embeddings()office_database = FAISS.load_local("office_faiss_database", embed, allow_dangerous_deserialization=True)retriever = office_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)# Prepare the context from relevant documentsexcel_context = "\n".join([doc.page_content for doc in relevant_docs])# Prepare the messages for the AI modelmessages = [{"role": "system", "content": "You are an AI assistant specialized in analyzing Excel data. Your task is to provide accurate and detailed responses based solely on the given Excel data context. Do not make assumptions or add information beyond what is explicitly provided in the context."},{"role": "user", "content": f"Based on the following Excel data:\n\n{excel_context}\n\nPlease answer this question: {query}\n\nProvide a step-by-step analysis if applicable, and ensure your response is factual and directly related to the provided Excel data."}]full_response = ""if model.startswith("duckduckgo/"):for _ in range(num_calls):try:ddg_model = model.split('/')[-1]results = DDGS().chat(messages[-1]["content"], model=ddg_model)full_response += results + "\n"logging.info(f"DuckDuckGo API response received for Excel query. Length: {len(results)}")except Exception as e:logging.error(f"Error in generating response from DuckDuckGo for Excel: {str(e)}")yield f"An error occurred with the {model} model: {str(e)}. Please try again."returnelif model == "@cf/meta/llama-3.1-8b-instruct":for response in get_response_from_cloudflare(prompt="", context=excel_context, query=query, num_calls=num_calls, temperature=temperature, search_type="excel"):yield responsereturnelse:client = InferenceClient(model, token=huggingface_token)for i in range(num_calls):logging.info(f"API call {i+1}/{num_calls} for Excel query")for message in client.chat_completion(messages=messages,max_tokens=1000,temperature=temperature,stream=True,top_p=0.8,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentfull_response += chunkyield full_responseif not full_response.strip():yield "I couldn't generate a response based on the Excel data. Please try rephrasing your question or check if the relevant data is present in the uploaded Excel files."else:yield full_response.strip()logging.info("Finished generating response for Excel data")28 unmodified linesyield "Unable to generate a response. Please try a different query."else:try:embed = get_embeddings()pdf_database = Noneoffice_database = None8 unmodified linesyield "No documents available. Please upload documents to answer questions."return# Determine if the query is specifically for Excel dataexcel_files = [doc for doc in selected_docs if doc.lower().endswith(('.xlsx', '.xls'))]if excel_files:# If Excel files are selected, use the Excel-specific functionfor response in get_response_from_excel(message, model, "", num_calls, temperature):yield responseelse:# Existing logic for PDF and other document typesall_relevant_docs = []if pdf_database:pdf_retriever = pdf_database.as_retriever(search_kwargs={"k": 10})all_relevant_docs.extend(pdf_retriever.get_relevant_documents(message))if office_database:office_retriever = office_database.as_retriever(search_kwargs={"k": 10})all_relevant_docs.extend(office_retriever.get_relevant_documents(message))relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]if not relevant_docs:yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."returncontext_str = "\n".join([doc.page_content for doc in relevant_docs])if model == "@cf/meta/llama-3.1-8b-instruct":for response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="document"):yield responseelse:for response in get_response_from_pdf(message, model, selected_docs, num_calls, temperature):
app.py+63-47