Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1816,11 +1816,14 @@ def load_user_memory(user_id):
|
|
| 1816 |
st.session_state["vector_store"] = {}
|
| 1817 |
|
| 1818 |
def get_document_content(doc_name=None):
|
|
|
|
|
|
|
|
|
|
| 1819 |
documents = st.session_state.get("documents", {})
|
| 1820 |
if not documents:
|
| 1821 |
return None, "No documents have been uploaded."
|
| 1822 |
|
| 1823 |
-
#
|
| 1824 |
if doc_name:
|
| 1825 |
for doc_id, doc_data in documents.items():
|
| 1826 |
if doc_data.get("name", "").lower() == doc_name.lower():
|
|
@@ -1831,7 +1834,7 @@ def get_document_content(doc_name=None):
|
|
| 1831 |
return None, f"Document '{doc_name}' does not contain any content."
|
| 1832 |
return None, f"Document '{doc_name}' not found."
|
| 1833 |
|
| 1834 |
-
# Default to the most
|
| 1835 |
last_doc = list(documents.values())[-1]
|
| 1836 |
content = last_doc.get("content")
|
| 1837 |
if content:
|
|
@@ -1842,21 +1845,18 @@ def get_document_content(doc_name=None):
|
|
| 1842 |
|
| 1843 |
def handle_document_query(query):
|
| 1844 |
"""
|
| 1845 |
-
Handle queries related to uploaded documents
|
| 1846 |
"""
|
| 1847 |
-
# Extract
|
| 1848 |
doc_name_match = re.search(r"[\"']?([^\"']+\.(pdf|docx|doc|txt))[\"']?", query, re.IGNORECASE)
|
| 1849 |
doc_name = doc_name_match.group(1) if doc_name_match else None
|
| 1850 |
|
| 1851 |
-
if not doc_name:
|
| 1852 |
-
return "No document mentioned in the query."
|
| 1853 |
-
|
| 1854 |
# Fetch document content
|
| 1855 |
doc_content, error = get_document_content(doc_name)
|
| 1856 |
if error:
|
| 1857 |
return error
|
| 1858 |
|
| 1859 |
-
# Generate AI response
|
| 1860 |
full_prompt = f"Document Content:\n{doc_content}\n\nUser Query: {query}\n\nResponse:"
|
| 1861 |
try:
|
| 1862 |
llm = ChatOpenAI(model="gpt-4", temperature=0.5, api_key=openai_api_key)
|
|
@@ -1865,7 +1865,6 @@ def handle_document_query(query):
|
|
| 1865 |
except Exception as e:
|
| 1866 |
logger.error(f"Error generating response using the document: {e}")
|
| 1867 |
return f"Error generating response using the document: {e}"
|
| 1868 |
-
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
|
|
|
| 1816 |
st.session_state["vector_store"] = {}
|
| 1817 |
|
| 1818 |
def get_document_content(doc_name=None):
|
| 1819 |
+
"""
|
| 1820 |
+
Retrieve content of an uploaded document from Streamlit session state.
|
| 1821 |
+
"""
|
| 1822 |
documents = st.session_state.get("documents", {})
|
| 1823 |
if not documents:
|
| 1824 |
return None, "No documents have been uploaded."
|
| 1825 |
|
| 1826 |
+
# Fetch specific document by name
|
| 1827 |
if doc_name:
|
| 1828 |
for doc_id, doc_data in documents.items():
|
| 1829 |
if doc_data.get("name", "").lower() == doc_name.lower():
|
|
|
|
| 1834 |
return None, f"Document '{doc_name}' does not contain any content."
|
| 1835 |
return None, f"Document '{doc_name}' not found."
|
| 1836 |
|
| 1837 |
+
# Default to the most recently uploaded document
|
| 1838 |
last_doc = list(documents.values())[-1]
|
| 1839 |
content = last_doc.get("content")
|
| 1840 |
if content:
|
|
|
|
| 1845 |
|
| 1846 |
def handle_document_query(query):
|
| 1847 |
"""
|
| 1848 |
+
Handle user queries related to uploaded documents.
|
| 1849 |
"""
|
| 1850 |
+
# Extract document name from the query
|
| 1851 |
doc_name_match = re.search(r"[\"']?([^\"']+\.(pdf|docx|doc|txt))[\"']?", query, re.IGNORECASE)
|
| 1852 |
doc_name = doc_name_match.group(1) if doc_name_match else None
|
| 1853 |
|
|
|
|
|
|
|
|
|
|
| 1854 |
# Fetch document content
|
| 1855 |
doc_content, error = get_document_content(doc_name)
|
| 1856 |
if error:
|
| 1857 |
return error
|
| 1858 |
|
| 1859 |
+
# Generate AI response using document context
|
| 1860 |
full_prompt = f"Document Content:\n{doc_content}\n\nUser Query: {query}\n\nResponse:"
|
| 1861 |
try:
|
| 1862 |
llm = ChatOpenAI(model="gpt-4", temperature=0.5, api_key=openai_api_key)
|
|
|
|
| 1865 |
except Exception as e:
|
| 1866 |
logger.error(f"Error generating response using the document: {e}")
|
| 1867 |
return f"Error generating response using the document: {e}"
|
|
|
|
| 1868 |
|
| 1869 |
|
| 1870 |
|