Maira-ghaffar commited on
Commit
b291d5e
·
verified ·
1 Parent(s): 7e78d25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -81
app.py CHANGED
@@ -1,90 +1,60 @@
1
- import streamlit as st
2
- import requests
3
- import os
4
- from sentence_transformers import SentenceTransformer
5
- import faiss
6
- import numpy as np
7
 
8
- st.set_page_config(page_title="📚 AI Adaptive Learning (Ultimate)", layout="wide")
9
- st.title("📚 AI Adaptive Learning & Smart Revision System (Mistral 7B)")
10
 
11
- # -----------------------------
12
- # 1️⃣ Hugging Face API Setup
13
- # -----------------------------
14
- HF_API_TOKEN = st.secrets["HF_API_TOKEN"]
15
  MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
16
  API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
17
  HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
18
 
19
- def query_hf_api(prompt):
20
- payload = {
21
- "inputs": prompt,
22
- "parameters": {"max_new_tokens": 250, "temperature": 0.7, "top_p": 0.9}
23
- }
24
- response = requests.post(API_URL, headers=HEADERS, json=payload)
25
- if response.status_code == 200:
26
- output = response.json()
27
- return output[0]["generated_text"]
28
- else:
29
- return f"Error: {response.status_code} - {response.text}"
30
-
31
- # -----------------------------
32
- # 2️⃣ Build RAG (Context Retrieval)
33
- # -----------------------------
34
- @st.cache_resource
35
- def build_rag_index(doc_folder="docs"):
36
- embeddings = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
37
- corpus = []
38
- corpus_texts = []
39
-
40
- for file_name in os.listdir(doc_folder):
41
- if file_name.endswith(".txt"):
42
- path = os.path.join(doc_folder, file_name)
43
- with open(path, "r", encoding="utf-8") as f:
44
- text = f.read()
45
- sentences = text.split("\n")
46
- for sent in sentences:
47
- if sent.strip():
48
- corpus.append(embeddings.encode(sent))
49
- corpus_texts.append(sent)
50
-
51
- dim = corpus[0].shape[0]
52
- index = faiss.IndexFlatL2(dim)
53
- index.add(np.array(corpus).astype("float32"))
54
- return index, corpus_texts, embeddings
55
-
56
- index, corpus_texts, embedder = build_rag_index()
57
-
58
- # -----------------------------
59
- # 3️⃣ Retrieve Context
60
- # -----------------------------
61
- def retrieve_context(query, k=3):
62
- query_vec = embedder.encode(query).astype("float32")
63
- D, I = index.search(np.array([query_vec]), k)
64
- context = "\n".join([corpus_texts[i] for i in I[0]])
65
- return context
66
-
67
- # -----------------------------
68
- # 4️⃣ Streamlit UI
69
- # -----------------------------
70
- difficulty = st.selectbox("Select explanation style:", ["Explain Like I'm 10", "Detailed", "Step-by-Step"])
71
- user_question = st.text_input("Ask a question:")
72
-
73
- if st.button("Submit") and user_question:
74
- with st.spinner("Generating high-quality answer..."):
75
- context = retrieve_context(user_question, k=3)
76
- prompt = f"""
77
- You are a knowledgeable teacher and explain concepts clearly.
78
 
79
- Context:
80
- {context}
81
 
82
- Question:
83
- {user_question}
84
 
85
- Explain the answer in a {difficulty} manner.
86
- Answer:
87
- """
88
- answer = query_hf_api(prompt)
89
- st.subheader("📖 AI Answer:")
90
- st.write(answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ !{sys.executable} -m pip install streamlit transformers accelerate torch
 
 
 
 
3
 
4
+ import streamlit as st
 
5
 
6
+ HF_API_TOKEN = "your_huggingface_api_token_here" # <-- paste your token
 
 
 
7
  MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
8
  API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
9
  HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
10
 
11
+ import streamlit as st
12
+ from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
13
+ import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ st.title("📚 AI Adaptive Learning (Local Small Model)")
 
16
 
17
+ MODEL_ID = "microsoft/phi-2"
 
18
 
19
+ @st.cache_resource
20
+ def load_model():
21
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
22
+ # Set pad_token_id for the tokenizer if it's not already set, using eos_token_id as a fallback
23
+ if tokenizer.pad_token_id is None:
24
+ tokenizer.pad_token_id = tokenizer.eos_token_id
25
+
26
+ # Load the model configuration
27
+ config = AutoConfig.from_pretrained(MODEL_ID)
28
+
29
+ # Check if pad_token_id exists in config and set it if not, for Phi models this often needs to be explicitly added
30
+ if not hasattr(config, 'pad_token_id') or config.pad_token_id is None:
31
+ config.pad_token_id = tokenizer.pad_token_id
32
+
33
+ model = AutoModelForCausalLM.from_pretrained(
34
+ MODEL_ID,
35
+ config=config, # Pass the modified config to the model
36
+ torch_dtype=torch.float32,
37
+ device_map="auto"
38
+ )
39
+ return tokenizer, model
40
+
41
+ tokenizer, model = load_model()
42
+
43
+ # Input question
44
+ user_input = st.text_input("Ask a question:")
45
+
46
+ if st.button("Submit") and user_input:
47
+ inputs = tokenizer(user_input, return_tensors="pt")
48
+
49
+ with torch.no_grad():
50
+ outputs = model.generate(
51
+ **inputs,
52
+ max_new_tokens=150,
53
+ do_sample=True,
54
+ temperature=0.7
55
+ )
56
+
57
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
58
+
59
+ st.subheader("AI Answer:")
60
+ st.write(answer)