Spaces:
Sleeping
Sleeping
File size: 4,243 Bytes
582bf6b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# modules/info_page.py
import streamlit as st
from typing import Dict, List, Optional
from modules.api_utils import fetch_wikipedia_page_details, fetch_wikipedia_images
from io import BytesIO
import base64
import re # Import regex module
def display_page_info(topic: str, num_images: int = 1):
"""Displays detailed information and images for a given topic."""
st.subheader(f"Information on: {topic}")
page_details = fetch_wikipedia_page_details(topic)
if page_details:
st.markdown(f"### {page_details.get('title', topic)}")
with st.expander("Read Full Information", expanded=True):
extract = page_details.get("extract", "No detailed information found.")
# Attempt to split into paragraphs and identify potential list items
lines = extract.split('\n')
formatted_content = []
for line in lines:
line = line.strip()
if not line:
continue # Skip empty lines
# Heuristic for bullet points/list items
if re.match(r'^[\*\-\d]+\s', line): # Starts with *, -, or digit followed by space
formatted_content.append(f"* {line.lstrip('*- ')}") # Format as markdown list
elif line.endswith(':') or line.endswith('.'): # Likely a full sentence/paragraph
formatted_content.append(line)
else: # Treat as part of previous paragraph or new paragraph
if formatted_content and not formatted_content[-1].startswith('*'): # If previous was not a list item
formatted_content[-1] += " " + line # Append to last paragraph
else:
formatted_content.append(line) # New paragraph
# Join with appropriate spacing for markdown rendering
final_markdown = ""
for item in formatted_content:
if item.startswith('*'):
final_markdown += f"{item}\n"
else:
final_markdown += f"{item}\n\n"
st.markdown(final_markdown.strip()) # Render as markdown
st.markdown("---") # Separator
# Display images
if num_images > 0:
st.markdown("#### Images")
images_found = fetch_wikipedia_images(topic, limit=num_images)
if images_found:
# Use st.columns to display images in a grid
cols = st.columns(min(num_images, len(images_found))) # Adjust columns based on actual images found
for i, img_url in enumerate(images_found):
with cols[i]:
try:
st.image(img_url, caption=f"Image {i+1}", use_column_width=True)
except Exception as e:
st.warning(f"Could not load image {i+1}: {e}")
else:
st.info("No relevant images found for this topic.")
st.markdown("---") # Separator
# Download options
st.markdown("#### Download Information")
text_content = f"Topic: {page_details.get('title', topic)}\n\n{page_details.get('extract', '')}"
# Download as TXT
st.download_button(
label="Download as TXT",
data=text_content,
file_name=f"{topic}_info.txt",
mime="text/plain",
key="download_txt"
)
# Download as Markdown (simple version)
md_content = f"# {page_details.get('title', topic)}\n\n{page_details.get('extract', '')}"
if images_found:
for img_url in images_found:
md_content += f"\n\n"
st.download_button(
label="Download as Markdown",
data=md_content,
file_name=f"{topic}_info.md",
mime="text/markdown",
key="download_md"
)
else:
st.error(f"Could not retrieve detailed information for '{topic}'. Please try a different topic.")
|