Update src/streamlit_app.py
Browse files- src/streamlit_app.py +33 -31
src/streamlit_app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ["HOME"] = "/tmp" # Hugging Face
|
| 3 |
|
| 4 |
import streamlit as st
|
| 5 |
import requests
|
|
@@ -10,7 +10,7 @@ st.set_page_config(page_title="WikiTrail", layout="wide")
|
|
| 10 |
st.title("π WikiTrail")
|
| 11 |
st.markdown("Explore Wikipedia topics visually and get a summarized journey.")
|
| 12 |
|
| 13 |
-
# Language
|
| 14 |
languages = {
|
| 15 |
"English": "en",
|
| 16 |
"Hindi (ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦ΰ₯)": "hi",
|
|
@@ -20,10 +20,11 @@ languages = {
|
|
| 20 |
lang_name = st.selectbox("π Select Language", list(languages.keys()))
|
| 21 |
lang_code = languages[lang_name]
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
topic_input = st.text_input("π Enter a topic (in English)", placeholder="e.g., India,
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
def get_translated_title(query, lang):
|
| 28 |
search_url = f"https://{lang}.wikipedia.org/w/api.php"
|
| 29 |
params = {
|
|
@@ -33,15 +34,20 @@ def get_translated_title(query, lang):
|
|
| 33 |
"format": "json",
|
| 34 |
"origin": "*"
|
| 35 |
}
|
| 36 |
-
res = requests.get(search_url, params=params)
|
| 37 |
-
if res.status_code == 200:
|
| 38 |
-
results = res.json()
|
| 39 |
-
search_list = results.get("query", {}).get("search", [])
|
| 40 |
-
if search_list:
|
| 41 |
-
return search_list[0]["title"]
|
| 42 |
-
return None
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def fetch_summary(title, lang):
|
| 46 |
safe_title = urllib.parse.quote(title.replace(" ", "_"))
|
| 47 |
url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{safe_title}"
|
|
@@ -55,7 +61,7 @@ def fetch_summary(title, lang):
|
|
| 55 |
}
|
| 56 |
return None
|
| 57 |
|
| 58 |
-
#
|
| 59 |
def fetch_related(title, lang):
|
| 60 |
safe_title = urllib.parse.quote(title.replace(" ", "_"))
|
| 61 |
url = f"https://{lang}.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={safe_title}&prop=links&pllimit=5"
|
|
@@ -67,38 +73,35 @@ def fetch_related(title, lang):
|
|
| 67 |
return [link['title'] for link in pages[0]['links']]
|
| 68 |
return []
|
| 69 |
|
| 70 |
-
#
|
| 71 |
def summarize_bullets(texts, limit=3):
|
| 72 |
full_text = ' '.join(set(texts))
|
| 73 |
sentences = full_text.replace('ΰ₯€', '.').replace('?', '.').replace('!', '.').split('.')
|
| 74 |
clean = [s.strip() for s in sentences if s.strip()]
|
| 75 |
return ["β’ " + s + "." for s in clean[:limit]] if clean else ["No summary available."]
|
| 76 |
|
| 77 |
-
# Main logic
|
| 78 |
if topic_input:
|
| 79 |
with st.spinner("π Searching Wikipedia..."):
|
| 80 |
summaries = []
|
| 81 |
final_text = ""
|
| 82 |
|
| 83 |
translated_title = get_translated_title(topic_input, lang_code)
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
st.error(f"No matching page found in {lang_name} for '{topic_input}'")
|
| 86 |
st.stop()
|
| 87 |
|
| 88 |
-
# Main topic
|
| 89 |
st.subheader("π· Main Topic")
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
st.write(main["summary"])
|
| 97 |
-
st.markdown(f"[Read More β]({main['link']})", unsafe_allow_html=True)
|
| 98 |
-
else:
|
| 99 |
-
st.warning("Couldn't fetch main topic summary.")
|
| 100 |
|
| 101 |
-
# Related
|
| 102 |
st.subheader("π Related Topics")
|
| 103 |
related_titles = fetch_related(translated_title, lang_code)
|
| 104 |
if related_titles:
|
|
@@ -113,12 +116,11 @@ if topic_input:
|
|
| 113 |
else:
|
| 114 |
st.info("No related topics found.")
|
| 115 |
|
| 116 |
-
# Bullet summary
|
| 117 |
st.subheader("π§ Combined Summary")
|
| 118 |
for bullet in summarize_bullets(summaries):
|
| 119 |
st.markdown(bullet)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
st.download_button(
|
| 123 |
label="π₯ Download Summary as TXT",
|
| 124 |
data=final_text,
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["HOME"] = "/tmp" # Fix for Hugging Face Spaces
|
| 3 |
|
| 4 |
import streamlit as st
|
| 5 |
import requests
|
|
|
|
| 10 |
st.title("π WikiTrail")
|
| 11 |
st.markdown("Explore Wikipedia topics visually and get a summarized journey.")
|
| 12 |
|
| 13 |
+
# Language options
|
| 14 |
languages = {
|
| 15 |
"English": "en",
|
| 16 |
"Hindi (ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦ΰ₯)": "hi",
|
|
|
|
| 20 |
lang_name = st.selectbox("π Select Language", list(languages.keys()))
|
| 21 |
lang_code = languages[lang_name]
|
| 22 |
|
| 23 |
+
# Input
|
| 24 |
+
topic_input = st.text_input("π Enter a topic (in English)", placeholder="e.g., India, Telangana, Mahatma Gandhi")
|
| 25 |
+
topic_input = topic_input.strip()
|
| 26 |
|
| 27 |
+
# π Fix: Get best matching title or fallback
|
| 28 |
def get_translated_title(query, lang):
|
| 29 |
search_url = f"https://{lang}.wikipedia.org/w/api.php"
|
| 30 |
params = {
|
|
|
|
| 34 |
"format": "json",
|
| 35 |
"origin": "*"
|
| 36 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
res = requests.get(search_url, params=params)
|
| 40 |
+
res.raise_for_status()
|
| 41 |
+
data = res.json()
|
| 42 |
+
search_results = data.get("query", {}).get("search", [])
|
| 43 |
+
|
| 44 |
+
if search_results:
|
| 45 |
+
return search_results[0]["title"]
|
| 46 |
+
return query # fallback
|
| 47 |
+
except:
|
| 48 |
+
return query # fallback
|
| 49 |
+
|
| 50 |
+
# Summary API
|
| 51 |
def fetch_summary(title, lang):
|
| 52 |
safe_title = urllib.parse.quote(title.replace(" ", "_"))
|
| 53 |
url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{safe_title}"
|
|
|
|
| 61 |
}
|
| 62 |
return None
|
| 63 |
|
| 64 |
+
# Related Topics
|
| 65 |
def fetch_related(title, lang):
|
| 66 |
safe_title = urllib.parse.quote(title.replace(" ", "_"))
|
| 67 |
url = f"https://{lang}.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={safe_title}&prop=links&pllimit=5"
|
|
|
|
| 73 |
return [link['title'] for link in pages[0]['links']]
|
| 74 |
return []
|
| 75 |
|
| 76 |
+
# Simple Summary
|
| 77 |
def summarize_bullets(texts, limit=3):
|
| 78 |
full_text = ' '.join(set(texts))
|
| 79 |
sentences = full_text.replace('ΰ₯€', '.').replace('?', '.').replace('!', '.').split('.')
|
| 80 |
clean = [s.strip() for s in sentences if s.strip()]
|
| 81 |
return ["β’ " + s + "." for s in clean[:limit]] if clean else ["No summary available."]
|
| 82 |
|
| 83 |
+
# β
Main logic
|
| 84 |
if topic_input:
|
| 85 |
with st.spinner("π Searching Wikipedia..."):
|
| 86 |
summaries = []
|
| 87 |
final_text = ""
|
| 88 |
|
| 89 |
translated_title = get_translated_title(topic_input, lang_code)
|
| 90 |
+
st.caption(f"π Fetched title: {translated_title}") # Debug info
|
| 91 |
+
|
| 92 |
+
main = fetch_summary(translated_title, lang_code)
|
| 93 |
+
if not main:
|
| 94 |
st.error(f"No matching page found in {lang_name} for '{topic_input}'")
|
| 95 |
st.stop()
|
| 96 |
|
|
|
|
| 97 |
st.subheader("π· Main Topic")
|
| 98 |
+
summaries.append(main["summary"])
|
| 99 |
+
final_text += f"π {main['title']} - {lang_name} Wikipedia Summary\n\n"
|
| 100 |
+
final_text += main["summary"] + "\n\n"
|
| 101 |
+
st.markdown(f"### {main['title']}")
|
| 102 |
+
st.write(main["summary"])
|
| 103 |
+
st.markdown(f"[Read More β]({main['link']})", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
| 105 |
st.subheader("π Related Topics")
|
| 106 |
related_titles = fetch_related(translated_title, lang_code)
|
| 107 |
if related_titles:
|
|
|
|
| 116 |
else:
|
| 117 |
st.info("No related topics found.")
|
| 118 |
|
|
|
|
| 119 |
st.subheader("π§ Combined Summary")
|
| 120 |
for bullet in summarize_bullets(summaries):
|
| 121 |
st.markdown(bullet)
|
| 122 |
|
| 123 |
+
# π₯ Download
|
| 124 |
st.download_button(
|
| 125 |
label="π₯ Download Summary as TXT",
|
| 126 |
data=final_text,
|