Spaces:
Sleeping
Sleeping
File size: 4,226 Bytes
b878868 98a327c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 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) |