Update app.py
Browse files19 unmodified lines202122232425262728293031323367 unmodified lines10110210310410510610710810911011111211311411511611711811912012112212312412571 unmodified lines1971981992002012022032042052061 unmodified line2082092102112122132142152162172182192202212222232242252262272283 unmodified lines2322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922 unmodified lines2952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323331 unmodified line3353363373383393403413423433443453463473483493503517 unmodified lines35936036136236336436536636725 unmodified lines39339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845925 unmodified lines4854864874884894904916 unmodified lines4984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305312 unmodified lines53453553653753853919 unmodified linesfrom langchain_core.documents import Documentfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similarityfrom datetime import datetimefrom huggingface_hub.utils import HfHubHTTPErrorhuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")# Memory database to store question-answer pairsmemory_database = {}conversation_history = []def load_and_split_document_basic(file):"""Loads and splits the document into pages."""67 unmodified lineshuggingfacehub_api_token=huggingface_token)def generate_chunked_response(model, prompt, max_tokens=200):full_response = ""total_length = len(prompt.split()) # Approximate token count of promptwhile total_length < 7800: # Leave some margintry:chunk = model(prompt + full_response, max_new_tokens=min(200, 7800 - total_length))chunk = chunk.strip()if not chunk:breakfull_response += chunktotal_length += len(chunk.split()) # Approximate token countif chunk.endswith((".", "!", "?")):breakexcept Exception as e:print(f"Error generating response: {str(e)}")breakreturn full_response.strip()def manage_conversation_history(question, answer, history, max_history=5):71 unmodified linesprint(f"Found {len(result_block)} results on this page")for result in result_block:link = result.find("a", href=True)title = result.find("h3")if link and title:link = link["href"]title = title.get_text()print(f"Processing link: {link}")try:webpage = session.get(link, headers=headers, timeout=timeout)1 unmodified linevisible_text = extract_text_from_webpage(webpage.text)if len(visible_text) > max_chars_per_page:visible_text = visible_text[:max_chars_per_page] + "..."all_results.append({"link": link, "title": title, "text": visible_text})print(f"Successfully extracted text from {link}")except requests.exceptions.RequestException as e:print(f"Error retrieving webpage content: {e}")all_results.append({"link": link, "title": title, "text": None})else:print("No link or title found for this result")all_results.append({"link": None, "title": None, "text": None})start += len(result_block)print(f"Search completed. Total results: {len(all_results)}")print("Search results:")for i, result in enumerate(all_results, 1):print(f"Result {i}:")print(f" Title: {result['title']}")print(f" Link: {result['link']}")if result['text']:print(f" Text: {result['text'][:100]}...") # Print first 100 characters3 unmodified linesif not all_results:print("No search results found. Returning a default message.")return [{"link": None, "title": "No Results", "text": "No information found in the web search results."}]return all_resultsdef summarize_content(content, model):if content is None:return "No content available to summarize."summary_prompt = f"""You are a financial analyst and given a task to summarize the following news article in concise and coherent brief paragraph. Focus on the key points, main events, significant details and any point that could have major implications. Ensure the summary is informative and relevant to current news:{content[:3000]} # Limit input to avoid token limitsSummary:"""summary = generate_chunked_response(model, summary_prompt, max_tokens=300) # Adjust max_tokens as neededreturn summarydef rank_search_results(titles, summaries, model):if not titles or not summaries:print("No titles or summaries to rank.")return list(range(1, len(titles) + 1))ranking_prompt = ("Rank the following search results from a financial analyst perspective. "f"Assign a rank from 1 to {len(titles)} based on relevance, with 1 being the most relevant. ""Return only the numeric ranks in order, separated by commas.\n\n""Titles and summaries:\n")for i, (title, summary) in enumerate(zip(titles, summaries), 1):ranking_prompt += f"{i}. Title: {title}\nSummary: {summary}\n\n"ranking_prompt += "Ranks:"try: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 ranksexcept Exception as e:print(f"Error in ranking: {str(e)}. Using fallback ranking method.")return list(range(1, len(titles) + 1))def ask_question(question, temperature, top_p, repetition_penalty, web_search):global conversation_historyif not question:2 unmodified linesmodel = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()if web_search:search_results = google_search(question)processed_results = []for index, result in enumerate(search_results, start=1):if result["text"] is not None:try:summary = summarize_content(result["text"], model)processed_results.append({"title": result.get("title", f"Result {index}"),"summary": summary,"index": index})except Exception as e:print(f"Error processing search result {index}: {str(e)}")else:print(f"Skipping result {index} due to None content")if not processed_results:return "No valid search results found."print(f"Number of processed results: {len(processed_results)}")# For news requests, return the summaries directlyif "news" in question.lower():news_response = "Here are the latest news summaries on this topic:\n\n"for result in processed_results[:5]: # Limit to top 5 resultsnews_response += f"Title: {result['title']}\n\nSummary: {result['summary']}\n\n---\n\n"return news_response.strip()# For other questions, use the summaries as contextcontext_str = "\n\n".join([f"Title: {r['title']}\nSummary: {r['summary']}"for r in processed_results])prompt_template = """Answer the question based on the following web search results:1 unmodified line{context}Current Question: {question}If the web search results don't contain relevant information, state that the information is not available in the search results.Provide a concise and direct answer to the question:"""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)answer = generate_chunked_response(model, formatted_prompt)else:if not os.path.exists("faiss_database"):return "No documents available. Please upload documents or enable web search to answer questions."database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])7 unmodified linesprompt_val = ChatPromptTemplate.from_template(prompt)formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)answer = generate_chunked_response(model, formatted_prompt)if not web_search:memory_database[question] = answerconversation_history = manage_conversation_history(question, answer, conversation_history)25 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()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)documents = database.docstore._dict.values()data = [{"Search Date": doc.metadata["search_date"],"Search Title": doc.metadata["search_title"],"Search Content": doc.metadata["search_content"],"Search Summary": doc.metadata["search_summary"],"Rank": doc.metadata["rank"]} for doc in documents]df = pd.DataFrame(data)with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:excel_path = tmp.namedf.to_excel(excel_path, index=False)return excel_pathdef extract_db_to_excel():embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)25 unmodified lines# Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# Chat with your PDF documents")with gr.Row():file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])6 unmodified lineswith gr.Row():with gr.Column(scale=2):chatbot = gr.Chatbot(label="Conversation")question_input = gr.Textbox(label="Ask a question about your documents")submit_button = gr.Button("Submit")with gr.Column(scale=1):temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.5, step=0.1)top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False)def chat(question, history, temperature, top_p, repetition_penalty, web_search):answer = ask_question(question, temperature, top_p, repetition_penalty, web_search)if "news" in question.lower():# Split the answer into individual news itemsnews_items = answer.split("---")for item in news_items:if item.strip():history.append((question, item.strip()))else:history.append((question, answer))return "", historysubmit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox], outputs=[question_input, chatbot])export_vector_db_button = gr.Button("Export Vector DB to Excel")vector_db_excel_output = gr.File(label="Download Vector DB Excel File")export_vector_db_button.click(export_vector_db_to_excel, inputs=[], outputs=vector_db_excel_output)extract_button = gr.Button("Extract Database to Excel")excel_output = gr.File(label="Download Excel File")extract_button.click(extract_db_to_excel, inputs=[], outputs=excel_output)2 unmodified linesmemory_excel_output = gr.File(label="Download Memory Excel File")export_memory_button.click(export_memory_db_to_excel, inputs=[], outputs=memory_excel_output)clear_button = gr.Button("Clear Cache")clear_output = gr.Textbox(label="Cache Status")clear_button.click(clear_cache, inputs=[], outputs=clear_output)19 unmodified lines2021222324252627282930313267 unmodified lines10010110210310410510610710810911011111211311471 unmodified lines1861871881891901911921931 unmodified line1951961971981992002012022032042052062072082092102112122132143 unmodified lines2182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083092 unmodified lines3123133143153163173183193203213223233243253263273283293303313323333343351 unmodified line3373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663677 unmodified lines37537637737837938038138238338438538638738838939039139239339439539639739839940025 unmodified lines42642742842943043125 unmodified lines4574584594604614624636 unmodified lines4704714724734744754764774784794804814824834844854864874884894904914924934944954964974984992 unmodified lines50250350450550650750850951051119 unmodified linesfrom langchain_core.documents import Documentfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similarityhuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")# Memory database to store question-answer pairsmemory_database = {}conversation_history = []news_database = []def load_and_split_document_basic(file):"""Loads and splits the document into pages."""67 unmodified lineshuggingfacehub_api_token=huggingface_token)def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):full_response = ""for i in range(max_chunks):chunk = model(prompt + full_response, max_new_tokens=max_tokens)chunk = chunk.strip()if chunk.endswith((".", "!", "?")):full_response += chunkbreakfull_response += chunkreturn full_response.strip()def manage_conversation_history(question, answer, history, max_history=5):71 unmodified linesprint(f"Found {len(result_block)} results on this page")for result in result_block:link = result.find("a", href=True)if link:link = link["href"]print(f"Processing link: {link}")try:webpage = session.get(link, headers=headers, timeout=timeout)1 unmodified linevisible_text = extract_text_from_webpage(webpage.text)if len(visible_text) > max_chars_per_page:visible_text = visible_text[:max_chars_per_page] + "..."all_results.append({"link": link, "text": visible_text})print(f"Successfully extracted text from {link}")except requests.exceptions.RequestException as e:print(f"Error retrieving webpage content: {e}")all_results.append({"link": link, "text": None})else:print("No link found for this result")all_results.append({"link": None, "text": None})start += len(result_block)print(f"Search completed. Total results: {len(all_results)}")print("Search results:")for i, result in enumerate(all_results, 1):print(f"Result {i}:")print(f" Link: {result['link']}")if result['text']:print(f" Text: {result['text'][:100]}...") # Print first 100 characters3 unmodified linesif not all_results:print("No search results found. Returning a default message.")return [{"link": None, "text": "No information found in the web search results."}]return all_resultsdef fetch_google_news_rss(query, num_results=10):base_url = "https://news.google.com/rss/search"params = {"q": query,"hl": "en-US","gl": "US","ceid": "US:en"}url = f"{base_url}?{urllib.parse.urlencode(params)}"feed = feedparser.parse(url)articles = []for entry in feed.entries[:num_results]:article = {"published_date": entry.published,"title": entry.title,"url": entry.link,"content": entry.summary}articles.append(article)return articlesdef summarize_news_content(content, model):prompt_template = """Summarize the following news article in a concise manner:{content}Summary:"""prompt = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt.format(content=content)summary = generate_chunked_response(model, formatted_prompt, max_tokens=200)return summarydef process_google_news_rss(query, temperature, top_p, repetition_penalty):model = get_model(temperature, top_p, repetition_penalty)embed = get_embeddings()articles = fetch_google_news_rss(query)processed_articles = []for article in articles:summary = summarize_news_content(article["content"], model)processed_article = {"published_date": article["published_date"],"title": article["title"],"url": article["url"],"content": article["content"],"summary": summary}processed_articles.append(processed_article)# Add processed articles to the databasedocs = [Document(page_content=article["summary"], metadata={"url": article["url"], "title": article["title"], "published_date": article["published_date"]}) for article in processed_articles]if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)database.add_documents(docs)else:database = FAISS.from_documents(docs, embed)database.save_local("faiss_database")# Update news_database for excel exportglobal news_databasenews_database.extend(processed_articles)return f"Processed and added {len(processed_articles)} news articles to the database."def export_news_to_excel():global news_databasedf = pd.DataFrame(news_database)with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:excel_path = tmp.namedf.to_excel(excel_path, index=False)return excel_pathdef ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss):global conversation_historyif not question:2 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)web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:database = FAISS.from_documents(web_docs, embed)else:database.add_documents(web_docs)database.save_local("faiss_database")context_str = "\n".join([doc.page_content for doc in web_docs])prompt_template = """Answer the question based on the following web search results:1 unmodified line{context}Current Question: {question}If the web search results don't contain relevant information, state that the information is not available in the search results.Provide a concise and direct answer to the question without mentioning the web search or these instructions:"""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)elif google_news_rss:if database is None:return "No news articles available. Please fetch news articles first."retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(question)context_str = "\n".join([f"Title: {doc.metadata['title']}\nURL: {doc.metadata['url']}\nSummary: {doc.page_content}" for doc in relevant_docs])prompt_template = """Answer the question based on the following news summaries:News Summaries:{context}Current Question: {question}If the news summaries don't contain relevant information, state that the information is not available in the news articles.Provide a concise and direct answer to the question without mentioning the news summaries or these instructions:"""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)else:if database is None:return "No documents available. Please upload documents, enable web search, or fetch news articles to answer questions."history_str = "\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in conversation_history])7 unmodified linesprompt_val = ChatPromptTemplate.from_template(prompt)formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)full_response = generate_chunked_response(model, formatted_prompt)# Extract only the part after the last occurrence of a prompt-like sentenceanswer_patterns = [r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",r"Provide a concise and direct answer to the question without mentioning the news summaries or these instructions:",r"Provide a concise and direct answer to the question:",r"Answer:"]for pattern in answer_patterns:match = re.split(pattern, full_response, flags=re.IGNORECASE)if len(match) > 1:answer = match[-1].strip()breakelse:# If no pattern is found, return the full responseanswer = full_response.strip()if not web_search and not google_news_rss:memory_database[question] = answerconversation_history = manage_conversation_history(question, answer, conversation_history)25 unmodified linesreturn f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files."def extract_db_to_excel():embed = get_embeddings()database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)25 unmodified lines# Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# Chat with your PDF documents and News")with gr.Row():file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])6 unmodified lineswith gr.Row():with gr.Column(scale=2):chatbot = gr.Chatbot(label="Conversation")question_input = gr.Textbox(label="Ask a question about your documents or news")submit_button = gr.Button("Submit")with gr.Column(scale=1):temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.5, step=0.1)top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False)google_news_rss_checkbox = gr.Checkbox(label="Google News RSS", value=False)with gr.Row():news_query_input = gr.Textbox(label="Enter news query")fetch_news_button = gr.Button("Fetch News")news_fetch_output = gr.Textbox(label="News Fetch Status")def chat(question, history, temperature, top_p, repetition_penalty, web_search, google_news_rss):answer = ask_question(question, temperature, top_p, repetition_penalty, web_search, google_news_rss)history.append((question, answer))return "", historysubmit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox, google_news_rss_checkbox], outputs=[question_input, chatbot])fetch_news_button.click(process_google_news_rss, inputs=[news_query_input, temperature_slider, top_p_slider, repetition_penalty_slider], outputs=news_fetch_output)extract_button = gr.Button("Extract Database to Excel")excel_output = gr.File(label="Download Excel File")extract_button.click(extract_db_to_excel, inputs=[], outputs=excel_output)2 unmodified linesmemory_excel_output = gr.File(label="Download Memory Excel File")export_memory_button.click(export_memory_db_to_excel, inputs=[], outputs=memory_excel_output)export_news_button = gr.Button("Download News Excel File")news_excel_output = gr.File(label="Download News Excel File")export_news_button.click(export_news_to_excel, inputs=[], outputs=news_excel_output)clear_button = gr.Button("Clear Cache")clear_output = gr.Textbox(label="Cache Status")clear_button.click(clear_cache, inputs=[], outputs=clear_output)
app.py+160-188