Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
25 |
|
26 |
-
|
|
|
27 |
st.markdown("### π Document Upload")
|
28 |
uploaded_files: List[IO[bytes]] = st.file_uploader(
|
29 |
-
"Upload
|
30 |
accept_multiple_files=True,
|
31 |
type=["pdf", "docx"],
|
32 |
-
help="
|
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("π
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
-
if
|
46 |
-
|
47 |
-
if
|
48 |
-
|
49 |
|
50 |
-
if
|
51 |
-
|
52 |
-
get_vector_store(
|
53 |
-
st.success("β
Documents processed
|
54 |
else:
|
55 |
-
st.warning("β οΈ No
|
56 |
|
57 |
with st.expander("βΉοΈ How to use"):
|
58 |
st.markdown(
|
59 |
"""
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
63 |
"""
|
64 |
)
|
65 |
|
66 |
-
|
|
|
67 |
st.markdown("### π¬ Ask Your Question")
|
68 |
-
|
69 |
"",
|
70 |
-
placeholder="Type
|
71 |
-
help="Ask anything about your uploaded documents or general knowledge"
|
72 |
)
|
73 |
|
74 |
-
if
|
75 |
-
user_input(
|
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()
|