Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -109,26 +109,34 @@ def answer_with_generation(index, embeddings, chunks, question):
|
|
109 |
return answer.strip()
|
110 |
|
111 |
# ✅ API route
|
|
|
|
|
|
|
|
|
112 |
@app.route('/ask', methods=['POST'])
|
113 |
def ask():
|
114 |
file = request.files.get("pdf")
|
115 |
question = request.form.get("question", "")
|
116 |
|
117 |
if not file or not question:
|
118 |
-
return jsonify({"error": "PDF and question required"}), 400
|
119 |
|
120 |
filename = secure_filename(file.filename)
|
121 |
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
122 |
file.save(filepath)
|
123 |
|
124 |
try:
|
|
|
125 |
text = extract_text(filepath)
|
126 |
chunks = split_into_chunks(text)
|
127 |
answer = answer_with_qa_pipeline(chunks, question)
|
|
|
128 |
if len(answer.strip()) < 20:
|
129 |
index, embeddings, chunks = setup_faiss(chunks)
|
130 |
answer = answer_with_generation(index, embeddings, chunks, question)
|
|
|
131 |
return jsonify({"answer": answer})
|
|
|
132 |
except Exception as e:
|
133 |
return jsonify({"error": str(e)}), 500
|
134 |
|
|
|
109 |
return answer.strip()
|
110 |
|
111 |
# ✅ API route
|
112 |
+
@app.route('/')
|
113 |
+
def home():
|
114 |
+
return jsonify({"message": "PDF QA API is running!"})
|
115 |
+
|
116 |
@app.route('/ask', methods=['POST'])
|
117 |
def ask():
|
118 |
file = request.files.get("pdf")
|
119 |
question = request.form.get("question", "")
|
120 |
|
121 |
if not file or not question:
|
122 |
+
return jsonify({"error": "Both PDF file and question are required"}), 400
|
123 |
|
124 |
filename = secure_filename(file.filename)
|
125 |
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
126 |
file.save(filepath)
|
127 |
|
128 |
try:
|
129 |
+
# 🧠 Process PDF and generate answer
|
130 |
text = extract_text(filepath)
|
131 |
chunks = split_into_chunks(text)
|
132 |
answer = answer_with_qa_pipeline(chunks, question)
|
133 |
+
|
134 |
if len(answer.strip()) < 20:
|
135 |
index, embeddings, chunks = setup_faiss(chunks)
|
136 |
answer = answer_with_generation(index, embeddings, chunks, question)
|
137 |
+
|
138 |
return jsonify({"answer": answer})
|
139 |
+
|
140 |
except Exception as e:
|
141 |
return jsonify({"error": str(e)}), 500
|
142 |
|