sairaarif89 commited on
Commit
ad20893
·
verified ·
1 Parent(s): 9d01c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -46,18 +46,17 @@ def process_pdf(file):
46
  )
47
  splits = text_splitter.split_documents(docs)
48
 
49
- # Create FAISS index
50
  texts = [doc.page_content for doc in splits]
51
- embeddings = embedding_model.embed_documents(texts)
52
 
53
- # Convert to numpy array
54
- embeddings_np = np.array(embeddings).astype('float32')
55
 
56
- # Create FAISS index directly
57
- vectorstore = FAISS.from_embeddings(
58
- text_embeddings=list(zip(texts, embeddings)),
59
- embedding=embedding_model,
60
- metadatas=[doc.metadata for doc in splits]
61
  )
62
 
63
  os.unlink(tmp_path)
@@ -73,16 +72,14 @@ def answer_question(question):
73
  try:
74
  docs = vectorstore.similarity_search(question, k=3)
75
  context = "\n\n".join([doc.page_content for doc in docs])
76
-
77
- # Simple QA implementation
78
- answer = f"Relevant content from document:\n{context[:2000]}..."
79
- sources = "\n📄 Sources:\n" + "\n".join([f"- Page {doc.metadata['page']+1}" for doc in docs])
80
- return f"{answer}\n{sources}"
81
  except Exception as e:
82
  return f"❌ Error: {str(e)}"
83
 
84
  with gr.Blocks() as app:
85
- gr.Markdown("# PDF Question Answering System")
86
 
87
  with gr.Row():
88
  file_input = gr.File(label="Upload PDF", type="binary")
@@ -90,10 +87,10 @@ with gr.Blocks() as app:
90
  status = gr.Textbox(label="Status")
91
 
92
  question = gr.Textbox(label="Your Question")
93
- answer = gr.Textbox(label="Answer", interactive=False, lines=5)
94
  ask_btn = gr.Button("Get Answer")
95
 
96
  upload_btn.click(process_pdf, inputs=file_input, outputs=status)
97
  ask_btn.click(answer_question, inputs=question, outputs=answer)
98
 
99
- app.launch()
 
46
  )
47
  splits = text_splitter.split_documents(docs)
48
 
 
49
  texts = [doc.page_content for doc in splits]
50
+ metadatas = [doc.metadata for doc in splits]
51
 
52
+ # Generate embeddings
53
+ embeddings = embedding_model.embed_documents(texts)
54
 
55
+ # Create FAISS index
56
+ vectorstore = FAISS.from_texts(
57
+ texts=texts,
58
+ embedding=embedding_model.embed_query,
59
+ metadatas=metadatas
60
  )
61
 
62
  os.unlink(tmp_path)
 
72
  try:
73
  docs = vectorstore.similarity_search(question, k=3)
74
  context = "\n\n".join([doc.page_content for doc in docs])
75
+ sources = "\n📄 Sources:\n" + "\n".join([f"- Page {doc.metadata.get('page', 'N/A') + 1}" for doc in docs])
76
+ answer = f"Relevant content from document:\n{context[:2000]}...\n{sources}"
77
+ return answer
 
 
78
  except Exception as e:
79
  return f"❌ Error: {str(e)}"
80
 
81
  with gr.Blocks() as app:
82
+ gr.Markdown("# 📄 PDF Question Answering System")
83
 
84
  with gr.Row():
85
  file_input = gr.File(label="Upload PDF", type="binary")
 
87
  status = gr.Textbox(label="Status")
88
 
89
  question = gr.Textbox(label="Your Question")
90
+ answer = gr.Textbox(label="Answer", interactive=False, lines=10)
91
  ask_btn = gr.Button("Get Answer")
92
 
93
  upload_btn.click(process_pdf, inputs=file_input, outputs=status)
94
  ask_btn.click(answer_question, inputs=question, outputs=answer)
95
 
96
+ app.launch()