Update app.py
Browse files242 unmodified lines24324424524624724824925025125225325425525625725825926026127 unmodified lines289290291292293294295296297298299300301302303304305161 unmodified lines46746846947047147210 unmodified lines4834844854864874881 unmodified line49049149249349449549649749849922 unmodified lines522523524525526527528529530531532242 unmodified linesreturn chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")logging.info(f"Selected Documents: {selected_docs}")try:if use_web_search:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''# logging.info(f"Generated Response (first line): {first_line}")yield responseelse:embed = get_embeddings()27 unmodified linesfirst_line = partial_response.split('\n')[0] if partial_response else ''logging.info(f"Generated Response (first line): {first_line}")yield partial_responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")if "microsoft/Phi-3-mini-4k-instruct" in model:logging.info("Falling back to Mistral model due to Phi-3 error")fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search, selected_docs)else:yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."logging.basicConfig(level=logging.DEBUG)def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):headers = {"Authorization": f"Bearer {API_TOKEN}",161 unmodified lines}"""uploaded_documents = []def display_documents():10 unmodified linescustom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"demo = gr.ChatInterface(respond,additional_inputs=[1 unmodified linegr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),use_web_search,document_selector],title="AI-powered Web Search and PDF Chat Assistant",description="Chat with your PDFs or use web search to answer questions. Toggle between Web Search and PDF Chat in Additional Inputs below.",theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",22 unmodified linesanalytics_enabled=False,textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),chatbot = gr.Chatbot(show_copy_button=True,likeable=True,layout="bubble",height=400,))# Add file upload functionality242 unmodified lines24324424524624724824925025125225325425525625725825926026126226326426526626726827 unmodified lines296297298299300301302303304305306307308309310311312313314315316317318319320161 unmodified lines48248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352410 unmodified lines5355365375385395405415421 unmodified line54454554654754854955055155255355422 unmodified lines577578579580581582583584585586587242 unmodified linesreturn chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs, instruction_key):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")logging.info(f"Selected Documents: {selected_docs}")logging.info(f"Instruction Key: {instruction_key}")try:if instruction_key and instruction_key != "None":# This is a summary generation requestinstruction = INSTRUCTION_PROMPTS[instruction_key]context_str = get_context_for_summary(selected_docs)message = f"{instruction}\n\nUsing the following context from the PDF documents:\n{context_str}\nGenerate a detailed summary."use_web_search = False # Ensure we use PDF search for summariesif use_web_search:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''logging.info(f"Generated Response (first line): {first_line}")yield responseelse:embed = get_embeddings()27 unmodified linesfirst_line = partial_response.split('\n')[0] if partial_response else ''logging.info(f"Generated Response (first line): {first_line}")yield partial_responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")if "microsoft/Phi-3-mini-4k-instruct" in model:logging.info("Falling back to Mistral model due to Phi-3 error")fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search, selected_docs, instruction_key)else:yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."logging.basicConfig(level=logging.DEBUG)INSTRUCTION_PROMPTS = {"Asset Managers": "Summarize the key financial metrics, assets under management, and performance highlights for this asset management company.","Consumer Finance Companies": "Provide a summary of the company's loan portfolio, interest income, credit quality, and key operational metrics.","Mortgage REITs": "Summarize the REIT's mortgage-backed securities portfolio, net interest income, book value per share, and dividend yield.",# Add more instruction prompts as needed}def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):headers = {"Authorization": f"Bearer {API_TOKEN}",161 unmodified lines}"""def get_context_for_summary(selected_docs):embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = database.as_retriever(search_kwargs={"k": 5}) # Retrieve top 5 most relevant chunks# Create a generic query that covers common financial summary topicsgeneric_query = "financial performance revenue profit assets liabilities cash flow key metrics highlights"relevant_docs = retriever.get_relevant_documents(generic_query)filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]if not filtered_docs:return "No relevant information found in the selected documents for summary generation."context_str = "\n".join([doc.page_content for doc in filtered_docs])return context_strelse:return "No documents available for summary generation."def get_context_for_query(query, selected_docs):embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = database.as_retriever(search_kwargs={"k": 3}) # Retrieve top 3 most relevant chunksrelevant_docs = retriever.get_relevant_documents(query)filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]if not filtered_docs:return "No relevant information found in the selected documents for the given query."context_str = "\n".join([doc.page_content for doc in filtered_docs])return context_strelse:return "No documents available to answer the query."uploaded_documents = []def display_documents():10 unmodified linescustom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"instruction_choices = ["None"] + list(INSTRUCTION_PROMPTS.keys())demo = gr.ChatInterface(respond,additional_inputs=[1 unmodified linegr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),use_web_search,document_selector,gr.Dropdown(choices=instruction_choices, label="Select Entity Type for Summary", value="None")],title="AI-powered Web Search and PDF Chat Assistant",description="Chat with your PDFs, use web search to answer questions, or generate summaries. Select an Entity Type for Summary to generate a specific summary.",theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",22 unmodified linesanalytics_enabled=False,textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),chatbot = gr.Chatbot(show_copy_button=True,likeable=True,layout="bubble",height=400,))# Add file upload functionality
app.py+66-11