Alpha108 commited on
Commit
37ba77e
·
verified ·
1 Parent(s): 96deef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +220 -184
app.py CHANGED
@@ -39,15 +39,6 @@ def detect_and_translate_to_english(text: str) -> str:
39
 
40
 
41
  # Assume `translated_content` is your PDF or webpage content in text format (after translation)
42
- vectorstore = create_vectorstore_from_text(translated_content)
43
- rag_chain = create_rag_chain(vectorstore)
44
-
45
- # User input query
46
- query = st.text_input("Ask a question related to your uploaded content:")
47
- if query:
48
- result = rag_chain.run(query)
49
- st.success("Answer from the RAG system:")
50
- st.write(result)
51
 
52
 
53
  class GEOSEOApp:
@@ -204,8 +195,12 @@ class GEOSEOApp:
204
 
205
  # Create vector store and answer question
206
  with st.spinner("Creating embeddings and searching..."):
207
- qa_chain = self.vector_chunker.create_qa_chain(documents, self.llm)
208
- result = qa_chain({"query": user_query})
 
 
 
 
209
 
210
  # Display results
211
  st.markdown("### 💬 Answer")
@@ -225,187 +220,211 @@ class GEOSEOApp:
225
  st.error(f"An error occurred: {str(e)}")
226
 
227
  def render_content_enhancement_tab(self):
228
- """Render Content Enhancement tab"""
229
- st.header("🔧 Content Enhancement")
230
- st.markdown("Analyze and optimize your content for better AI/LLM performance.")
231
-
232
- # Content input
233
- input_text = st.text_area(
234
- "Enter content to analyze and enhance:",
235
- height=200,
236
- key="enhancement_input"
237
- )
238
-
239
- # Analysis options
240
- col1, col2 = st.columns(2)
241
- with col1:
242
- analyze_only = st.checkbox("Analysis only (no rewriting)", value=False)
243
- with col2:
244
- include_keywords = st.checkbox("Include keyword suggestions", value=True)
245
-
246
- # Submit button
247
- if st.button("🔧 Analyze & Enhance", key="enhancement_submit"):
248
- if not input_text.strip():
249
- st.warning("Please enter some content to analyze.")
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  return
251
 
252
- try:
253
- with st.spinner("Analyzing content..."):
254
- # Run content analysis and optimization
255
- result = self.content_optimizer.optimize_content(
256
- input_text,
257
- analyze_only=analyze_only,
258
- include_keywords=include_keywords
259
- )
260
-
261
- if result.get("error"):
262
- st.error(f"Analysis failed: {result['error']}")
263
- return
264
-
265
- # Display results
266
- if analyze_only:
267
- st.success("Content analysis and enhancement completed successfully!")
268
- st.markdown("### 📊 Analysis Results")
269
-
270
- # Show scores
271
- scores = result.get("scores", {})
272
- if scores:
273
- col1, col2, col3 = st.columns(3)
274
-
275
- with col1:
276
- clarity = scores.get("clarity", 0)
277
- st.metric("Clarity", f"{clarity}/10")
278
-
279
- with col2:
280
- structure = scores.get("structuredness", 0)
281
- st.metric("Structure", f"{structure}/10")
282
-
283
- with col3:
284
- answerability = scores.get("answerability", 0)
285
- st.metric("Answerability", f"{answerability}/10")
286
-
287
- # Show keywords
288
- keywords = result.get("keywords", [])
289
- if keywords:
290
- st.markdown("#### 🔑 Key Terms")
291
- st.write(", ".join(keywords))
292
-
293
- # Show optimized content
294
- optimized_text = result.get("optimized_text", "")
295
- # if optimized_text and not analyze_only:
296
- if optimized_text :
297
- st.markdown("#### ✨ Optimized Content")
298
- st.text_area(
299
- "Enhanced version:",
300
- value=optimized_text,
301
- height=200,
302
- key="optimized_output"
303
- )
304
-
305
- # Export option
306
- if st.button("📥 Export Results"):
307
- export_data = self.result_exporter.export_enhancement_results(result)
308
- st.download_button(
309
- label="Download Analysis Report",
310
- data=json.dumps(export_data, indent=2),
311
- file_name=f"content_analysis_{int(time.time())}.json",
312
- mime="application/json"
313
- )
314
 
315
- except Exception as e:
316
- st.error(f"An error occurred: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
- def render_website_analysis_tab(self):
319
- """Render Website GEO Analysis tab"""
320
- st.header("🌐 Website GEO Analysis")
321
- st.markdown("Analyze websites for Generative Engine Optimization (GEO) performance.")
322
-
323
- # URL input
324
- col1, col2 = st.columns([3, 1])
325
-
326
- with col1:
327
- website_url = st.text_input(
328
- "Enter website URL:",
329
- placeholder="https://example.com"
330
- )
331
-
332
- with col2:
333
- max_pages = st.selectbox("Pages to analyze:", [1, 3, 5], index=0)
334
-
335
- # Analysis options
336
- col1, col2 = st.columns(2)
337
- with col1:
338
- include_subpages = st.checkbox("Include subpages", value=False)
339
- with col2:
340
- detailed_analysis = st.checkbox("Detailed analysis", value=True)
341
-
342
- # Submit button
343
- if st.button("🌐 Analyze Website", key="website_analyze"):
344
- if not website_url.strip():
345
- st.warning("Please enter a website URL.")
346
- return
347
 
348
- try:
349
- # Normalize URL
350
- if not website_url.startswith(('http://', 'https://')):
351
- website_url = 'https://' + website_url
352
-
353
- with st.spinner(f"Analyzing website: {website_url}"):
354
- # Parse website content
355
- pages_data = self.webpage_parser.parse_website(
356
- website_url,
357
- max_pages=max_pages,
358
- include_subpages=include_subpages
359
- )
360
-
361
- if not pages_data:
362
- st.error("Could not extract content from the website.")
363
- return
364
-
365
- st.success(f"Successfully extracted content from {len(pages_data)} page(s)")
366
 
367
- # Analyze GEO scores
368
- with st.spinner("Calculating GEO scores..."):
369
- geo_results = []
370
-
371
- for i, page_data in enumerate(pages_data):
372
- with st.spinner(f"Analyzing page {i+1}/{len(pages_data)}..."):
373
- analysis = self.geo_scorer.analyze_page_geo(
374
- page_data['content'],
375
- page_data['title'],
376
- detailed=detailed_analysis
377
- )
378
-
379
- if not analysis.get('error'):
380
- analysis['page_data'] = page_data
381
- geo_results.append(analysis)
382
- else:
383
- st.warning(f"Could not analyze page {i+1}: {analysis['error']}")
384
-
385
- if not geo_results:
386
- st.error("Could not analyze any pages from the website.")
387
  return
388
 
389
- # Display results
390
- self.display_geo_results(geo_results, website_url)
391
-
392
- # Export functionality
393
- st.markdown("### 📥 Export Results")
394
- if st.button("📊 Generate Full Report"):
395
- report_data = self.result_exporter.export_geo_results(
396
- geo_results,
397
- website_url
398
- )
399
-
400
- st.download_button(
401
- label="Download GEO Report",
402
- data=json.dumps(report_data, indent=2),
403
- file_name=f"geo_analysis_{website_url.replace('https://', '').replace('/', '_')}.json",
404
- mime="application/json"
405
- )
406
 
407
- except Exception as e:
408
- st.error(f"An error occurred during website analysis: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
 
410
  def render_multilingual_tab(self):
411
  st.markdown("### 🌍 Multilingual Translator")
@@ -560,11 +579,28 @@ class GEOSEOApp:
560
  return tmp_file.name
561
 
562
 
 
563
  def main():
564
  """Main entry point"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
  app = GEOSEOApp()
566
  app.run()
567
 
568
 
569
  if __name__ == "__main__":
570
- main()
 
39
 
40
 
41
  # Assume `translated_content` is your PDF or webpage content in text format (after translation)
 
 
 
 
 
 
 
 
 
42
 
43
 
44
  class GEOSEOApp:
 
195
 
196
  # Create vector store and answer question
197
  with st.spinner("Creating embeddings and searching..."):
198
+ # Create new vectorstore and update RAG
199
+ vectorstore = create_vectorstore_from_text(documents, self.embeddings)
200
+ st.session_state.rag_chain = create_rag_chain(self.llm, vectorstore)
201
+
202
+ result = st.session_state.rag_chain.invoke({"question": user_query})
203
+
204
 
205
  # Display results
206
  st.markdown("### 💬 Answer")
 
220
  st.error(f"An error occurred: {str(e)}")
221
 
222
  def render_content_enhancement_tab(self):
223
+ """Render Content Enhancement tab"""
224
+ st.header("🔧 Content Enhancement")
225
+ st.markdown("Analyze and optimize your content for better AI/LLM performance.")
226
+
227
+ # Content input
228
+ input_text = st.text_area(
229
+ "Enter content to analyze and enhance:",
230
+ height=200,
231
+ key="enhancement_input"
232
+ )
233
+
234
+ # Analysis options
235
+ col1, col2 = st.columns(2)
236
+ with col1:
237
+ analyze_only = st.checkbox("Analysis only (no rewriting)", value=False)
238
+ with col2:
239
+ include_keywords = st.checkbox("Include keyword suggestions", value=True)
240
+
241
+ # Submit button
242
+ if st.button("🔧 Analyze & Enhance", key="enhancement_submit"):
243
+ if not input_text.strip():
244
+ st.warning("Please enter some content to analyze.")
245
+ return
246
+
247
+ try:
248
+ with st.spinner("Analyzing content..."):
249
+ # Run content analysis and optimization
250
+ result = self.content_optimizer.optimize_content(
251
+ input_text,
252
+ analyze_only=analyze_only,
253
+ include_keywords=include_keywords
254
+ )
255
+
256
+ if result.get("error"):
257
+ st.error(f"Analysis failed: {result['error']}")
258
  return
259
 
260
+ # Display results
261
+ if analyze_only:
262
+ st.success("Content analysis completed successfully!")
263
+ st.markdown("### 📊 Analysis Results")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
+ # Show scores
266
+ scores = result.get("scores", {})
267
+ if scores:
268
+ col1, col2, col3 = st.columns(3)
269
+
270
+ with col1:
271
+ clarity = scores.get("clarity", 0)
272
+ st.metric("Clarity", f"{clarity}/10")
273
+
274
+ with col2:
275
+ structure = scores.get("structuredness", 0)
276
+ st.metric("Structure", f"{structure}/10")
277
+
278
+ with col3:
279
+ answerability = scores.get("answerability", 0)
280
+ st.metric("Answerability", f"{answerability}/10")
281
+
282
+ # Show keywords
283
+ keywords = result.get("keywords", [])
284
+ if keywords:
285
+ st.markdown("#### 🔑 Key Terms")
286
+ st.write(", ".join(keywords))
287
+
288
+ # Show optimized content
289
+ optimized_text = result.get("optimized_text", "")
290
+ if optimized_text:
291
+ st.markdown("#### ✨ Optimized Content")
292
+ st.text_area(
293
+ "Enhanced version:",
294
+ value=optimized_text,
295
+ height=200,
296
+ key="optimized_output"
297
+ )
298
+
299
+ # ✅ Optional RAG-based Q&A on the analyzed content
300
+ st.markdown("### 💬 Ask a question about the analyzed content:")
301
+ user_query = st.text_input("Enter your question:", key="enhancement_q")
302
+
303
+ if user_query:
304
+ from langchain.docstore.document import Document
305
+ new_doc = Document(page_content=optimized_text or input_text)
306
+ vectorstore = create_vectorstore_from_text([new_doc], self.embeddings)
307
+ st.session_state.rag_chain = create_rag_chain(self.llm, vectorstore)
308
+
309
+ result = st.session_state.rag_chain.invoke({"question": user_query})
310
+ st.success("Answer:")
311
+ st.write(result["result"])
312
+
313
+ # Export option
314
+ if st.button("📥 Export Results"):
315
+ export_data = self.result_exporter.export_enhancement_results(result)
316
+ st.download_button(
317
+ label="Download Analysis Report",
318
+ data=json.dumps(export_data, indent=2),
319
+ file_name=f"content_analysis_{int(time.time())}.json",
320
+ mime="application/json"
321
+ )
322
+
323
+ except Exception as e:
324
+ st.error(f"An error occurred: {str(e)}")
325
+
326
 
327
+ def render_website_analysis_tab(self):
328
+ """Render Website GEO Analysis tab"""
329
+ st.header("🌐 Website GEO Analysis")
330
+ st.markdown("Analyze websites for Generative Engine Optimization (GEO) performance.")
331
+
332
+ # URL input
333
+ col1, col2 = st.columns([3, 1])
334
+ with col1:
335
+ website_url = st.text_input("Enter website URL:", placeholder="https://example.com")
336
+ with col2:
337
+ max_pages = st.selectbox("Pages to analyze:", [1, 3, 5], index=0)
338
+
339
+ # Analysis options
340
+ col1, col2 = st.columns(2)
341
+ with col1:
342
+ include_subpages = st.checkbox("Include subpages", value=False)
343
+ with col2:
344
+ detailed_analysis = st.checkbox("Detailed analysis", value=True)
345
+
346
+ # Submit button
347
+ if st.button("🌐 Analyze Website", key="website_analyze"):
348
+ if not website_url.strip():
349
+ st.warning("Please enter a website URL.")
350
+ return
351
+
352
+ try:
353
+ # Normalize URL
354
+ if not website_url.startswith(('http://', 'https://')):
355
+ website_url = 'https://' + website_url
356
 
357
+ with st.spinner(f"Analyzing website: {website_url}"):
358
+ # Parse website content
359
+ pages_data = self.webpage_parser.parse_website(
360
+ website_url,
361
+ max_pages=max_pages,
362
+ include_subpages=include_subpages
363
+ )
 
 
 
 
 
 
 
 
 
 
 
364
 
365
+ if not pages_data:
366
+ st.error("Could not extract content from the website.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
  return
368
 
369
+ st.success(f"Successfully extracted content from {len(pages_data)} page(s)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
+ # Analyze GEO scores
372
+ with st.spinner("Calculating GEO scores..."):
373
+ geo_results = []
374
+ for i, page_data in enumerate(pages_data):
375
+ with st.spinner(f"Analyzing page {i+1}/{len(pages_data)}..."):
376
+ analysis = self.geo_scorer.analyze_page_geo(
377
+ page_data['content'],
378
+ page_data['title'],
379
+ detailed=detailed_analysis
380
+ )
381
+
382
+ if not analysis.get('error'):
383
+ analysis['page_data'] = page_data
384
+ geo_results.append(analysis)
385
+ else:
386
+ st.warning(f"Could not analyze page {i+1}: {analysis['error']}")
387
+
388
+ if not geo_results:
389
+ st.error("Could not analyze any pages from the website.")
390
+ return
391
+
392
+ # Combine all page content for RAG
393
+ combined_content = "\n\n".join([page['content'] for page in pages_data])
394
+ from langchain.docstore.document import Document
395
+ doc = Document(page_content=combined_content)
396
+
397
+ vectorstore = create_vectorstore_from_text([doc], self.embeddings)
398
+ st.session_state.rag_chain = create_rag_chain(self.llm, vectorstore)
399
+
400
+ # RAG-based Q&A
401
+ st.markdown("### 💬 Ask a question about the website:")
402
+ user_query = st.text_input("Ask here:", key="website_q")
403
+
404
+ if user_query:
405
+ result = st.session_state.rag_chain.invoke({"question": user_query})
406
+ st.success("Answer:")
407
+ st.write(result["result"])
408
+
409
+ # Display results
410
+ self.display_geo_results(geo_results, website_url)
411
+
412
+ # Export functionality
413
+ st.markdown("### 📥 Export Results")
414
+ if st.button("📊 Generate Full Report"):
415
+ report_data = self.result_exporter.export_geo_results(
416
+ geo_results,
417
+ website_url
418
+ )
419
+ st.download_button(
420
+ label="Download GEO Report",
421
+ data=json.dumps(report_data, indent=2),
422
+ file_name=f"geo_analysis_{website_url.replace('https://', '').replace('/', '_')}.json",
423
+ mime="application/json"
424
+ )
425
+
426
+ except Exception as e:
427
+ st.error(f"An error occurred during website analysis: {str(e)}")
428
 
429
  def render_multilingual_tab(self):
430
  st.markdown("### 🌍 Multilingual Translator")
 
579
  return tmp_file.name
580
 
581
 
582
+
583
  def main():
584
  """Main entry point"""
585
+ if "rag_chain" not in st.session_state or st.session_state.rag_chain is None:
586
+ # Sample base docs to initialize chain (you can also load a default corpus or skip this)
587
+ from langchain.docstore.document import Document
588
+ base_docs = [Document(page_content="This is a placeholder document for RAG initialization.")]
589
+
590
+ vectorstore = create_vectorstore_from_text(base_docs, HuggingFaceEmbeddings(
591
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
592
+ model_kwargs={"device": "cpu"},
593
+ cache_folder="./hf_cache",
594
+ ))
595
+ st.session_state.rag_chain = create_rag_chain(
596
+ ChatGroq(api_key=os.getenv("GROQ_API_KEY", "your-groq-api-key"), model_name="llama3-8b-8192", temperature=0.1),
597
+ vectorstore
598
+ )
599
+
600
+
601
  app = GEOSEOApp()
602
  app.run()
603
 
604
 
605
  if __name__ == "__main__":
606
+ main()