Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,13 +43,13 @@ def get_loader(file_path):
|
|
43 |
return UnstructuredFileLoader(file_path, mode="elements", strategy="fast") # "elements" is good for tables
|
44 |
# Fallback or specific loaders if UnstructuredFileLoader has issues with a particular file
|
45 |
# elif ext == ".pdf":
|
46 |
-
#
|
47 |
# elif ext in [".docx", ".doc"]:
|
48 |
-
#
|
49 |
# elif ext in [".xlsx", ".xls"]:
|
50 |
-
#
|
51 |
# elif ext == ".json":
|
52 |
-
#
|
53 |
else:
|
54 |
st.warning(f"Unsupported file type: {ext}. Skipping {os.path.basename(file_path)}")
|
55 |
return None
|
@@ -134,7 +134,16 @@ def get_llm(api_key: str, model_name: str = "llama3-8b-8192"): # UPDATED MODEL
|
|
134 |
return None
|
135 |
|
136 |
# --- RAG Chain Setup ---
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
# --- Main Application Logic ---
|
140 |
def main():
|
@@ -147,7 +156,19 @@ def main():
|
|
147 |
# Custom CSS (remains the same)
|
148 |
st.markdown("""
|
149 |
<style>
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
</style>
|
152 |
""", unsafe_allow_html=True)
|
153 |
|
@@ -199,7 +220,6 @@ def main():
|
|
199 |
retriever = vector_store.as_retriever(search_kwargs={"k": 5})
|
200 |
|
201 |
# --- Query Input and Response ---
|
202 |
-
# ... (rest of the main function remains the same, including prompt templates, query input, button, and response display logic) ...
|
203 |
|
204 |
st.markdown("---")
|
205 |
st.subheader("Ask a question about our documents:")
|
@@ -230,9 +250,9 @@ def main():
|
|
230 |
Please perform the following steps:
|
231 |
1. Carefully analyze the context for any order details (Order ID, Customer Name, Status, Items, Dates, etc.).
|
232 |
2. If an order matching the query (or related to a name in the query) is found in the context:
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
3. If no specific order details are found in the context that match the query, or if the context is insufficient, politely state that you couldn't find the specific order information in the provided documents and suggest they contact support for further assistance.
|
237 |
4. Do NOT invent or infer any information not explicitly present in the context.
|
238 |
|
|
|
43 |
return UnstructuredFileLoader(file_path, mode="elements", strategy="fast") # "elements" is good for tables
|
44 |
# Fallback or specific loaders if UnstructuredFileLoader has issues with a particular file
|
45 |
# elif ext == ".pdf":
|
46 |
+
# return PyPDFLoader(file_path) # Basic PDF loader
|
47 |
# elif ext in [".docx", ".doc"]:
|
48 |
+
# return Docx2txtLoader(file_path) # Basic DOCX loader
|
49 |
# elif ext in [".xlsx", ".xls"]:
|
50 |
+
# return UnstructuredExcelLoader(file_path, mode="elements") # Unstructured for Excel
|
51 |
# elif ext == ".json":
|
52 |
+
# return JSONLoader(file_path, jq_schema='.[]', text_content=False) # Adjust jq_schema as needed
|
53 |
else:
|
54 |
st.warning(f"Unsupported file type: {ext}. Skipping {os.path.basename(file_path)}")
|
55 |
return None
|
|
|
134 |
return None
|
135 |
|
136 |
# --- RAG Chain Setup ---
|
137 |
+
def get_rag_chain(llm, retriever, prompt_template):
|
138 |
+
"""Creates the Retrieval QA chain."""
|
139 |
+
prompt = PromptTemplate.from_template(prompt_template)
|
140 |
+
rag_chain = (
|
141 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
142 |
+
| prompt
|
143 |
+
| llm
|
144 |
+
| StrOutputParser()
|
145 |
+
)
|
146 |
+
return rag_chain
|
147 |
|
148 |
# --- Main Application Logic ---
|
149 |
def main():
|
|
|
156 |
# Custom CSS (remains the same)
|
157 |
st.markdown("""
|
158 |
<style>
|
159 |
+
.reportview-container .main .block-container{{
|
160 |
+
padding-top: 2rem;
|
161 |
+
padding-bottom: 2rem;
|
162 |
+
}}
|
163 |
+
.st-emotion-cache-z5fcl4 {{
|
164 |
+
padding-top: 1rem;
|
165 |
+
}}
|
166 |
+
.response-area {{
|
167 |
+
background-color: #f0f2f6;
|
168 |
+
padding: 15px;
|
169 |
+
border-radius: 5px;
|
170 |
+
margin-top: 10px;
|
171 |
+
}}
|
172 |
</style>
|
173 |
""", unsafe_allow_html=True)
|
174 |
|
|
|
220 |
retriever = vector_store.as_retriever(search_kwargs={"k": 5})
|
221 |
|
222 |
# --- Query Input and Response ---
|
|
|
223 |
|
224 |
st.markdown("---")
|
225 |
st.subheader("Ask a question about our documents:")
|
|
|
250 |
Please perform the following steps:
|
251 |
1. Carefully analyze the context for any order details (Order ID, Customer Name, Status, Items, Dates, etc.).
|
252 |
2. If an order matching the query (or related to a name in the query) is found in the context:
|
253 |
+
- Address the customer by their name if available in the order details (e.g., "Hello [Customer Name],").
|
254 |
+
- Provide ALL available information about their order, including Order ID, status, items, dates, and any other relevant details found in the context.
|
255 |
+
- Be comprehensive and clear.
|
256 |
3. If no specific order details are found in the context that match the query, or if the context is insufficient, politely state that you couldn't find the specific order information in the provided documents and suggest they contact support for further assistance.
|
257 |
4. Do NOT invent or infer any information not explicitly present in the context.
|
258 |
|