saravan5757 commited on
Commit
d0bda0e
·
verified ·
1 Parent(s): c286994

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import faiss
3
+ import numpy as np
4
+ from sentence_transformers import SentenceTransformer
5
+ from transformers import pipeline
6
+ from PyPDF2 import PdfReader
7
+
8
+ # Load AI models
9
+ embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") # Converts text to numbers
10
+ llm_pipeline = pipeline("text2text-generation", model="google/flan-t5-small") # AI that answers questions
11
+
12
+ # Memory (Database) to store text
13
+ index = None
14
+ chunks = []
15
+
16
+ # Load and process document
17
+ def load_document(file):
18
+ global index, chunks
19
+ text = ""
20
+
21
+ # Determine file path or object
22
+ # If file is a string, it's a file path
23
+ if isinstance(file, str):
24
+ file_path = file
25
+ else:
26
+ # If file is not a string, try to use its .name attribute
27
+ file_path = file.name
28
+
29
+ # Read PDF or text file
30
+ if file_path.endswith(".pdf"):
31
+ reader = PdfReader(file_path)
32
+ text = "\n".join(
33
+ [page.extract_text() for page in reader.pages if page.extract_text()]
34
+ )
35
+ else:
36
+ with open(file_path, "r", encoding="utf-8") as f:
37
+ text = f.read()
38
+
39
+ # Break text into small parts
40
+ sentences = text.split(". ")
41
+ chunks = [" ".join(sentences[i:i + 5]) for i in range(0, len(sentences), 5)]
42
+
43
+ # Create embeddings and store in FAISS
44
+ embeddings = np.array([embedding_model.encode(chunk) for chunk in chunks])
45
+ index = faiss.IndexFlatL2(embeddings.shape[1])
46
+ index.add(embeddings)
47
+
48
+ return "📄 Document is ready! Now ask your question."
49
+
50
+ # Find and answer questions
51
+ def get_answer(query):
52
+ if index is None:
53
+ return "❌ Please upload a document first."
54
+
55
+ # Find best matching text
56
+ query_embedding = embedding_model.encode(query).reshape(1, -1)
57
+ distances, indices = index.search(query_embedding, 3)
58
+ retrieved_text = " ".join([chunks[i] for i in indices[0]])
59
+
60
+ # Ask AI to generate an answer
61
+ input_text = f"Question: {query}\nContext: {retrieved_text}"
62
+ response = llm_pipeline(input_text, max_length=100)[0]['generated_text']
63
+
64
+ return response
65
+
66
+ # Webpage design
67
+ with gr.Blocks() as demo:
68
+ gr.Markdown("# 📚 Smart Study Helper")
69
+
70
+ file_input = gr.File(label="Upload a textbook or notes (PDF/TXT)", file_types=[".pdf", ".txt"])
71
+ upload_button = gr.Button("Process Document")
72
+ status_text = gr.Textbox(label="📢 Status", interactive=False)
73
+
74
+ query_input = gr.Textbox(label="Ask a question from the document:")
75
+ query_button = gr.Button("Get Answer")
76
+ output_text = gr.Textbox(label="🤖 AI Answer", interactive=False)
77
+
78
+ upload_button.click(load_document, inputs=file_input, outputs=status_text)
79
+ query_button.click(get_answer, inputs=query_input, outputs=output_text)
80
+
81
+ # Run the app
82
+ demo.launch()