sathvikk commited on
Commit
bd27fc9
Β·
verified Β·
1 Parent(s): dddd074

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 permission fix
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 selector
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
- # 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 = {
@@ -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
- # 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}"
@@ -55,7 +61,7 @@ def fetch_summary(title, lang):
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"
@@ -67,38 +73,35 @@ def fetch_related(title, lang):
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
  final_text = ""
82
 
83
  translated_title = get_translated_title(topic_input, lang_code)
84
- if not translated_title:
 
 
 
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
- main = fetch_summary(translated_title, lang_code)
91
- if main:
92
- summaries.append(main["summary"])
93
- final_text += f"πŸ“š {main['title']} - {lang_name} Wikipedia Summary\n\n"
94
- final_text += main["summary"] + "\n\n"
95
- st.markdown(f"### {main['title']}")
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
- # Download button
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,