Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines456789781213149101118192012131424151617182919203233342122233839402425262728294730315051523233341 unmodified line363738606162394065414268434445727374464777487949505152838453865455565758915960939495616263646510110210310466676869108109110701127172114737411675761181191207778798081828384126127128853 unmodified linesimport faissimport numpy as np
# -------------------------------# Load embedding model# -------------------------------embed_model = SentenceTransformer('all-MiniLM-L6-v2')
# -------------------------------# Global variables# -------------------------------index = Nonechunks = None
# -------------------------------# Load PDF# -------------------------------def load_pdf(file): reader = PdfReader(file) text = ""
for page in reader.pages: page_text = page.extract_text() if page_text: text += page_text
return text
# -------------------------------# Split text# -------------------------------def split_text(text, chunk_size=500): return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
# -------------------------------# Create vector store# -------------------------------def create_vector_store(chunks): embeddings = embed_model.encode(chunks) embeddings = np.array(embeddings).astype("float32")
index = faiss.IndexFlatL2(embeddings.shape[1]) index.add(embeddings)
return index
# -------------------------------# Retrieve best chunk# -------------------------------def retrieve(query, index, chunks): query_vec = embed_model.encode([query]) query_vec = np.array(query_vec).astype("float32")1 unmodified line D, I = index.search(query_vec, 1) return chunks[I[0][0]]
# -------------------------------# Answer function# -------------------------------def answer_question(query): global index, chunks
if index is None: return "β Upload and process PDF first."
result = retrieve(query, index, chunks) return result[:500]
# -------------------------------# Process PDF# -------------------------------def process_pdf(file): global index, chunks
if file is None: return "β Please upload a PDF." return "β Upload PDF"
text = load_pdf(file)
print("Text length:", len(text))
if len(text) == 0: return "β PDF has no readable text (scanned)." return "β PDF has no readable text"
chunks = split_text(text) index = create_vector_store(chunks)
return "β
PDF processed successfully!" return "β
PDF processed!"
# -------------------------------# Chat function (FIXED)# -------------------------------def respond(message, history): if history is None: history = []
reply = answer_question(message)
# β
Correct format (dictionary) history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": reply}) history.append((message, reply)) # β
correct
return "", history
# -------------------------------# UI (FIXED)# -------------------------------with gr.Blocks() as app: gr.Markdown("# π Course Material Chatbot") gr.Markdown("# π Course Chatbot")
file_input = gr.File(label="Upload PDF") file_input = gr.File() btn = gr.Button("Process PDF") status = gr.Textbox(label="Status") status = gr.Textbox()
chatbot = gr.Chatbot(type="messages") # β
IMPORTANT msg = gr.Textbox(placeholder="Ask a question...") clear = gr.Button("Clear Chat") chatbot = gr.Chatbot() # β
correct for your version msg = gr.Textbox() clear = gr.Button("Clear")
btn.click(process_pdf, inputs=file_input, outputs=status) msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) clear.click(lambda: [], None, chatbot)
# -------------------------------# Launch# -------------------------------app.launch(server_name="0.0.0.0", server_port=7860)app.py+10-54