Update src/streamlit_app.py
Browse files- src/streamlit_app.py +69 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,71 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
|
4 |
+
st.set_page_config(page_title="WikiTrail", layout="wide")
|
5 |
+
|
6 |
+
st.markdown("<h1 style='text-align: center;'>π WikiTrail</h1>", unsafe_allow_html=True)
|
7 |
+
st.markdown("<p style='text-align: center;'>Explore Wikipedia topics visually and get a summarized journey.</p>", unsafe_allow_html=True)
|
8 |
+
|
9 |
+
topic = st.text_input("Enter a topic", placeholder="e.g., India")
|
10 |
+
|
11 |
+
def fetch_topic_summary(topic):
|
12 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{topic}"
|
13 |
+
res = requests.get(url)
|
14 |
+
if res.status_code == 200:
|
15 |
+
data = res.json()
|
16 |
+
return {
|
17 |
+
"title": data.get("title", ""),
|
18 |
+
"summary": data.get("extract", ""),
|
19 |
+
"image": data.get("thumbnail", {}).get("source"),
|
20 |
+
"link": data.get("content_urls", {}).get("desktop", {}).get("page")
|
21 |
+
}
|
22 |
+
return None
|
23 |
+
|
24 |
+
def fetch_related_topics(topic):
|
25 |
+
url = f"https://en.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={topic}&prop=links&pllimit=5"
|
26 |
+
res = requests.get(url)
|
27 |
+
if res.status_code == 200:
|
28 |
+
data = res.json()
|
29 |
+
pages = list(data['query']['pages'].values())
|
30 |
+
if pages and 'links' in pages[0]:
|
31 |
+
return [link['title'] for link in pages[0]['links']]
|
32 |
+
return []
|
33 |
+
|
34 |
+
def simple_summary(all_summaries, limit=3):
|
35 |
+
full_text = ' '.join(all_summaries)
|
36 |
+
sentences = full_text.split('. ')
|
37 |
+
return '. '.join(sentences[:limit]) + '.'
|
38 |
+
|
39 |
+
if topic:
|
40 |
+
with st.spinner("π Searching Wikipedia..."):
|
41 |
+
summaries = []
|
42 |
+
|
43 |
+
st.subheader("π· Main Topic")
|
44 |
+
main = fetch_topic_summary(topic)
|
45 |
+
if main:
|
46 |
+
summaries.append(main["summary"])
|
47 |
+
st.markdown(f"### {main['title']}")
|
48 |
+
if main["image"]:
|
49 |
+
st.image(main["image"], width=300)
|
50 |
+
st.write(main["summary"])
|
51 |
+
st.markdown(f"[Read More β]({main['link']})", unsafe_allow_html=True)
|
52 |
+
else:
|
53 |
+
st.warning("Couldn't fetch data for the main topic.")
|
54 |
+
|
55 |
+
st.subheader("π Related Topics")
|
56 |
+
related = fetch_related_topics(topic)
|
57 |
+
if related:
|
58 |
+
for rel in related:
|
59 |
+
data = fetch_topic_summary(rel)
|
60 |
+
if data:
|
61 |
+
summaries.append(data["summary"])
|
62 |
+
with st.expander(data["title"]):
|
63 |
+
if data["image"]:
|
64 |
+
st.image(data["image"], width=200)
|
65 |
+
st.write(data["summary"])
|
66 |
+
st.markdown(f"[Read More β]({data['link']})", unsafe_allow_html=True)
|
67 |
+
else:
|
68 |
+
st.info("No related topics found.")
|
69 |
+
|
70 |
+
st.subheader("π§ Combined Summary")
|
71 |
+
st.success(simple_summary(summaries))
|