Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,53 +10,75 @@ app = Flask(__name__)
|
|
| 10 |
|
| 11 |
print("Loading models and data...")
|
| 12 |
|
| 13 |
-
# Download FAISS index and chunks
|
| 14 |
faiss_path = hf_hub_download(
|
| 15 |
-
repo_id="Redfire-1234/
|
| 16 |
filename="faiss_index.bin",
|
| 17 |
-
repo_type="
|
| 18 |
)
|
| 19 |
chunks_path = hf_hub_download(
|
| 20 |
-
repo_id="Redfire-1234/
|
| 21 |
filename="all_chunks.pkl",
|
| 22 |
-
repo_type="
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# Load FAISS
|
| 26 |
index = faiss.read_index(faiss_path)
|
|
|
|
|
|
|
| 27 |
with open(chunks_path, "rb") as f:
|
| 28 |
all_chunks = pickle.load(f)
|
| 29 |
|
| 30 |
-
# Load
|
| 31 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
|
|
|
| 32 |
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 34 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
| 36 |
|
| 37 |
-
print(f"
|
| 38 |
|
| 39 |
def search(query, k=3):
|
|
|
|
| 40 |
q_emb = embed_model.encode([query]).astype("float32")
|
| 41 |
D, I = index.search(q_emb, k)
|
| 42 |
return "\n".join([all_chunks[i] for i in I[0]])
|
| 43 |
|
| 44 |
def generate_answer(query, context, mode="answer"):
|
|
|
|
| 45 |
if mode == "mcq":
|
| 46 |
-
prompt = f"""
|
| 47 |
-
-
|
| 48 |
-
-
|
|
|
|
|
|
|
|
|
|
| 49 |
Text: {context}
|
|
|
|
| 50 |
Question:"""
|
| 51 |
else:
|
| 52 |
-
prompt = f"""Answer based on
|
|
|
|
|
|
|
| 53 |
Text: {context}
|
|
|
|
| 54 |
Question: {query}
|
|
|
|
| 55 |
Answer:"""
|
| 56 |
|
| 57 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
| 58 |
-
outputs = model.generate(**inputs, max_new_tokens=200)
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
HTML_TEMPLATE = """
|
| 62 |
<!DOCTYPE html>
|
|
@@ -64,57 +86,215 @@ HTML_TEMPLATE = """
|
|
| 64 |
<head>
|
| 65 |
<title>Class 12 PCB AI Tutor</title>
|
| 66 |
<style>
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
.tab-content { display: none; }
|
| 79 |
.tab-content.active { display: block; }
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
.loading.show { display: block; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
</style>
|
| 87 |
</head>
|
| 88 |
<body>
|
| 89 |
<div class="container">
|
| 90 |
-
<
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
</div>
|
| 97 |
|
| 98 |
-
<div
|
| 99 |
-
<
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
<
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
<div
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
</div>
|
| 114 |
</div>
|
| 115 |
|
| 116 |
<script>
|
|
|
|
|
|
|
| 117 |
function switchTab(tab) {
|
|
|
|
| 118 |
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
|
| 119 |
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
| 120 |
event.target.classList.add('active');
|
|
@@ -124,42 +304,82 @@ HTML_TEMPLATE = """
|
|
| 124 |
|
| 125 |
async function getAnswer() {
|
| 126 |
const query = document.getElementById('queryInput').value.trim();
|
| 127 |
-
if (!query) {
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
}
|
| 130 |
|
| 131 |
async function generateMCQ() {
|
| 132 |
const topic = document.getElementById('mcqTopic').value.trim();
|
| 133 |
-
if (!topic) {
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
async function makeRequest(query, mode) {
|
| 138 |
-
const loading = document.getElementById('loading');
|
| 139 |
-
const result = document.getElementById('result');
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
|
| 144 |
try {
|
| 145 |
const response = await fetch('/query', {
|
| 146 |
method: 'POST',
|
| 147 |
headers: {'Content-Type': 'application/json'},
|
| 148 |
-
body: JSON.stringify({query, mode})
|
| 149 |
});
|
| 150 |
|
| 151 |
const data = await response.json();
|
| 152 |
-
if (data.error) { alert('Error: ' + data.error); return; }
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
} catch (error) {
|
| 158 |
alert('Error: ' + error.message);
|
| 159 |
} finally {
|
| 160 |
-
|
| 161 |
}
|
| 162 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
</script>
|
| 164 |
</body>
|
| 165 |
</html>
|
|
@@ -173,17 +393,31 @@ def home():
|
|
| 173 |
def query():
|
| 174 |
try:
|
| 175 |
data = request.get_json()
|
|
|
|
| 176 |
if not data or 'query' not in data:
|
| 177 |
return jsonify({'error': 'No query provided'}), 400
|
| 178 |
|
| 179 |
query_text = data['query']
|
| 180 |
mode = data.get('mode', 'answer')
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
context = search(query_text, k=3)
|
|
|
|
|
|
|
| 183 |
answer = generate_answer(query_text, context, mode)
|
| 184 |
|
| 185 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
except Exception as e:
|
|
|
|
| 187 |
return jsonify({'error': str(e)}), 500
|
| 188 |
|
| 189 |
@app.route('/health')
|
|
|
|
| 10 |
|
| 11 |
print("Loading models and data...")
|
| 12 |
|
| 13 |
+
# Download FAISS index and chunks from your model repo
|
| 14 |
faiss_path = hf_hub_download(
|
| 15 |
+
repo_id="Redfire-1234/pcb_tutor",
|
| 16 |
filename="faiss_index.bin",
|
| 17 |
+
repo_type="model"
|
| 18 |
)
|
| 19 |
chunks_path = hf_hub_download(
|
| 20 |
+
repo_id="Redfire-1234/pcb_tutor",
|
| 21 |
filename="all_chunks.pkl",
|
| 22 |
+
repo_type="model"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Load FAISS index
|
| 26 |
index = faiss.read_index(faiss_path)
|
| 27 |
+
|
| 28 |
+
# Load chunks
|
| 29 |
with open(chunks_path, "rb") as f:
|
| 30 |
all_chunks = pickle.load(f)
|
| 31 |
|
| 32 |
+
# Load embedding model
|
| 33 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 34 |
+
|
| 35 |
+
# Load LLM
|
| 36 |
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 38 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 39 |
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
| 40 |
|
| 41 |
+
print(f"Models loaded successfully! Using device: {device}")
|
| 42 |
|
| 43 |
def search(query, k=3):
|
| 44 |
+
"""Search for relevant chunks using FAISS"""
|
| 45 |
q_emb = embed_model.encode([query]).astype("float32")
|
| 46 |
D, I = index.search(q_emb, k)
|
| 47 |
return "\n".join([all_chunks[i] for i in I[0]])
|
| 48 |
|
| 49 |
def generate_answer(query, context, mode="answer"):
|
| 50 |
+
"""Generate answer or MCQ based on context"""
|
| 51 |
if mode == "mcq":
|
| 52 |
+
prompt = f"""You are a Class-12 teacher. Based on this text, generate 1 MCQ question with:
|
| 53 |
+
- 1 correct answer
|
| 54 |
+
- 3 wrong but realistic options
|
| 55 |
+
- Label them as A, B, C, D
|
| 56 |
+
- Indicate the correct answer
|
| 57 |
+
|
| 58 |
Text: {context}
|
| 59 |
+
|
| 60 |
Question:"""
|
| 61 |
else:
|
| 62 |
+
prompt = f"""You are a Class-12 teacher. Answer this question based on the given text.
|
| 63 |
+
Keep your answer clear, concise, and accurate.
|
| 64 |
+
|
| 65 |
Text: {context}
|
| 66 |
+
|
| 67 |
Question: {query}
|
| 68 |
+
|
| 69 |
Answer:"""
|
| 70 |
|
| 71 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
| 72 |
+
outputs = model.generate(**inputs, max_new_tokens=200, temperature=0.7, do_sample=True)
|
| 73 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 74 |
+
|
| 75 |
+
# Extract only the answer part (after "Answer:" or "Question:")
|
| 76 |
+
if mode == "answer" and "Answer:" in response:
|
| 77 |
+
response = response.split("Answer:")[-1].strip()
|
| 78 |
+
elif mode == "mcq" and "Question:" in response:
|
| 79 |
+
response = response.split("Question:")[-1].strip()
|
| 80 |
+
|
| 81 |
+
return response
|
| 82 |
|
| 83 |
HTML_TEMPLATE = """
|
| 84 |
<!DOCTYPE html>
|
|
|
|
| 86 |
<head>
|
| 87 |
<title>Class 12 PCB AI Tutor</title>
|
| 88 |
<style>
|
| 89 |
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 90 |
+
body {
|
| 91 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 92 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 93 |
+
min-height: 100vh;
|
| 94 |
+
padding: 20px;
|
| 95 |
+
}
|
| 96 |
+
.container {
|
| 97 |
+
max-width: 900px;
|
| 98 |
+
margin: 0 auto;
|
| 99 |
+
background: white;
|
| 100 |
+
border-radius: 20px;
|
| 101 |
+
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 102 |
+
overflow: hidden;
|
| 103 |
+
}
|
| 104 |
+
.header {
|
| 105 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 106 |
+
color: white;
|
| 107 |
+
padding: 30px;
|
| 108 |
+
text-align: center;
|
| 109 |
+
}
|
| 110 |
+
.header h1 { font-size: 2.5em; margin-bottom: 10px; }
|
| 111 |
+
.header p { font-size: 1.1em; opacity: 0.9; }
|
| 112 |
+
.content { padding: 40px; }
|
| 113 |
+
.tabs {
|
| 114 |
+
display: flex;
|
| 115 |
+
gap: 10px;
|
| 116 |
+
margin-bottom: 30px;
|
| 117 |
+
border-bottom: 2px solid #eee;
|
| 118 |
+
}
|
| 119 |
+
.tab {
|
| 120 |
+
padding: 15px 30px;
|
| 121 |
+
cursor: pointer;
|
| 122 |
+
border: none;
|
| 123 |
+
background: none;
|
| 124 |
+
font-size: 16px;
|
| 125 |
+
font-weight: 600;
|
| 126 |
+
color: #666;
|
| 127 |
+
border-bottom: 3px solid transparent;
|
| 128 |
+
transition: all 0.3s;
|
| 129 |
+
}
|
| 130 |
+
.tab:hover { color: #667eea; }
|
| 131 |
+
.tab.active {
|
| 132 |
+
color: #667eea;
|
| 133 |
+
border-bottom-color: #667eea;
|
| 134 |
+
}
|
| 135 |
.tab-content { display: none; }
|
| 136 |
.tab-content.active { display: block; }
|
| 137 |
+
textarea {
|
| 138 |
+
width: 100%;
|
| 139 |
+
padding: 15px;
|
| 140 |
+
border: 2px solid #e0e0e0;
|
| 141 |
+
border-radius: 10px;
|
| 142 |
+
font-size: 16px;
|
| 143 |
+
font-family: inherit;
|
| 144 |
+
resize: vertical;
|
| 145 |
+
min-height: 120px;
|
| 146 |
+
margin-bottom: 20px;
|
| 147 |
+
transition: border-color 0.3s;
|
| 148 |
+
}
|
| 149 |
+
textarea:focus {
|
| 150 |
+
outline: none;
|
| 151 |
+
border-color: #667eea;
|
| 152 |
+
}
|
| 153 |
+
.button-group {
|
| 154 |
+
display: flex;
|
| 155 |
+
gap: 10px;
|
| 156 |
+
margin-bottom: 20px;
|
| 157 |
+
}
|
| 158 |
+
button {
|
| 159 |
+
flex: 1;
|
| 160 |
+
padding: 15px 30px;
|
| 161 |
+
border: none;
|
| 162 |
+
border-radius: 10px;
|
| 163 |
+
font-size: 16px;
|
| 164 |
+
font-weight: 600;
|
| 165 |
+
cursor: pointer;
|
| 166 |
+
transition: all 0.3s;
|
| 167 |
+
}
|
| 168 |
+
.btn-primary {
|
| 169 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 170 |
+
color: white;
|
| 171 |
+
}
|
| 172 |
+
.btn-primary:hover {
|
| 173 |
+
transform: translateY(-2px);
|
| 174 |
+
box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
|
| 175 |
+
}
|
| 176 |
+
.btn-primary:disabled {
|
| 177 |
+
background: #ccc;
|
| 178 |
+
cursor: not-allowed;
|
| 179 |
+
transform: none;
|
| 180 |
+
}
|
| 181 |
+
.result-box {
|
| 182 |
+
background: #f8f9fa;
|
| 183 |
+
border-radius: 10px;
|
| 184 |
+
padding: 20px;
|
| 185 |
+
margin-top: 20px;
|
| 186 |
+
display: none;
|
| 187 |
+
border-left: 4px solid #667eea;
|
| 188 |
+
}
|
| 189 |
+
.result-box.show { display: block; }
|
| 190 |
+
.result-box h3 {
|
| 191 |
+
color: #667eea;
|
| 192 |
+
margin-bottom: 15px;
|
| 193 |
+
font-size: 1.2em;
|
| 194 |
+
}
|
| 195 |
+
.result-box .context {
|
| 196 |
+
background: white;
|
| 197 |
+
padding: 15px;
|
| 198 |
+
border-radius: 8px;
|
| 199 |
+
margin-bottom: 15px;
|
| 200 |
+
font-size: 14px;
|
| 201 |
+
color: #666;
|
| 202 |
+
max-height: 200px;
|
| 203 |
+
overflow-y: auto;
|
| 204 |
+
}
|
| 205 |
+
.result-box .answer {
|
| 206 |
+
background: white;
|
| 207 |
+
padding: 15px;
|
| 208 |
+
border-radius: 8px;
|
| 209 |
+
font-size: 16px;
|
| 210 |
+
line-height: 1.6;
|
| 211 |
+
white-space: pre-wrap;
|
| 212 |
+
}
|
| 213 |
+
.loading {
|
| 214 |
+
text-align: center;
|
| 215 |
+
padding: 20px;
|
| 216 |
+
display: none;
|
| 217 |
+
}
|
| 218 |
.loading.show { display: block; }
|
| 219 |
+
.spinner {
|
| 220 |
+
border: 3px solid #f3f3f3;
|
| 221 |
+
border-top: 3px solid #667eea;
|
| 222 |
+
border-radius: 50%;
|
| 223 |
+
width: 40px;
|
| 224 |
+
height: 40px;
|
| 225 |
+
animation: spin 1s linear infinite;
|
| 226 |
+
margin: 0 auto;
|
| 227 |
+
}
|
| 228 |
+
@keyframes spin {
|
| 229 |
+
0% { transform: rotate(0deg); }
|
| 230 |
+
100% { transform: rotate(360deg); }
|
| 231 |
+
}
|
| 232 |
+
.subject-tag {
|
| 233 |
+
display: inline-block;
|
| 234 |
+
padding: 5px 15px;
|
| 235 |
+
border-radius: 20px;
|
| 236 |
+
font-size: 12px;
|
| 237 |
+
font-weight: 600;
|
| 238 |
+
margin-right: 10px;
|
| 239 |
+
margin-bottom: 10px;
|
| 240 |
+
}
|
| 241 |
+
.bio { background: #d4edda; color: #155724; }
|
| 242 |
+
.chem { background: #d1ecf1; color: #0c5460; }
|
| 243 |
+
.phy { background: #f8d7da; color: #721c24; }
|
| 244 |
</style>
|
| 245 |
</head>
|
| 246 |
<body>
|
| 247 |
<div class="container">
|
| 248 |
+
<div class="header">
|
| 249 |
+
<h1>🎓 Class 12 PCB AI Tutor</h1>
|
| 250 |
+
<p>Ask questions or generate MCQs from Physics, Chemistry & Biology</p>
|
| 251 |
+
<div style="margin-top: 15px;">
|
| 252 |
+
<span class="subject-tag bio">Biology</span>
|
| 253 |
+
<span class="subject-tag chem">Chemistry</span>
|
| 254 |
+
<span class="subject-tag phy">Physics</span>
|
| 255 |
+
</div>
|
| 256 |
</div>
|
| 257 |
|
| 258 |
+
<div class="content">
|
| 259 |
+
<div class="tabs">
|
| 260 |
+
<button class="tab active" onclick="switchTab('answer')">Ask Question</button>
|
| 261 |
+
<button class="tab" onclick="switchTab('mcq')">Generate MCQ</button>
|
| 262 |
+
</div>
|
| 263 |
+
|
| 264 |
+
<div id="answer-tab" class="tab-content active">
|
| 265 |
+
<textarea id="queryInput" placeholder="Ask any question from Class 12 PCB...
|
| 266 |
+
Example: What is the structure of mitochondria?"></textarea>
|
| 267 |
+
<div class="button-group">
|
| 268 |
+
<button class="btn-primary" onclick="getAnswer()">Get Answer</button>
|
| 269 |
+
</div>
|
| 270 |
+
</div>
|
| 271 |
+
|
| 272 |
+
<div id="mcq-tab" class="tab-content">
|
| 273 |
+
<textarea id="mcqTopic" placeholder="Enter a topic to generate MCQ...
|
| 274 |
+
Example: Mitochondria, Chemical Bonding, Newton's Laws"></textarea>
|
| 275 |
+
<div class="button-group">
|
| 276 |
+
<button class="btn-primary" onclick="generateMCQ()">Generate MCQ</button>
|
| 277 |
+
</div>
|
| 278 |
+
</div>
|
| 279 |
+
|
| 280 |
+
<div class="loading" id="loading">
|
| 281 |
+
<div class="spinner"></div>
|
| 282 |
+
<p style="margin-top: 15px; color: #666;">Processing your request...</p>
|
| 283 |
+
</div>
|
| 284 |
+
|
| 285 |
+
<div class="result-box" id="result">
|
| 286 |
+
<h3 id="resultTitle">Answer:</h3>
|
| 287 |
+
<div class="context" id="context"></div>
|
| 288 |
+
<div class="answer" id="answer"></div>
|
| 289 |
+
</div>
|
| 290 |
</div>
|
| 291 |
</div>
|
| 292 |
|
| 293 |
<script>
|
| 294 |
+
let currentTab = 'answer';
|
| 295 |
+
|
| 296 |
function switchTab(tab) {
|
| 297 |
+
currentTab = tab;
|
| 298 |
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
|
| 299 |
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
| 300 |
event.target.classList.add('active');
|
|
|
|
| 304 |
|
| 305 |
async function getAnswer() {
|
| 306 |
const query = document.getElementById('queryInput').value.trim();
|
| 307 |
+
if (!query) {
|
| 308 |
+
alert('Please enter a question!');
|
| 309 |
+
return;
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
showLoading(true);
|
| 313 |
+
hideResult();
|
| 314 |
+
|
| 315 |
+
try {
|
| 316 |
+
const response = await fetch('/query', {
|
| 317 |
+
method: 'POST',
|
| 318 |
+
headers: {'Content-Type': 'application/json'},
|
| 319 |
+
body: JSON.stringify({query: query, mode: 'answer'})
|
| 320 |
+
});
|
| 321 |
+
|
| 322 |
+
const data = await response.json();
|
| 323 |
+
|
| 324 |
+
if (data.error) {
|
| 325 |
+
alert('Error: ' + data.error);
|
| 326 |
+
return;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
showResult('Answer:', data.context, data.answer);
|
| 330 |
+
} catch (error) {
|
| 331 |
+
alert('Error: ' + error.message);
|
| 332 |
+
} finally {
|
| 333 |
+
showLoading(false);
|
| 334 |
+
}
|
| 335 |
}
|
| 336 |
|
| 337 |
async function generateMCQ() {
|
| 338 |
const topic = document.getElementById('mcqTopic').value.trim();
|
| 339 |
+
if (!topic) {
|
| 340 |
+
alert('Please enter a topic!');
|
| 341 |
+
return;
|
| 342 |
+
}
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
+
showLoading(true);
|
| 345 |
+
hideResult();
|
| 346 |
|
| 347 |
try {
|
| 348 |
const response = await fetch('/query', {
|
| 349 |
method: 'POST',
|
| 350 |
headers: {'Content-Type': 'application/json'},
|
| 351 |
+
body: JSON.stringify({query: topic, mode: 'mcq'})
|
| 352 |
});
|
| 353 |
|
| 354 |
const data = await response.json();
|
|
|
|
| 355 |
|
| 356 |
+
if (data.error) {
|
| 357 |
+
alert('Error: ' + data.error);
|
| 358 |
+
return;
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
showResult('Generated MCQ:', data.context, data.answer);
|
| 362 |
} catch (error) {
|
| 363 |
alert('Error: ' + error.message);
|
| 364 |
} finally {
|
| 365 |
+
showLoading(false);
|
| 366 |
}
|
| 367 |
}
|
| 368 |
+
|
| 369 |
+
function showLoading(show) {
|
| 370 |
+
document.getElementById('loading').classList.toggle('show', show);
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
function hideResult() {
|
| 374 |
+
document.getElementById('result').classList.remove('show');
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
function showResult(title, context, answer) {
|
| 378 |
+
document.getElementById('resultTitle').textContent = title;
|
| 379 |
+
document.getElementById('context').textContent = 'Retrieved Context:\n' + context;
|
| 380 |
+
document.getElementById('answer').textContent = answer;
|
| 381 |
+
document.getElementById('result').classList.add('show');
|
| 382 |
+
}
|
| 383 |
</script>
|
| 384 |
</body>
|
| 385 |
</html>
|
|
|
|
| 393 |
def query():
|
| 394 |
try:
|
| 395 |
data = request.get_json()
|
| 396 |
+
|
| 397 |
if not data or 'query' not in data:
|
| 398 |
return jsonify({'error': 'No query provided'}), 400
|
| 399 |
|
| 400 |
query_text = data['query']
|
| 401 |
mode = data.get('mode', 'answer')
|
| 402 |
|
| 403 |
+
if not query_text.strip():
|
| 404 |
+
return jsonify({'error': 'Query cannot be empty'}), 400
|
| 405 |
+
|
| 406 |
+
# Search for relevant context
|
| 407 |
context = search(query_text, k=3)
|
| 408 |
+
|
| 409 |
+
# Generate answer or MCQ
|
| 410 |
answer = generate_answer(query_text, context, mode)
|
| 411 |
|
| 412 |
+
return jsonify({
|
| 413 |
+
'query': query_text,
|
| 414 |
+
'context': context,
|
| 415 |
+
'answer': answer,
|
| 416 |
+
'mode': mode
|
| 417 |
+
})
|
| 418 |
+
|
| 419 |
except Exception as e:
|
| 420 |
+
print(f"Error: {e}")
|
| 421 |
return jsonify({'error': str(e)}), 500
|
| 422 |
|
| 423 |
@app.route('/health')
|