Spaces:
Running
Running
Commit
Β·
c577877
0
Parent(s):
π Initial commit
Browse files- Read Me.txt +78 -0
- app.py +173 -0
- requirements.txt +7 -0
Read Me.txt
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π AI Study Assistant (Powered by Microsoft Phi-3)
|
| 2 |
+
|
| 3 |
+
Welcome to the **AI Study Assistant** β your personalized academic learning tool, built to help students understand complex course material with clarity, precision, and speed.
|
| 4 |
+
|
| 5 |
+
### π What it does
|
| 6 |
+
|
| 7 |
+
- β
**Upload Study Material**: PDF, DOCX, TXT, or PPTX
|
| 8 |
+
- π€ **Ask Questions**: About any concept in the uploaded files
|
| 9 |
+
- π§ **AI-Powered Reasoning**: Uses Microsoftβs Phi-3 (128k) to provide deep, structured, academic responses
|
| 10 |
+
- π **Retrieval-Augmented Generation**: Combines vector-based document search with LLMs
|
| 11 |
+
- π **Context-Based Answers**: Doesnβt hallucinate β only uses whatβs in your file
|
| 12 |
+
- β¨ **Auto-Summary**: Large answers are summarized into a short academic recap
|
| 13 |
+
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
### π Who it's for
|
| 17 |
+
|
| 18 |
+
- Students revising for exams
|
| 19 |
+
- Professionals needing quick insights from documents
|
| 20 |
+
- Educators building tutoring tools
|
| 21 |
+
- Anyone learning from dense academic material
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
### π How it works
|
| 26 |
+
|
| 27 |
+
1. **Upload** one or more lecture files (PDF, Word, PPT, TXT)
|
| 28 |
+
2. Ask a question like:
|
| 29 |
+
- *"Define supervised learning"*
|
| 30 |
+
- *"Explain the difference between classification and regression"*
|
| 31 |
+
3. The AI will:
|
| 32 |
+
- Extract your document
|
| 33 |
+
- Find the most relevant parts
|
| 34 |
+
- Think deeply using a powerful LLM
|
| 35 |
+
- Give a clean, well-structured, academic answer
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
### π§ Under the hood
|
| 40 |
+
|
| 41 |
+
| Component | Description |
|
| 42 |
+
|------------------|----------------------------------------------------------|
|
| 43 |
+
| **LLM** | `microsoft/phi-3-mini-128k-instruct` (4-bit CPU mode) |
|
| 44 |
+
| **Vector Search** | FAISS + HuggingFace MiniLM Embeddings |
|
| 45 |
+
| **Framework** | Gradio Interface via Hugging Face Spaces |
|
| 46 |
+
| **Auto Summary** | DistilBART summarization for long answers |
|
| 47 |
+
|
| 48 |
+
---
|
| 49 |
+
|
| 50 |
+
### π Privacy
|
| 51 |
+
|
| 52 |
+
This app does **not** store or log any of your uploaded files or questions. All processing happens in memory and is discarded after your session.
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
### π¬ Example Prompts
|
| 57 |
+
|
| 58 |
+
> - "List all types of memory in operating systems"
|
| 59 |
+
> - "What does this document say about reinforcement learning?"
|
| 60 |
+
> - "Explain deadlock prevention based on the slides"
|
| 61 |
+
|
| 62 |
+
---
|
| 63 |
+
|
| 64 |
+
### π‘ Inspiration
|
| 65 |
+
|
| 66 |
+
Built to replicate how a personal AI tutor should behave: focused, accurate, context-aware, and reliable β not a chatbot, but a **study partner**.
|
| 67 |
+
|
| 68 |
+
---
|
| 69 |
+
|
| 70 |
+
### π§βπ» Built by
|
| 71 |
+
|
| 72 |
+
> Designed & developed by a student β for students β using only open-source models.
|
| 73 |
+
|
| 74 |
+
---
|
| 75 |
+
|
| 76 |
+
### π Tags
|
| 77 |
+
|
| 78 |
+
`#Education` `#AI` `#Phi3` `#StudentTools` `#NLP` `#Gradio` `#DocumentQA` `#OpenSource`
|
app.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import re
|
| 5 |
+
import torch
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from PyPDF2 import PdfReader
|
| 8 |
+
from docx import Document as DocxDocument
|
| 9 |
+
from pptx import Presentation
|
| 10 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 11 |
+
from langchain_community.vectorstores import FAISS
|
| 12 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 13 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 14 |
+
from langchain_core.documents import Document
|
| 15 |
+
|
| 16 |
+
# Load Reasoning Model
|
| 17 |
+
model_id = "microsoft/phi-3-mini-128k-instruct"
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
model_id,
|
| 21 |
+
torch_dtype=torch.bfloat16,
|
| 22 |
+
device_map="auto"
|
| 23 |
+
)
|
| 24 |
+
reasoning_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer,
|
| 25 |
+
max_new_tokens=512, temperature=0.7, top_p=0.9)
|
| 26 |
+
|
| 27 |
+
# Embedding Model
|
| 28 |
+
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 29 |
+
vectorstore = None
|
| 30 |
+
|
| 31 |
+
# Summarizer
|
| 32 |
+
summary_pipeline = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 33 |
+
|
| 34 |
+
def clean_text(text):
|
| 35 |
+
lines = text.split("\n")
|
| 36 |
+
cleaned = []
|
| 37 |
+
for line in lines:
|
| 38 |
+
line = line.strip()
|
| 39 |
+
if re.search(r'(Page \d+|Slide \d+|CS583|UIC|Bing Liu)', line, re.IGNORECASE):
|
| 40 |
+
continue
|
| 41 |
+
if len(line) < 3:
|
| 42 |
+
continue
|
| 43 |
+
line = re.sub(r'[^\x00-\x7F]+', ' ', line)
|
| 44 |
+
cleaned.append(line)
|
| 45 |
+
return "\n".join(cleaned)
|
| 46 |
+
|
| 47 |
+
def extract_text(file_path, ext):
|
| 48 |
+
if ext == ".pdf":
|
| 49 |
+
reader = PdfReader(file_path)
|
| 50 |
+
return "\n".join([page.extract_text() or "" for page in reader.pages])
|
| 51 |
+
elif ext == ".docx":
|
| 52 |
+
doc = DocxDocument(file_path)
|
| 53 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
| 54 |
+
elif ext == ".txt":
|
| 55 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 56 |
+
return f.read()
|
| 57 |
+
elif ext == ".pptx":
|
| 58 |
+
prs = Presentation(file_path)
|
| 59 |
+
return "\n".join(shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text"))
|
| 60 |
+
else:
|
| 61 |
+
raise ValueError("Unsupported file format")
|
| 62 |
+
|
| 63 |
+
def process_file(file):
|
| 64 |
+
global vectorstore
|
| 65 |
+
try:
|
| 66 |
+
ext = os.path.splitext(file.name)[1].lower()
|
| 67 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
|
| 68 |
+
tmp.write(file.read())
|
| 69 |
+
tmp.flush()
|
| 70 |
+
full_text = extract_text(tmp.name, ext)
|
| 71 |
+
|
| 72 |
+
cleaned = clean_text(full_text)
|
| 73 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 74 |
+
chunks = splitter.split_text(cleaned)
|
| 75 |
+
docs = [Document(page_content=c) for c in chunks]
|
| 76 |
+
vectorstore = FAISS.from_documents(docs, embedding_model)
|
| 77 |
+
|
| 78 |
+
return "β
File processed. You can now ask questions."
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"β Error: {str(e)}"
|
| 81 |
+
|
| 82 |
+
def generate_prompt(context, question):
|
| 83 |
+
return f"""
|
| 84 |
+
You are a helpful academic tutor assisting a student strictly based on course slides or textbook material.
|
| 85 |
+
|
| 86 |
+
Context:
|
| 87 |
+
{context}
|
| 88 |
+
|
| 89 |
+
Question:
|
| 90 |
+
{question}
|
| 91 |
+
|
| 92 |
+
Instructions:
|
| 93 |
+
- Answer ONLY using the above context. Do NOT add outside knowledge.
|
| 94 |
+
- Think clearly and deeply before answering.
|
| 95 |
+
- Use structured academic language based strictly on the context.
|
| 96 |
+
- Use clean formatting with helpful headings and minimal bullet points.
|
| 97 |
+
- Do NOT repeat the question or include prompt labels.
|
| 98 |
+
- If the context lacks an answer, say: "The provided material does not contain sufficient information to answer this question accurately."
|
| 99 |
+
- Output must be academically concise, well-organized, and visually clear.
|
| 100 |
+
""".strip()
|
| 101 |
+
|
| 102 |
+
def detect_question_type(q):
|
| 103 |
+
q = q.lower().strip()
|
| 104 |
+
if q.startswith(("what is", "define", "give definition")):
|
| 105 |
+
return "definition"
|
| 106 |
+
elif q.startswith(("how", "explain", "why")):
|
| 107 |
+
return "explanation"
|
| 108 |
+
elif "difference between" in q or "compare" in q:
|
| 109 |
+
return "comparison"
|
| 110 |
+
elif q.startswith("list") or "types of" in q:
|
| 111 |
+
return "list"
|
| 112 |
+
return "general"
|
| 113 |
+
|
| 114 |
+
def post_process_output(answer_text, question):
|
| 115 |
+
qtype = detect_question_type(question)
|
| 116 |
+
label_map = {
|
| 117 |
+
"definition": "π **Definition**",
|
| 118 |
+
"explanation": "π **Explanation**",
|
| 119 |
+
"comparison": "π **Comparison**",
|
| 120 |
+
"list": "π **Key Points**",
|
| 121 |
+
"general": "π **Insight**",
|
| 122 |
+
}
|
| 123 |
+
answer_text = f"{label_map.get(qtype)}\n\n{answer_text}"
|
| 124 |
+
|
| 125 |
+
if len(answer_text.split()) > 80:
|
| 126 |
+
summary = summary_pipeline(answer_text, max_length=60, min_length=25, do_sample=False)[0]['summary_text']
|
| 127 |
+
answer_text += f"\n\nπ **Summary:** {summary.strip()}"
|
| 128 |
+
|
| 129 |
+
return answer_text
|
| 130 |
+
|
| 131 |
+
def ask_question(question):
|
| 132 |
+
global vectorstore
|
| 133 |
+
if vectorstore is None:
|
| 134 |
+
return "β Please upload and process a file first."
|
| 135 |
+
|
| 136 |
+
docs = vectorstore.similarity_search(question, k=3)
|
| 137 |
+
if not docs:
|
| 138 |
+
return "β No relevant information found."
|
| 139 |
+
|
| 140 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 141 |
+
prompt = generate_prompt(context, question)
|
| 142 |
+
result = reasoning_pipeline(prompt)[0]['generated_text']
|
| 143 |
+
|
| 144 |
+
for marker in ["Context:", "Question:", "Instructions:"]:
|
| 145 |
+
if marker in result:
|
| 146 |
+
result = result.split(marker)[-1].strip()
|
| 147 |
+
if "." in result:
|
| 148 |
+
result = result.rsplit(".", 1)[0] + "."
|
| 149 |
+
|
| 150 |
+
return post_process_output(result.strip(), question)
|
| 151 |
+
|
| 152 |
+
# Gradio UI
|
| 153 |
+
title = "π AI Study Assistant"
|
| 154 |
+
with gr.Blocks(css="footer {display:none !important}") as demo:
|
| 155 |
+
gr.Markdown("""# π AI Study Assistant
|
| 156 |
+
Upload your lecture notes and ask deep academic questions. Powered by Phi-3 & FAISS.""")
|
| 157 |
+
|
| 158 |
+
with gr.Row():
|
| 159 |
+
file_input = gr.File(label="Upload Course Material (PDF, DOCX, TXT, PPTX)")
|
| 160 |
+
upload_btn = gr.Button("Process File")
|
| 161 |
+
|
| 162 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 163 |
+
|
| 164 |
+
question = gr.Textbox(label="Ask a Question", placeholder="E.g., What is demand paging?")
|
| 165 |
+
ask_btn = gr.Button("Get Answer")
|
| 166 |
+
answer = gr.Markdown("", elem_id="answer-box")
|
| 167 |
+
|
| 168 |
+
upload_btn.click(fn=process_file, inputs=file_input, outputs=status)
|
| 169 |
+
ask_btn.click(fn=ask_question, inputs=question, outputs=answer)
|
| 170 |
+
|
| 171 |
+
if __name__ == "__main__":
|
| 172 |
+
demo.launch()
|
| 173 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
pypdf
|
| 4 |
+
faiss-cpu
|
| 5 |
+
python-pptx
|
| 6 |
+
python-docx
|
| 7 |
+
gradio
|