adi-123 commited on
Commit
6ff6ba2
Β·
verified Β·
1 Parent(s): 7e6e9de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -32
app.py CHANGED
@@ -1,4 +1,7 @@
1
  import streamlit as st
 
 
 
2
  from utils import (
3
  get_pdf_text,
4
  get_docx_text,
@@ -6,12 +9,12 @@ from utils import (
6
  get_vector_store,
7
  user_input,
8
  )
9
- from typing import List, IO
10
 
 
 
 
11
  def main() -> None:
12
- """
13
- Main function to run the Streamlit app.
14
- """
15
  st.set_page_config(
16
  page_title="Docosphere",
17
  page_icon="πŸ“„",
@@ -19,60 +22,66 @@ def main() -> None:
19
  )
20
 
21
  st.title("πŸ“„ Docosphere")
22
- st.markdown("*Where Documents Come Alive ...*")
23
 
24
- col1, col2 = st.columns([2, 1])
 
25
 
26
- with col2:
 
27
  st.markdown("### πŸ“ Document Upload")
28
  uploaded_files: List[IO[bytes]] = st.file_uploader(
29
- "Upload your documents (PDFs or Word files)",
30
  accept_multiple_files=True,
31
  type=["pdf", "docx"],
32
- help="Select one or more PDF or Word files to analyze"
33
  )
34
 
35
  if st.button("πŸš€ Process Documents"):
36
  if not uploaded_files:
37
- st.warning("πŸ“‹ Please upload at least one file.")
38
  return
39
 
40
- with st.spinner("πŸ”„ Processing your documents..."):
41
- raw_text = ""
42
- pdf_docs = [file for file in uploaded_files if file.name.endswith(".pdf")]
43
- docx_docs = [file for file in uploaded_files if file.name.endswith(".docx")]
 
44
 
45
- if pdf_docs:
46
- raw_text += get_pdf_text(pdf_docs)
47
- if docx_docs:
48
- raw_text += get_docx_text(docx_docs)
49
 
50
- if raw_text.strip():
51
- text_chunks: List[str] = get_text_chunks(raw_text)
52
- get_vector_store(text_chunks)
53
- st.success("βœ… Documents processed successfully! You can now ask questions about your content.")
54
  else:
55
- st.warning("⚠️ No content could be extracted from the uploaded files.")
56
 
57
  with st.expander("ℹ️ How to use"):
58
  st.markdown(
59
  """
60
- - Upload one or more PDF or Word files using the uploader above.
61
- - Click 'Process Documents' to analyze the content.
62
- - Ask your question below and receive answers powered by Together AI.
 
 
63
  """
64
  )
65
 
66
- with col1:
 
67
  st.markdown("### πŸ’¬ Ask Your Question")
68
- user_question: str = st.text_input(
69
  "",
70
- placeholder="Type your question here...",
71
- help="Ask anything about your uploaded documents or general knowledge"
72
  )
73
 
74
- if user_question:
75
- user_input(user_question)
76
 
 
77
  if __name__ == "__main__":
78
  main()
 
1
  import streamlit as st
2
+ from typing import List, IO
3
+
4
+ # Import utilities you finalised
5
  from utils import (
6
  get_pdf_text,
7
  get_docx_text,
 
9
  get_vector_store,
10
  user_input,
11
  )
 
12
 
13
+ # ---------------------------------------------------------------------------#
14
+ # Main Streamlit application
15
+ # ---------------------------------------------------------------------------#
16
  def main() -> None:
17
+ # ----- Page configuration ------------------------------------------------
 
 
18
  st.set_page_config(
19
  page_title="Docosphere",
20
  page_icon="πŸ“„",
 
22
  )
23
 
24
  st.title("πŸ“„ Docosphere")
25
+ st.markdown("*Where Documents Come Alive …*")
26
 
27
+ # Two-column layout: Q&A on left, file upload on right
28
+ col_left, col_right = st.columns([2, 1])
29
 
30
+ # --------------------- Right column – document upload -------------------
31
+ with col_right:
32
  st.markdown("### πŸ“ Document Upload")
33
  uploaded_files: List[IO[bytes]] = st.file_uploader(
34
+ "Upload PDF or Word files",
35
  accept_multiple_files=True,
36
  type=["pdf", "docx"],
37
+ help="You can select multiple files at once."
38
  )
39
 
40
  if st.button("πŸš€ Process Documents"):
41
  if not uploaded_files:
42
+ st.warning("πŸ“‹ Please upload at least one file first.")
43
  return
44
 
45
+ with st.spinner("πŸ”„ Extracting text & creating vector index…"):
46
+ combined_text = ""
47
+
48
+ pdfs = [f for f in uploaded_files if f.name.lower().endswith(".pdf")]
49
+ docs = [f for f in uploaded_files if f.name.lower().endswith(".docx")]
50
 
51
+ if pdfs:
52
+ combined_text += get_pdf_text(pdfs)
53
+ if docs:
54
+ combined_text += get_docx_text(docs)
55
 
56
+ if combined_text.strip():
57
+ chunks = get_text_chunks(combined_text)
58
+ get_vector_store(chunks)
59
+ st.success("βœ… Documents processed! Ask away in the left panel.")
60
  else:
61
+ st.warning("⚠️ No readable text found in the uploaded files.")
62
 
63
  with st.expander("ℹ️ How to use"):
64
  st.markdown(
65
  """
66
+ 1. Upload one or more **PDF** or **Word** documents.\n
67
+ 2. Click **Process Documents** to build the knowledge index.\n
68
+ 3. Ask natural-language questions in the input box (left column).\n
69
+ 4. The assistant will either answer from its own model knowledge or
70
+ retrieve context from your documents when needed.
71
  """
72
  )
73
 
74
+ # ---------------------- Left column – chat interface --------------------
75
+ with col_left:
76
  st.markdown("### πŸ’¬ Ask Your Question")
77
+ question: str = st.text_input(
78
  "",
79
+ placeholder="Type a question about your documents or general topics…"
 
80
  )
81
 
82
+ if question:
83
+ user_input(question)
84
 
85
+ # Entry-point guard
86
  if __name__ == "__main__":
87
  main()