Update app.py
Browse files470 unmodified lines47147247347447547623 unmodified lines50050150250350450550641 unmodified lines5485495505515525537 unmodified lines561562563564565566470 unmodified lineslogging.info("Finished generating response for Excel data")# Modify the existing respond function to handle both PDF and web searchdef respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):logging.info(f"User Query: {message}")23 unmodified linesyield final_summaryelse:yield "Unable to generate a response. Please try a different query."else:try:embed = get_embeddings()41 unmodified linesif model == "gemini-1.5-flash":for chunk in get_response_from_gemini(message, model, selected_docs, file_type, num_calls, temperature):yield chunkelse:for response in get_response_from_excel(message, model, excel_context, num_calls, temperature):yield response7 unmodified lineselif 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):yield response470 unmodified lines47147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454523 unmodified lines56957057157257357441 unmodified lines6166176186196206216226236247 unmodified lines632633634635636637638639640470 unmodified lineslogging.info("Finished generating response for Excel data")def get_response_from_llama(query, model, selected_docs, file_type, num_calls=1, temperature=0.2):logging.info(f"Getting response from Llama using model: {model}")# Initialize the Hugging Face clientclient = InferenceClient(model, token=huggingface_token)if file_type == "excel":# Excel functionalitysystem_instruction = """You are a highly specialized Python programmer with deep expertise in data analysis and visualization using Excel spreadsheets.Your primary goal is to generate accurate and efficient Python code to perform calculations or create visualizations based on the user's requests.Strictly use the data provided to write code that identifies key metrics, trends, and significant details relevant to the query.Do not make assumptions or include any information that is not explicitly supported by the dataset.If the user requests a calculation, provide the appropriate Python code to execute it, and if a visualization is needed, generate code using the matplotlib library to create the chart."""# Get the context from selected Excel documentsembed = get_embeddings()office_database = FAISS.load_local("office_faiss_database", embed, allow_dangerous_deserialization=True)retriever = office_database.as_retriever(search_kwargs={"k": 10})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs if doc.metadata["source"] in selected_docs])messages = [{"role": "system", "content": system_instruction},{"role": "user", "content": f"Based on the following data extracted from Excel spreadsheets:\n{context}\n\nPlease provide the Python code needed to execute the following task: '{query}'. Ensure that the code is derived directly from the dataset. If a chart is requested, use the matplotlib library to generate the appropriate visualization."}]elif file_type == "pdf":# PDF functionalityembed = get_embeddings()pdf_database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = pdf_database.as_retriever(search_kwargs={"k": 10})relevant_docs = retriever.get_relevant_documents(query)context_str = "\n".join([doc.page_content for doc in relevant_docs if doc.metadata["source"] in selected_docs])system_instruction = """You are a highly specialized financial analyst assistant with expertise in analyzing and summarizing financial documents.Your goal is to provide accurate, detailed, and precise summaries based on the context provided.Avoid making assumptions or adding information that is not explicitly supported by the context from the PDF documents."""messages = [{"role": "system", "content": system_instruction},{"role": "user", "content": f"Using the following context from the PDF documents:\n{context_str}\n\nPlease generate a step-by-step reasoning before arriving at a comprehensive and accurate summary addressing the following question: '{query}'. Ensure your response is strictly based on the provided context, highlighting key metrics, trends, and significant details relevant to the query. Avoid any speculative or unverified information."}]else:raise ValueError("Invalid file type. Use 'excel' or 'pdf'.")full_response = ""for _ in range(num_calls):try:# Generate content with streaming enabledfor message in client.chat_completion(messages=messages,max_new_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.contentfull_response += chunkyield full_response # Yield the accumulated response so farexcept Exception as e:yield f"An error occurred with the Llama model: {str(e)}. Please try again."if not full_response:yield "No response generated from the Llama model."# Modify the existing respond function to handle both PDF and web searchdef respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):logging.info(f"User Query: {message}")23 unmodified linesyield final_summaryelse:yield "Unable to generate a response. Please try a different query."else:try:embed = get_embeddings()41 unmodified linesif model == "gemini-1.5-flash":for chunk in get_response_from_gemini(message, model, selected_docs, file_type, num_calls, temperature):yield chunkelif "llama" in model.lower():for chunk in get_response_from_llama(message, model, selected_docs, file_type, num_calls, temperature):yield chunkelse:for response in get_response_from_excel(message, model, excel_context, num_calls, temperature):yield response7 unmodified lineselif 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 responseelif "llama" in model.lower():for chunk in get_response_from_llama(message, model, selected_docs, file_type, num_calls, temperature):yield chunkelse:for response in get_response_from_pdf(message, model, selected_docs, num_calls, temperature):yield response
app.py+75-1