MusaR commited on
Commit
6eb665c
·
verified ·
1 Parent(s): 9e52c49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -10
app.py CHANGED
@@ -1,12 +1,129 @@
1
- print("--- app.py: TOP OF FILE ---")
 
 
 
2
  import streamlit as st
3
- print("--- app.py: Streamlit imported ---")
4
  import os
5
- print("--- app.py: os imported ---")
6
-
7
- st.set_page_config(page_title="Clean Slate Test", page_icon="🧼")
8
- st.title("🧼 Clean Slate Test App")
9
- st.write("If you see this, the basic Streamlit environment on a FRESH SPACE is working.")
10
- print("--- app.py: Streamlit UI rendered ---")
11
- print(f"CWD: {os.getcwd()}")
12
- print("--- app.py: END OF FILE ---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py (DEBUGGING VERSION)
2
+
3
+ print("--- Python script starting ---")
4
+
5
  import streamlit as st
 
6
  import os
7
+ from dotenv import load_dotenv
8
+ from pinecone import Pinecone
9
+
10
+ # --- Standard Imports ---
11
+ from langchain_pinecone import PineconeVectorStore
12
+ from langchain_community.embeddings import SentenceTransformerEmbeddings
13
+ from langchain_groq import ChatGroq
14
+ from langchain_core.prompts import PromptTemplate
15
+ from langchain_core.runnables import RunnablePassthrough
16
+ from langchain_core.output_parsers import PydanticOutputParser
17
+ from pydantic import BaseModel, Field
18
+ from langchain.retrievers import ContextualCompressionRetriever
19
+ from langchain.retrievers.document_compressors import CohereRerank
20
+
21
+ print("--- All imports successful ---")
22
+
23
+ # We wrap the ENTIRE app in a try/except block to catch any startup error
24
+ try:
25
+ # --- Load Environment Variables ---
26
+ print("Step 1: Loading environment variables...")
27
+ load_dotenv()
28
+ PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
29
+ GROQ_API_KEY = os.getenv('GROQ_API_KEY')
30
+ COHERE_API_KEY = os.getenv('COHERE_API_KEY')
31
+ INDEX_NAME = "rag-chatbot"
32
+ print("Step 1: SUCCESS")
33
+
34
+ # --- Page Configuration ---
35
+ st.set_page_config(page_title="Production RAG System", page_icon="🚀", layout="wide")
36
+ st.title("🚀 Production-Grade RAG System")
37
+
38
+ # --- Pydantic Model ---
39
+ class StructuredAnswer(BaseModel):
40
+ summary: str = Field(description="A concise summary.")
41
+ key_points: list[str] = Field(description="A list of key bullet points.")
42
+ confidence_score: float = Field(description="A 0.0 to 1.0 confidence score.")
43
+
44
+ # --- Caching and Initialization ---
45
+ @st.cache_resource
46
+ def initialize_services():
47
+ print("Step 2: Entering initialize_services function...")
48
+ if not all([PINECONE_API_KEY, GROQ_API_KEY, COHERE_API_KEY]):
49
+ raise ValueError("An API key is missing!")
50
+
51
+ print("Step 2a: Initializing embedding model...")
52
+ embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
53
+ print("Step 2a: SUCCESS")
54
+
55
+ print("Step 2b: Initializing Pinecone client...")
56
+ pinecone = Pinecone(api_key=PINECONE_API_KEY)
57
+ host = "https://rag-chatbot-sg8t88c.svc.aped-4627-b74a.pinecone.io"
58
+ index = pinecone.Index(host=host)
59
+ print("Step 2b: SUCCESS")
60
+
61
+ print("Step 2c: Creating PineconeVectorStore object...")
62
+ vectorstore = PineconeVectorStore(index=index, embedding=embeddings)
63
+ print("Step 2c: SUCCESS")
64
+
65
+ print("Step 2d: Initializing Cohere Re-ranker...")
66
+ base_retriever = vectorstore.as_retriever(search_kwargs={'k': 20})
67
+ compressor = CohereRerank(cohere_api_key=COHERE_API_KEY, top_n=5)
68
+ reranking_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=base_retriever)
69
+ print("Step 2d: SUCCESS")
70
+
71
+ print("Step 2e: Initializing Groq LLM...")
72
+ llm = ChatGroq(temperature=0, model_name="llama3-70b-8192", api_key=GROQ_API_KEY)
73
+ print("Step 2e: SUCCESS")
74
+
75
+ print("Step 2: All services initialized successfully.")
76
+ return reranking_retriever, llm
77
+
78
+ print("Step 3: Calling initialize_services...")
79
+ retriever, llm = initialize_services()
80
+ print("Step 3: SUCCESS, services are loaded.")
81
+
82
+ # --- RAG Chain Definition ---
83
+ print("Step 4: Defining RAG chain...")
84
+ pydantic_parser = PydanticOutputParser(pydantic_object=StructuredAnswer)
85
+ format_instructions = pydantic_parser.get_format_instructions()
86
+ template = """
87
+ You are a world-class analysis engine. Your task is to provide a structured, factual answer based *only* on the following context.
88
+ Synthesize the information from all context snippets. Do not use any outside knowledge.
89
+
90
+ Context:
91
+ {context}
92
+
93
+ Question:
94
+ {question}
95
+
96
+ Follow these formatting instructions precisely:
97
+ {format_instructions}
98
+ """
99
+ prompt = PromptTemplate(
100
+ template=template,
101
+ input_variables=["context", "question"],
102
+ partial_variables={"format_instructions": format_instructions}
103
+ )
104
+ rag_chain = (
105
+ {"context": retriever, "question": RunnablePassthrough()}
106
+ | prompt
107
+ | llm
108
+ | pydantic_parser
109
+ )
110
+ print("Step 4: SUCCESS")
111
+
112
+ # --- UI Rendering ---
113
+ print("Step 5: Starting to render Streamlit UI...")
114
+ st.success("System is ready. Ask your question below.")
115
+ query = st.text_input("Enter your question:", key="query_input")
116
+
117
+ if query:
118
+ with st.spinner("Processing..."):
119
+ structured_answer = rag_chain.invoke(query)
120
+ st.write("### Answer")
121
+ # ... rest of UI ...
122
+ print("Step 5: SUCCESS, UI is rendered.")
123
+
124
+ except Exception as e:
125
+ # If ANY error happens during startup, it will be printed here
126
+ print(f"!!!!!!!!!! A FATAL ERROR OCCURRED !!!!!!!!!!")
127
+ import traceback
128
+ print(traceback.format_exc())
129
+ st.error(f"A fatal error occurred during startup. Please check the container logs. Error: {e}")