sathvikk commited on
Commit
ca75932
·
verified ·
1 Parent(s): 3e053bf

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +54 -80
src/streamlit_app.py CHANGED
@@ -1,67 +1,48 @@
1
  import os
2
- os.environ["HOME"] = "/tmp" # Fix for Hugging Face permission issue
3
 
4
  import streamlit as st
5
  import requests
6
  import urllib.parse
7
- from langdetect import detect
8
 
9
  st.set_page_config(page_title="WikiTrail", layout="wide")
10
 
11
- # Custom style
12
- st.markdown("""
13
- <style>
14
- body {
15
- font-family: 'Segoe UI', sans-serif;
16
- background-color: #f8f9fa;
17
- }
18
- .title {
19
- font-size: 48px;
20
- text-align: center;
21
- color: #2c3e50;
22
- font-weight: bold;
23
- margin-bottom: 5px;
24
- }
25
- .subtitle {
26
- text-align: center;
27
- font-size: 18px;
28
- color: #555;
29
- margin-top: 0px;
30
- }
31
- .section {
32
- background-color: #ffffff;
33
- padding: 20px;
34
- border-radius: 16px;
35
- box-shadow: 0 4px 12px rgba(0,0,0,0.08);
36
- margin-bottom: 25px;
37
- }
38
- .emoji-title {
39
- font-size: 28px;
40
- margin-bottom: 10px;
41
- color: #2c3e50;
42
- }
43
- </style>
44
- """, unsafe_allow_html=True)
45
-
46
- # Title
47
- st.markdown("<div class='title'>📚 WikiTrail</div>", unsafe_allow_html=True)
48
- st.markdown("<div class='subtitle'>Explore Wikipedia topics visually and get a summarized journey.</div>", unsafe_allow_html=True)
49
 
50
- # Language Selector
51
  languages = {
52
  "English": "en",
53
  "Hindi (हिन्दी)": "hi",
54
  "Telugu (తెలుగు)": "te",
55
  "Tamil (தமிழ்)": "ta"
56
  }
57
- lang_name = st.selectbox("🌐 Select Language", list(languages.keys()), index=0)
58
  lang_code = languages[lang_name]
59
 
60
- # Topic Input
61
- topic_input = st.text_input("🔍 Enter a topic (in English)", placeholder="e.g., India, Gandhi, Hyderabad")
62
 
63
- # Wikipedia Summary Fetcher
64
- def fetch_topic_summary(title, lang):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  safe_title = urllib.parse.quote(title.replace(" ", "_"))
66
  url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{safe_title}"
67
  res = requests.get(url)
@@ -74,8 +55,8 @@ def fetch_topic_summary(title, lang):
74
  }
75
  return None
76
 
77
- # Related Topics
78
- def fetch_related_topics(title, lang):
79
  safe_title = urllib.parse.quote(title.replace(" ", "_"))
80
  url = f"https://{lang}.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={safe_title}&prop=links&pllimit=5"
81
  res = requests.get(url)
@@ -86,43 +67,40 @@ def fetch_related_topics(title, lang):
86
  return [link['title'] for link in pages[0]['links']]
87
  return []
88
 
89
- # Summary Cleaner
90
- def summarize_bullets(summaries, limit=3):
91
- unique = list(set(summaries))
92
- full = ' '.join(unique)
93
- sentences = full.replace('।', '.').replace('?', '.').replace('!', '.').split('.')
94
- cleaned = [s.strip() for s in sentences if s.strip()]
95
- top = cleaned[:limit]
96
- return ["• " + s + "." for s in top] if top else ["No summary available."]
97
 
98
  # Main logic
99
  if topic_input:
100
  with st.spinner("🔍 Searching Wikipedia..."):
101
  summaries = []
102
 
103
- st.markdown("<div class='section'>", unsafe_allow_html=True)
104
- st.markdown("<div class='emoji-title'>🔷 Main Topic</div>", unsafe_allow_html=True)
 
 
105
 
106
- main = fetch_topic_summary(topic_input, lang_code)
107
- if main and main["summary"]:
 
 
108
  summaries.append(main["summary"])
109
  st.markdown(f"### {main['title']}")
110
  st.write(main["summary"])
111
  st.markdown(f"[Read More →]({main['link']})", unsafe_allow_html=True)
112
  else:
113
- st.warning(f" No matching page found in {lang_name}. Try a different word.")
114
- st.markdown("</div>", unsafe_allow_html=True)
115
- st.stop()
116
-
117
- st.markdown("</div>", unsafe_allow_html=True)
118
 
119
- # Related Topics
120
- st.markdown("<div class='section'>", unsafe_allow_html=True)
121
- st.markdown("<div class='emoji-title'>🔗 Related Topics</div>", unsafe_allow_html=True)
122
- related = fetch_related_topics(topic_input, lang_code)
123
- if related:
124
- for rel in related:
125
- data = fetch_topic_summary(rel, lang_code)
126
  if data and data["summary"] not in summaries:
127
  summaries.append(data["summary"])
128
  with st.expander(data["title"]):
@@ -130,12 +108,8 @@ if topic_input:
130
  st.markdown(f"[Read More →]({data['link']})", unsafe_allow_html=True)
131
  else:
132
  st.info("No related topics found.")
133
- st.markdown("</div>", unsafe_allow_html=True)
134
 
135
- # Combined Summary
136
- st.markdown("<div class='section'>", unsafe_allow_html=True)
137
- st.markdown("<div class='emoji-title'>🧠 Combined Summary</div>", unsafe_allow_html=True)
138
- bullets = summarize_bullets(summaries)
139
- for b in bullets:
140
- st.markdown(b)
141
- st.markdown("</div>", unsafe_allow_html=True)
 
1
  import os
2
+ os.environ["HOME"] = "/tmp" # Hugging Face fix
3
 
4
  import streamlit as st
5
  import requests
6
  import urllib.parse
 
7
 
8
  st.set_page_config(page_title="WikiTrail", layout="wide")
9
 
10
+ st.title("📚 WikiTrail")
11
+ st.markdown("Explore Wikipedia topics visually and get a summarized journey.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Language selector
14
  languages = {
15
  "English": "en",
16
  "Hindi (हिन्दी)": "hi",
17
  "Telugu (తెలుగు)": "te",
18
  "Tamil (தமிழ்)": "ta"
19
  }
20
+ lang_name = st.selectbox("🌐 Select Language", list(languages.keys()))
21
  lang_code = languages[lang_name]
22
 
23
+ # Topic input
24
+ topic_input = st.text_input("🔍 Enter a topic (in English)", placeholder="e.g., India, Gandhi, Telangana")
25
 
26
+ # Translate topic using Wikipedia search
27
+ def get_translated_title(query, lang):
28
+ search_url = f"https://{lang}.wikipedia.org/w/api.php"
29
+ params = {
30
+ "action": "query",
31
+ "list": "search",
32
+ "srsearch": query,
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
+ # Fetch summary
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}"
48
  res = requests.get(url)
 
55
  }
56
  return None
57
 
58
+ # Fetch related
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"
62
  res = requests.get(url)
 
67
  return [link['title'] for link in pages[0]['links']]
68
  return []
69
 
70
+ # Summarizer
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
 
82
+ translated_title = get_translated_title(topic_input, lang_code)
83
+ if not translated_title:
84
+ st.error(f"No matching page found in {lang_name} for '{topic_input}'")
85
+ st.stop()
86
 
87
+ # Main topic
88
+ st.subheader("🔷 Main Topic")
89
+ main = fetch_summary(translated_title, lang_code)
90
+ if main:
91
  summaries.append(main["summary"])
92
  st.markdown(f"### {main['title']}")
93
  st.write(main["summary"])
94
  st.markdown(f"[Read More →]({main['link']})", unsafe_allow_html=True)
95
  else:
96
+ st.warning("Couldn't fetch main topic summary.")
 
 
 
 
97
 
98
+ # Related
99
+ st.subheader("🔗 Related Topics")
100
+ related_titles = fetch_related(translated_title, lang_code)
101
+ if related_titles:
102
+ for title in related_titles:
103
+ data = fetch_summary(title, lang_code)
 
104
  if data and data["summary"] not in summaries:
105
  summaries.append(data["summary"])
106
  with st.expander(data["title"]):
 
108
  st.markdown(f"[Read More →]({data['link']})", unsafe_allow_html=True)
109
  else:
110
  st.info("No related topics found.")
 
111
 
112
+ # Bullet summary
113
+ st.subheader("🧠 Combined Summary")
114
+ for bullet in summarize_bullets(summaries):
115
+ st.markdown(bullet)