Shivamsinghtomar78 commited on
Commit
8e37b2e
Β·
verified Β·
1 Parent(s): 084e167

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -27
app.py CHANGED
@@ -17,23 +17,17 @@ from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
17
  from langchain_community.vectorstores import FAISS
18
  from langchain_text_splitters import RecursiveCharacterTextSplitter
19
 
20
- # ======================
21
- # SECRETS CONFIGURATION
22
- # ======================
23
- # Get API keys from Hugging Face Secrets
24
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
25
  HUGGINGFACE_ACCESS_TOKEN = os.environ.get("HUGGINGFACE_ACCESS_TOKEN")
26
 
27
- # Validate required secrets
28
  if not GOOGLE_API_KEY:
29
- st.error("❌ GOOGLE_API_KEY not found. Please set it in Space Settings > Secrets.")
30
  st.stop()
31
 
32
  if not HUGGINGFACE_ACCESS_TOKEN:
33
- st.error("❌ HUGGINGFACE_ACCESS_TOKEN not found. Please set it in Space Settings > Secrets.")
34
  st.stop()
35
 
36
- # Initialize LLM and embeddings with secrets
37
  llm = ChatGoogleGenerativeAI(
38
  model="gemini-1.5-pro",
39
  google_api_key=GOOGLE_API_KEY
@@ -44,9 +38,6 @@ embeddings = HuggingFaceInferenceAPIEmbeddings(
44
  model_name="BAAI/bge-small-en-v1.5"
45
  )
46
 
47
- # ======================
48
- # DOCUMENT ANALYSIS CLASSES
49
- # ======================
50
  class KeyPoint(BaseModel):
51
  point: str = Field(description="A key point extracted from the document.")
52
 
@@ -57,9 +48,6 @@ class DocumentAnalysis(BaseModel):
57
  key_points: List[KeyPoint] = Field(description="List of key points from the document.")
58
  summary: Summary = Field(description="Summary of the document.")
59
 
60
- # ======================
61
- # CHAIN SETUP
62
- # ======================
63
  parser = PydanticOutputParser(pydantic_object=DocumentAnalysis)
64
 
65
  prompt_template = """
@@ -75,9 +63,6 @@ prompt = PromptTemplate(
75
 
76
  chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser)
77
 
78
- # ======================
79
- # UTILITY FUNCTIONS
80
- # ======================
81
  def analyze_text_structured(text):
82
  return chain.run(text=text)
83
 
@@ -99,7 +84,7 @@ def create_pdf_report(analysis):
99
  pdf.cell(200, 10, txt="PDF Analysis Report", ln=True, align='C')
100
  pdf.cell(200, 10, txt=f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
101
  pdf.multi_cell(0, 10, txt=json_to_text(analysis))
102
- return pdf.output(dest='S')
103
 
104
  def create_word_report(analysis):
105
  doc = Document()
@@ -112,9 +97,6 @@ def create_word_report(analysis):
112
  docx_bytes.seek(0)
113
  return docx_bytes.getvalue()
114
 
115
- # ======================
116
- # STREAMLIT UI
117
- # ======================
118
  st.set_page_config(page_title="Chat With PDF", page_icon="πŸ“„")
119
 
120
  def local_css():
@@ -201,7 +183,6 @@ def local_css():
201
 
202
  local_css()
203
 
204
- # Initialize session state
205
  if "current_file" not in st.session_state:
206
  st.session_state.current_file = None
207
  if "pdf_summary" not in st.session_state:
@@ -217,14 +198,12 @@ if "vectorstore" not in st.session_state:
217
  if "messages" not in st.session_state:
218
  st.session_state.messages = []
219
 
220
- # UI Components
221
  st.markdown('<div class="main-header">', unsafe_allow_html=True)
222
  st.markdown('<div class="flag-stripe"></div>', unsafe_allow_html=True)
223
  st.title("πŸ“„ Chat With PDF")
224
  st.caption("Your AI-powered Document Analyzer")
225
  st.markdown('</div>', unsafe_allow_html=True)
226
 
227
- # File Uploader
228
  with st.container():
229
  st.markdown('<div class="card animate-fadeIn">', unsafe_allow_html=True)
230
  uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
@@ -274,8 +253,7 @@ with st.container():
274
  )
275
  st.markdown('</div>', unsafe_allow_html=True)
276
 
277
- # Chat Interface
278
- if "vectorstore" in st.session_state:
279
  st.subheader("Chat with the Document")
280
 
281
  for message in st.session_state.messages:
@@ -303,7 +281,6 @@ if "vectorstore" in st.session_state:
303
 
304
  st.session_state.messages.append({"role": "assistant", "content": response.content})
305
 
306
- # Footer
307
  st.markdown(
308
  f'<div class="footer">Analysis Time: {st.session_state.analysis_time:.1f}s | Powered by Google Generative AI</div>',
309
  unsafe_allow_html=True
 
17
  from langchain_community.vectorstores import FAISS
18
  from langchain_text_splitters import RecursiveCharacterTextSplitter
19
 
 
 
 
 
20
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
21
  HUGGINGFACE_ACCESS_TOKEN = os.environ.get("HUGGINGFACE_ACCESS_TOKEN")
22
 
 
23
  if not GOOGLE_API_KEY:
24
+ st.error("❌ GOOGLE_API_KEY not found.")
25
  st.stop()
26
 
27
  if not HUGGINGFACE_ACCESS_TOKEN:
28
+ st.error("❌ HUGGINGFACE_ACCESS_TOKEN not found.")
29
  st.stop()
30
 
 
31
  llm = ChatGoogleGenerativeAI(
32
  model="gemini-1.5-pro",
33
  google_api_key=GOOGLE_API_KEY
 
38
  model_name="BAAI/bge-small-en-v1.5"
39
  )
40
 
 
 
 
41
  class KeyPoint(BaseModel):
42
  point: str = Field(description="A key point extracted from the document.")
43
 
 
48
  key_points: List[KeyPoint] = Field(description="List of key points from the document.")
49
  summary: Summary = Field(description="Summary of the document.")
50
 
 
 
 
51
  parser = PydanticOutputParser(pydantic_object=DocumentAnalysis)
52
 
53
  prompt_template = """
 
63
 
64
  chain = LLMChain(llm=llm, prompt=prompt, output_parser=parser)
65
 
 
 
 
66
  def analyze_text_structured(text):
67
  return chain.run(text=text)
68
 
 
84
  pdf.cell(200, 10, txt="PDF Analysis Report", ln=True, align='C')
85
  pdf.cell(200, 10, txt=f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
86
  pdf.multi_cell(0, 10, txt=json_to_text(analysis))
87
+ return bytes(pdf.output(dest='S').encode('latin-1'))
88
 
89
  def create_word_report(analysis):
90
  doc = Document()
 
97
  docx_bytes.seek(0)
98
  return docx_bytes.getvalue()
99
 
 
 
 
100
  st.set_page_config(page_title="Chat With PDF", page_icon="πŸ“„")
101
 
102
  def local_css():
 
183
 
184
  local_css()
185
 
 
186
  if "current_file" not in st.session_state:
187
  st.session_state.current_file = None
188
  if "pdf_summary" not in st.session_state:
 
198
  if "messages" not in st.session_state:
199
  st.session_state.messages = []
200
 
 
201
  st.markdown('<div class="main-header">', unsafe_allow_html=True)
202
  st.markdown('<div class="flag-stripe"></div>', unsafe_allow_html=True)
203
  st.title("πŸ“„ Chat With PDF")
204
  st.caption("Your AI-powered Document Analyzer")
205
  st.markdown('</div>', unsafe_allow_html=True)
206
 
 
207
  with st.container():
208
  st.markdown('<div class="card animate-fadeIn">', unsafe_allow_html=True)
209
  uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
 
253
  )
254
  st.markdown('</div>', unsafe_allow_html=True)
255
 
256
+ if "vectorstore" in st.session_state and st.session_state.vectorstore is not None:
 
257
  st.subheader("Chat with the Document")
258
 
259
  for message in st.session_state.messages:
 
281
 
282
  st.session_state.messages.append({"role": "assistant", "content": response.content})
283
 
 
284
  st.markdown(
285
  f'<div class="footer">Analysis Time: {st.session_state.analysis_time:.1f}s | Powered by Google Generative AI</div>',
286
  unsafe_allow_html=True