MiakOnline commited on
Commit
7ec7528
·
verified ·
1 Parent(s): 3cc40cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -70,11 +70,17 @@ def extract_text(file_path):
70
  doc = Document(file_path)
71
  for para in doc.paragraphs:
72
  text += para.text + "\n"
73
- return text
74
 
75
  def create_vectorstore(text):
 
 
76
  splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
77
  docs = splitter.create_documents([text])
 
 
 
 
78
  embeddings = HuggingFaceEmbeddings()
79
  vectorstore = FAISS.from_documents(docs, embeddings)
80
  return vectorstore
@@ -107,17 +113,23 @@ def get_answer(query, vectorstore, mode):
107
 
108
  if uploaded_file:
109
  raw_text = extract_text(uploaded_file)
110
- st.success("📄 Syllabus loaded successfully!")
111
- query = st.text_input(" Ask your question (Urdu or English)")
112
- if query:
113
- with st.spinner("Thinking..."):
114
- vs = create_vectorstore(raw_text)
115
- answer = get_answer(query, vs, mode)
116
- st.markdown("### ✅ Answer:")
117
- st.write(answer)
118
- if voice_enabled:
119
- audio_file = generate_voice(answer)
120
- with open(audio_file, "rb") as f:
121
- st.audio(f.read(), format="audio/mp3")
 
 
 
 
 
 
122
  else:
123
  st.info("Please paste a valid Google Drive link to load your syllabus file.")
 
70
  doc = Document(file_path)
71
  for para in doc.paragraphs:
72
  text += para.text + "\n"
73
+ return text.strip()
74
 
75
  def create_vectorstore(text):
76
+ if not text:
77
+ return None
78
  splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
79
  docs = splitter.create_documents([text])
80
+ # Filter out empty docs if any
81
+ docs = [doc for doc in docs if doc.page_content.strip()]
82
+ if not docs:
83
+ return None
84
  embeddings = HuggingFaceEmbeddings()
85
  vectorstore = FAISS.from_documents(docs, embeddings)
86
  return vectorstore
 
113
 
114
  if uploaded_file:
115
  raw_text = extract_text(uploaded_file)
116
+ if not raw_text:
117
+ st.error(" No text could be extracted from the syllabus file. Please check the file.")
118
+ else:
119
+ st.success("📄 Syllabus loaded successfully!")
120
+ query = st.text_input("❓ Ask your question (Urdu or English)")
121
+ if query:
122
+ with st.spinner("Thinking..."):
123
+ vs = create_vectorstore(raw_text)
124
+ if vs is None:
125
+ st.error("❌ Failed to create vector store. The syllabus content may be too short or invalid.")
126
+ else:
127
+ answer = get_answer(query, vs, mode)
128
+ st.markdown("### ✅ Answer:")
129
+ st.write(answer)
130
+ if voice_enabled:
131
+ audio_file = generate_voice(answer)
132
+ with open(audio_file, "rb") as f:
133
+ st.audio(f.read(), format="audio/mp3")
134
  else:
135
  st.info("Please paste a valid Google Drive link to load your syllabus file.")