-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (117 loc) · 5.09 KB
/
app.py
File metadata and controls
141 lines (117 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import streamlit as st
import tempfile
import os
import json
from rag_pipeline import run_pipeline, load_document, split_documents
from tools import summarize_document_tool, generate_mcqs_tool, topic_explanation_tool
# ── Page Config ─────────────────────────────────────────────────────────────
st.set_page_config(
page_title="Smart Academic Assistant",
page_icon="🎓",
layout="wide"
)
st.title("🎓 Smart Academic Assistant")
st.markdown("Upload your study material and ask questions — powered by RAG + Groq LLM")
# ── Sidebar ──────────────────────────────────────────────────────────────────
with st.sidebar:
st.header("⚙️ Settings")
mode = st.radio(
"Choose Mode",
["📖 Q&A (RAG)", "🤖 Agentic Tools"]
)
st.markdown("---")
st.markdown("**Models used:**")
st.markdown("- LLM: `llama-3.1-8b-instant` via Groq")
st.markdown("- Embeddings: `all-MiniLM-L6-v2`")
st.markdown("- Vector Store: `FAISS`")
# ── File Upload ───────────────────────────────────────────────────────────────
st.header("📂 Upload Document")
uploaded_file = st.file_uploader(
"Upload a PDF, DOCX, or TXT file",
type=["pdf", "docx", "txt"]
)
tmp_path = None
doc_text = ""
if uploaded_file:
suffix = os.path.splitext(uploaded_file.name)[-1]
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
tmp.write(uploaded_file.read())
tmp_path = tmp.name
st.success(f"✅ File uploaded: **{uploaded_file.name}**")
try:
docs = load_document(tmp_path)
doc_text = "\n\n".join([d.page_content for d in docs])
except Exception as e:
st.error(f"Error reading file: {e}")
if mode == "📖 Q&A (RAG)":
st.header("❓ Ask a Question")
question = st.text_input(
"Enter your question about the document:",
placeholder="e.g. What is the main topic of this document?"
)
if st.button("🔍 Get Answer", type="primary"):
if not uploaded_file:
st.warning("Please upload a document first.")
elif not question.strip():
st.warning("Please enter a question.")
else:
with st.spinner("Thinking... 🤔"):
try:
result = run_pipeline(tmp_path, question)
st.header("📊 Structured Response")
col1, col2 = st.columns(2)
with col1:
st.metric("Confidence Score", result.get("confidence_score", "N/A"))
with col2:
st.metric("Source", result.get("source_document", "N/A")[:40])
st.subheader("Question")
st.info(result.get("question", question))
st.subheader("Answer")
st.success(result.get("answer", "No answer found."))
st.subheader("Raw JSON Output")
st.json(result)
except Exception as e:
st.error(f"Pipeline error: {e}")
elif mode == "🤖 Agentic Tools":
st.header("🤖 Agentic Tools")
if not uploaded_file:
st.warning("Upload a document to use agentic tools.")
else:
tool_choice = st.selectbox(
"Select a tool:",
[
"📝 Summarize Document",
"❓ Generate MCQs",
"💡 Explain a Topic"
]
)
if tool_choice == "📝 Summarize Document":
if st.button("Summarize", type="primary"):
with st.spinner("Summarizing..."):
result = summarize_document_tool.invoke(doc_text)
st.subheader("📝 Summary")
st.write(result)
elif tool_choice == "❓ Generate MCQs":
if st.button("Generate MCQs", type="primary"):
with st.spinner("Generating questions..."):
result = generate_mcqs_tool.invoke(doc_text)
st.subheader("❓ MCQs")
st.write(result)
elif tool_choice == "💡 Explain a Topic":
topic = st.text_input(
"Enter topic to explain:",
placeholder="e.g. Binary Trees, Gradient Descent, French Revolution"
)
if st.button("Explain", type="primary"):
if not topic.strip():
st.warning("Enter a topic.")
else:
with st.spinner(f"Explaining '{topic}'..."):
result = topic_explanation_tool.invoke(topic)
st.subheader(f"💡 {topic}")
st.write(result)
st.markdown("---")
st.markdown(
"Built with LangChain · Groq · FAISS · Streamlit | "
"Smart Academic Assistant v1.0"
)