Spaces:
Sleeping
Sleeping
File size: 6,509 Bytes
1b795b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import os
import streamlit as st
from dotenv import load_dotenv
load_dotenv()
def _get_streamlit_secret(key: str):
"""Safely fetch a Streamlit secret.
When running locally without `.streamlit/secrets.toml`, Streamlit raises an
exception on `st.secrets` access. We treat secrets as optional and fall back
to environment variables / `.env`.
"""
try:
return st.secrets.get(key)
except Exception:
return None
# Support Streamlit Cloud Secrets: if GROQ_API_KEY/GROQ_MODEL are set in secrets,
# expose them as environment variables for DocumentManager.
if not os.getenv("GROQ_API_KEY"):
secret_key = _get_streamlit_secret("GROQ_API_KEY")
if secret_key:
os.environ["GROQ_API_KEY"] = str(secret_key)
if not os.getenv("GROQ_MODEL"):
secret_model = _get_streamlit_secret("GROQ_MODEL")
if secret_model:
os.environ["GROQ_MODEL"] = str(secret_model)
from smartdocument import DocumentManager
st.set_page_config(page_title="π Smart Document Q&A", layout="wide")
st.title("π Smart Document System")
st.write("Upload documents to analyze, summarize, compare, and perform more tasks.")
dm = DocumentManager()
# ---- Dropdown Menu ----
mode = st.selectbox(
"Select Action",
[
"π Upload & Ask Questions",
"π Summarize Document",
"π Compare Documents",
"π Extract Keywords",
"π Sentiment Analysis",
"π Translate Document"
]
)
# ---- Upload & Ask Questions ----
if mode == "π Upload & Ask Questions":
uploaded_file = st.file_uploader("Upload a document", type=["pdf", "txt", "docx", "csv", "md", "xlsx"])
if uploaded_file:
os.makedirs("uploads", exist_ok=True)
save_path = os.path.join("uploads", uploaded_file.name)
with open(save_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success(f"β
Saved file: {save_path}")
st.success(dm.load_documents(save_path))
question = st.text_input("Enter your question")
if st.button("Get Answer") and question:
answer = dm.ask_question(question)
st.markdown(f"**Answer:** {answer}")
# ---- Summarize Document ----
elif mode == "π Summarize Document":
uploaded_file = st.file_uploader("Upload a PDF,TXT or DOCX file for summarization", type=["pdf", "txt", "docx"])
if uploaded_file:
os.makedirs("uploads", exist_ok=True)
save_path = os.path.join("uploads", uploaded_file.name)
with open(save_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success(dm.load_documents(save_path))
if st.button("Summarize"):
summary = dm.ask_question("Summarize the document in detail.")
st.markdown(f"**Summary:** {summary}")
# ---- Compare Documents ----
elif mode == "π Compare Documents":
if "dm1" not in st.session_state:
st.session_state.dm1 = DocumentManager()
if "dm2" not in st.session_state:
st.session_state.dm2 = DocumentManager()
file1 = st.file_uploader("Upload First Document", type=["pdf", "txt", "docx"], key="file1")
if file1:
path1 = os.path.join("uploads", file1.name)
os.makedirs("uploads", exist_ok=True)
with open(path1, "wb") as f:
f.write(file1.getbuffer())
result1 = st.session_state.dm1.load_documents(path1)
st.success(f"β
Loaded {file1.name}")
if result1:
st.info(result1)
file2 = st.file_uploader("Upload Second Document", type=["pdf", "txt", "docx"], key="file2")
if file2:
path2 = os.path.join("uploads", file2.name)
os.makedirs("uploads", exist_ok=True)
with open(path2, "wb") as f:
f.write(file2.getbuffer())
result2 = st.session_state.dm2.load_documents(path2)
st.success(f"β
Loaded {file2.name}")
if result2:
st.info(result2)
if st.button("Compare"):
if getattr(st.session_state.dm1, "vector_store", None) and getattr(st.session_state.dm2, "vector_store", None):
answer1 = st.session_state.dm1.ask_question("Summarize the document in detail.")
answer2 = st.session_state.dm2.ask_question("Summarize the document in detail.")
comparison_result = st.session_state.dm1.ask_question(
f"Document 1 Summary: {answer1}\nDocument 2 Summary: {answer2}\nNow compare them in detail."
)
st.markdown(f"**Comparison:** {comparison_result}")
else:
st.error("β Please upload and load both documents before comparing.")
# ---- Extract Keywords ----
elif mode == "π Extract Keywords":
uploaded_file = st.file_uploader("Upload a PDF,TXT or DOCX file", type=["pdf", "txt", "docx"])
if uploaded_file:
os.makedirs("uploads", exist_ok=True)
save_path = os.path.join("uploads", uploaded_file.name)
with open(save_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success(dm.load_documents(save_path))
if st.button("Extract Keywords"):
keywords = dm.extract_keywords()
st.markdown(f"**Keywords:** {keywords}")
# ---- Sentiment Analysis ----
elif mode == "π Sentiment Analysis":
uploaded_file = st.file_uploader("Upload a PDF,TXT or DOCX file", type=["pdf", "txt", "docx"])
if uploaded_file:
os.makedirs("uploads", exist_ok=True)
save_path = os.path.join("uploads", uploaded_file.name)
with open(save_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success(dm.load_documents(save_path))
if st.button("Analyze Sentiment"):
sentiment = dm.sentiment_analysis()
st.markdown(f"**Sentiment:** {sentiment}")
# ---- Translate Document ----
elif mode == "π Translate Document":
uploaded_file = st.file_uploader("Upload a PDF,TXT or DOCX file", type=["pdf", "txt", "docx"])
if uploaded_file:
os.makedirs("uploads", exist_ok=True)
save_path = os.path.join("uploads", uploaded_file.name)
with open(save_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success(dm.load_documents(save_path))
target_language = st.text_input("Enter target language (e.g., French, Hindi, Spanish)")
if st.button("Translate") and target_language:
translation = dm.translate_text(target_language)
st.markdown(f"**Translated Text ({target_language}):** {translation}")
|