import os import tempfile import mimetypes from flask import Flask, request, jsonify from dotenv import load_dotenv from langchain_community.document_loaders import PyMuPDFLoader from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_huggingface import HuggingFaceEmbeddings from langchain_groq import ChatGroq from langchain_community.vectorstores import FAISS from langchain_classic.chains import create_retrieval_chain from langchain_classic.chains.combine_documents import create_stuff_documents_chain from langchain_core.prompts import ChatPromptTemplate # --- FORCE MIME TYPES FOR WINDOWS --- mimetypes.add_type('text/css', '.css') mimetypes.add_type('application/javascript', '.jsx') app = Flask(__name__, static_folder='frontend', static_url_path='/') global_vector_store = None # --- LLM --- load_dotenv() groq_api_key = os.getenv("GROQ_API_KEY") embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5") llm = ChatGroq( temperature=0.1, model_name="llama-3.3-70b-versatile", groq_api_key=groq_api_key ) @app.route('/') def serve_index(): return app.send_static_file('index.html') # --- UPLOAD & PROCESS PDF --- @app.route('/upload', methods=['POST']) def upload_file(): global global_vector_store if 'file' not in request.files: return jsonify({"error": "No file part"}), 400 file = request.files['file'] if file.filename == '': return jsonify({"error": "No selected file"}), 400 try: with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file: file.save(tmp_file.name) tmp_file_path = tmp_file.name loader = PyMuPDFLoader(tmp_file_path) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter( separators=["\n\n", "\n", " ", ""], chunk_size=1000, chunk_overlap=200 ) chunks = text_splitter.split_documents(documents) global_vector_store = FAISS.from_documents(chunks, embeddings) intro_text = " ".join([doc.page_content for doc in chunks[:4]]) extraction_prompt = f""" Based on the following introductory text from a research paper, provide: 1. A 2-sentence summary of the paper. 2. The core problem the authors are trying to solve. 3. The primary methodology or model architecture proposed. Text: {intro_text} """ response = llm.invoke([("human", extraction_prompt)]) os.remove(tmp_file_path) return jsonify({"insights": response.content}), 200 except Exception as e: return jsonify({"error": str(e)}), 500 # --- CHAT WITH PDF --- @app.route('/chat', methods=['POST']) def chat(): global global_vector_store if not global_vector_store: return jsonify({"error": "Please upload a document first."}), 400 data = request.json user_message = data.get('message') if not user_message: return jsonify({"error": "No message provided."}), 400 try: retriever = global_vector_store.as_retriever(search_kwargs={"k": 4}) system_prompt = ( "You are a highly technical AI assistant helping a machine learning engineer understand a research paper. " "Use the following pieces of retrieved context to answer the question. " "If the answer is not in the context, state clearly that the paper does not mention it. " "Keep the answer concise and highly technical.\n\n" "Context: {context}" ) prompt = ChatPromptTemplate.from_messages([ ("system", system_prompt), ("human", "{input}"), ]) question_answer_chain = create_stuff_documents_chain(llm, prompt) rag_chain = create_retrieval_chain(retriever, question_answer_chain) response = rag_chain.invoke({"input": user_message}) return jsonify({"answer": response["answer"]}), 200 except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=True)