sathvikk commited on
Commit
ef64852
Β·
verified Β·
1 Parent(s): 6f42966

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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))