Update src/streamlit_app.py
Browse files- src/streamlit_app.py +18 -8
src/streamlit_app.py
CHANGED
|
@@ -6,10 +6,20 @@ st.set_page_config(page_title="WikiTrail", layout="wide")
|
|
| 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://
|
| 13 |
res = requests.get(url)
|
| 14 |
if res.status_code == 200:
|
| 15 |
data = res.json()
|
|
@@ -21,8 +31,8 @@ def fetch_topic_summary(topic):
|
|
| 21 |
}
|
| 22 |
return None
|
| 23 |
|
| 24 |
-
def fetch_related_topics(topic):
|
| 25 |
-
url = f"https://
|
| 26 |
res = requests.get(url)
|
| 27 |
if res.status_code == 200:
|
| 28 |
data = res.json()
|
|
@@ -34,14 +44,14 @@ def fetch_related_topics(topic):
|
|
| 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']}")
|
|
@@ -53,10 +63,10 @@ if topic:
|
|
| 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"]):
|
|
|
|
| 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 |
+
# Language selector
|
| 10 |
+
languages = {
|
| 11 |
+
"English": "en",
|
| 12 |
+
"Hindi (हिन्दी)": "hi",
|
| 13 |
+
"Telugu (తెలుగు)": "te",
|
| 14 |
+
"Tamil (தமிழ்)": "ta"
|
| 15 |
+
}
|
| 16 |
+
lang_name = st.selectbox("🌐 Select Language", list(languages.keys()), index=0)
|
| 17 |
+
lang_code = languages[lang_name]
|
| 18 |
+
|
| 19 |
topic = st.text_input("Enter a topic", placeholder="e.g., India")
|
| 20 |
|
| 21 |
+
def fetch_topic_summary(topic, lang):
|
| 22 |
+
url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{topic}"
|
| 23 |
res = requests.get(url)
|
| 24 |
if res.status_code == 200:
|
| 25 |
data = res.json()
|
|
|
|
| 31 |
}
|
| 32 |
return None
|
| 33 |
|
| 34 |
+
def fetch_related_topics(topic, lang):
|
| 35 |
+
url = f"https://{lang}.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={topic}&prop=links&pllimit=5"
|
| 36 |
res = requests.get(url)
|
| 37 |
if res.status_code == 200:
|
| 38 |
data = res.json()
|
|
|
|
| 44 |
def simple_summary(all_summaries, limit=3):
|
| 45 |
full_text = ' '.join(all_summaries)
|
| 46 |
sentences = full_text.split('. ')
|
| 47 |
+
return '. '.join(sentences[:limit]) + '.' if sentences else full_text
|
| 48 |
|
| 49 |
if topic:
|
| 50 |
with st.spinner("🔍 Searching Wikipedia..."):
|
| 51 |
summaries = []
|
| 52 |
|
| 53 |
st.subheader("🔷 Main Topic")
|
| 54 |
+
main = fetch_topic_summary(topic, lang_code)
|
| 55 |
if main:
|
| 56 |
summaries.append(main["summary"])
|
| 57 |
st.markdown(f"### {main['title']}")
|
|
|
|
| 63 |
st.warning("Couldn't fetch data for the main topic.")
|
| 64 |
|
| 65 |
st.subheader("🔗 Related Topics")
|
| 66 |
+
related = fetch_related_topics(topic, lang_code)
|
| 67 |
if related:
|
| 68 |
for rel in related:
|
| 69 |
+
data = fetch_topic_summary(rel, lang_code)
|
| 70 |
if data:
|
| 71 |
summaries.append(data["summary"])
|
| 72 |
with st.expander(data["title"]):
|