Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,4 @@
|
|
| 1 |
|
| 2 |
-
# import gradio as gr
|
| 3 |
-
# import fitz # PyMuPDF
|
| 4 |
-
# from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
-
# from langchain_community.vectorstores import FAISS
|
| 6 |
-
# from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
-
# import os
|
| 8 |
-
|
| 9 |
-
# # --- Backend Logic ---
|
| 10 |
-
|
| 11 |
-
# class VectorSystem:
|
| 12 |
-
# def __init__(self):
|
| 13 |
-
# self.vector_store = None
|
| 14 |
-
# self.embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 15 |
-
|
| 16 |
-
# def process_file(self, file_obj):
|
| 17 |
-
# """Extracts text from PDF OR TXT and builds the Vector Index"""
|
| 18 |
-
# if file_obj is None:
|
| 19 |
-
# return "No file uploaded."
|
| 20 |
-
|
| 21 |
-
# try:
|
| 22 |
-
# text = ""
|
| 23 |
-
# file_path = file_obj.name
|
| 24 |
-
|
| 25 |
-
# # --- LOGIC BRANCH: Detect File Type ---
|
| 26 |
-
# if file_path.lower().endswith('.pdf'):
|
| 27 |
-
# # Handle PDF
|
| 28 |
-
# doc = fitz.open(file_path)
|
| 29 |
-
# for page in doc:
|
| 30 |
-
# text += page.get_text()
|
| 31 |
-
# elif file_path.lower().endswith('.txt'):
|
| 32 |
-
# # Handle Text File
|
| 33 |
-
# with open(file_path, 'r', encoding='utf-8') as f:
|
| 34 |
-
# text = f.read()
|
| 35 |
-
# else:
|
| 36 |
-
# return "❌ Error: Only .pdf and .txt files are supported."
|
| 37 |
-
# # --------------------------------------
|
| 38 |
-
|
| 39 |
-
# # 2. Split Text into Chunks (Logic is identical for both)
|
| 40 |
-
# text_splitter = RecursiveCharacterTextSplitter(
|
| 41 |
-
# chunk_size=800,
|
| 42 |
-
# chunk_overlap=150,
|
| 43 |
-
# separators=["\n\n", "\n", ".", " ", ""]
|
| 44 |
-
# )
|
| 45 |
-
# chunks = text_splitter.split_text(text)
|
| 46 |
-
|
| 47 |
-
# if not chunks:
|
| 48 |
-
# return "Could not extract text. Is the file empty?"
|
| 49 |
-
|
| 50 |
-
# # 3. Build Vector Index (FAISS)
|
| 51 |
-
# self.vector_store = FAISS.from_texts(chunks, self.embeddings)
|
| 52 |
-
|
| 53 |
-
# return f"✅ Success! Indexed {len(chunks)} text chunks."
|
| 54 |
-
|
| 55 |
-
# except Exception as e:
|
| 56 |
-
# return f"Error processing file: {str(e)}"
|
| 57 |
-
|
| 58 |
-
# def retrieve_evidence(self, question, student_answer):
|
| 59 |
-
# if not self.vector_store:
|
| 60 |
-
# return "⚠️ Please upload and process a file first."
|
| 61 |
-
|
| 62 |
-
# if not question:
|
| 63 |
-
# return "⚠️ Please enter a Question."
|
| 64 |
-
|
| 65 |
-
# docs = self.vector_store.similarity_search(question, k=3)
|
| 66 |
-
|
| 67 |
-
# output_text = "### 🔍 Relevant Context Found:\n\n"
|
| 68 |
-
# for i, doc in enumerate(docs):
|
| 69 |
-
# output_text += f"**Chunk {i+1}:**\n> {doc.page_content}\n\n"
|
| 70 |
-
|
| 71 |
-
# output_text += "---\n*These are the most relevant segments to grade the answer against.*"
|
| 72 |
-
# return output_text
|
| 73 |
-
|
| 74 |
-
# # Initialize System
|
| 75 |
-
# system = VectorSystem()
|
| 76 |
-
|
| 77 |
-
# # --- Gradio UI ---
|
| 78 |
-
|
| 79 |
-
# with gr.Blocks(title="EduGenius Context Retriever") as demo:
|
| 80 |
-
# gr.Markdown("# 🎓 EduGenius: Context Retriever")
|
| 81 |
-
# gr.Markdown("Upload a Chapter (PDF or TXT), ask a question, and see exactly which part of the text proves the answer right or wrong.")
|
| 82 |
-
|
| 83 |
-
# with gr.Row():
|
| 84 |
-
# with gr.Column(scale=1):
|
| 85 |
-
# # UPDATED: Added ".txt" to file_types and changed label
|
| 86 |
-
# pdf_input = gr.File(label="1. Upload File (PDF or TXT)", file_types=[".pdf", ".txt"])
|
| 87 |
-
# upload_btn = gr.Button("Process File", variant="primary")
|
| 88 |
-
# upload_status = gr.Textbox(label="Status", interactive=False)
|
| 89 |
-
|
| 90 |
-
# with gr.Column(scale=2):
|
| 91 |
-
# question_input = gr.Textbox(label="2. Question", placeholder="e.g., What causes the chemical reaction?")
|
| 92 |
-
# answer_input = gr.Textbox(label="Student Answer (Optional Context)", placeholder="e.g., The heat causes it...")
|
| 93 |
-
# search_btn = gr.Button("Find Relevant Evidence", variant="secondary")
|
| 94 |
-
|
| 95 |
-
# evidence_output = gr.Markdown(label="Relevant Text Chunks")
|
| 96 |
-
|
| 97 |
-
# # Event Handlers
|
| 98 |
-
# upload_btn.click(
|
| 99 |
-
# fn=system.process_file, # Note: Function name changed
|
| 100 |
-
# inputs=[pdf_input],
|
| 101 |
-
# outputs=[upload_status]
|
| 102 |
-
# )
|
| 103 |
-
|
| 104 |
-
# search_btn.click(
|
| 105 |
-
# fn=system.retrieve_evidence,
|
| 106 |
-
# inputs=[question_input, answer_input],
|
| 107 |
-
# outputs=[evidence_output]
|
| 108 |
-
# )
|
| 109 |
-
|
| 110 |
-
# if __name__ == "__main__":
|
| 111 |
-
# demo.launch()
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
|
| 127 |
import gradio as gr
|
| 128 |
import fitz # PyMuPDF
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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