File size: 2,734 Bytes
d65eab7
ef64852
d65eab7
ef64852
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests

st.set_page_config(page_title="WikiTrail", layout="wide")

st.markdown("<h1 style='text-align: center;'>πŸ“š WikiTrail</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center;'>Explore Wikipedia topics visually and get a summarized journey.</p>", unsafe_allow_html=True)

topic = st.text_input("Enter a topic", placeholder="e.g., India")

def fetch_topic_summary(topic):
    url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{topic}"
    res = requests.get(url)
    if res.status_code == 200:
        data = res.json()
        return {
            "title": data.get("title", ""),
            "summary": data.get("extract", ""),
            "image": data.get("thumbnail", {}).get("source"),
            "link": data.get("content_urls", {}).get("desktop", {}).get("page")
        }
    return None

def fetch_related_topics(topic):
    url = f"https://en.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={topic}&prop=links&pllimit=5"
    res = requests.get(url)
    if res.status_code == 200:
        data = res.json()
        pages = list(data['query']['pages'].values())
        if pages and 'links' in pages[0]:
            return [link['title'] for link in pages[0]['links']]
    return []

def simple_summary(all_summaries, limit=3):
    full_text = ' '.join(all_summaries)
    sentences = full_text.split('. ')
    return '. '.join(sentences[:limit]) + '.'

if topic:
    with st.spinner("πŸ” Searching Wikipedia..."):
        summaries = []

        st.subheader("πŸ”· Main Topic")
        main = fetch_topic_summary(topic)
        if main:
            summaries.append(main["summary"])
            st.markdown(f"### {main['title']}")
            if main["image"]:
                st.image(main["image"], width=300)
            st.write(main["summary"])
            st.markdown(f"[Read More β†’]({main['link']})", unsafe_allow_html=True)
        else:
            st.warning("Couldn't fetch data for the main topic.")

        st.subheader("πŸ”— Related Topics")
        related = fetch_related_topics(topic)
        if related:
            for rel in related:
                data = fetch_topic_summary(rel)
                if data:
                    summaries.append(data["summary"])
                    with st.expander(data["title"]):
                        if data["image"]:
                            st.image(data["image"], width=200)
                        st.write(data["summary"])
                        st.markdown(f"[Read More β†’]({data['link']})", unsafe_allow_html=True)
        else:
            st.info("No related topics found.")

        st.subheader("🧠 Combined Summary")
        st.success(simple_summary(summaries))