sathvikk commited on
Commit
f04f858
Β·
verified Β·
1 Parent(s): b9b2222

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +18 -19
src/streamlit_app.py CHANGED
@@ -2,15 +2,13 @@ import streamlit as st
2
  import requests
3
  import urllib.parse
4
  from langdetect import detect
5
- from gtts import gTTS
6
- import os
7
 
8
  st.set_page_config(page_title="WikiTrail", layout="wide")
9
 
10
  st.markdown("<h1 style='text-align: center;'>πŸ“š WikiTrail</h1>", unsafe_allow_html=True)
11
  st.markdown("<p style='text-align: center;'>Explore Wikipedia topics visually and get a summarized journey.</p>", unsafe_allow_html=True)
12
 
13
- # Language options
14
  languages = {
15
  "English": "en",
16
  "Hindi (ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦ΰ₯€)": "hi",
@@ -20,7 +18,7 @@ languages = {
20
  lang_name = st.selectbox("🌐 Select Language", list(languages.keys()), index=0)
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, Hyderabad, Gandhi")
25
 
26
  # Search matches
@@ -51,7 +49,7 @@ def get_search_matches(title, lang):
51
  matches = search(lang, top_english_title)
52
  return matches
53
 
54
- # Safe summary fetch
55
  def fetch_topic_summary(title, lang):
56
  safe_title = urllib.parse.quote(title.replace(" ", "_"))
57
  url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{safe_title}"
@@ -77,7 +75,7 @@ def fetch_related_topics(title, lang):
77
  return [link['title'] for link in pages[0]['links']]
78
  return []
79
 
80
- # Summary formatter
81
  def summarize_bullets(summaries, limit=3):
82
  unique = list(set(summaries))
83
  full = ' '.join(unique)
@@ -86,12 +84,11 @@ def summarize_bullets(summaries, limit=3):
86
  top = cleaned[:limit]
87
  return ["β€’ " + s + "." for s in top] if top else ["No summary available."]
88
 
89
- # MAIN logic
90
  if topic_input:
91
  with st.spinner("πŸ” Searching Wikipedia..."):
92
  summaries = []
93
 
94
- # Detect language
95
  try:
96
  detected_lang = detect(topic_input)
97
  except:
@@ -103,9 +100,19 @@ if topic_input:
103
  st.error(f"No matching pages found in {lang_name}. Try a different topic.")
104
  st.stop()
105
 
106
- selected_title = st.selectbox("πŸ”Ž Select best matching article:", matches)
 
 
 
107
 
108
- # Main Topic Section
 
 
 
 
 
 
 
109
  st.subheader("πŸ”· Main Topic")
110
  main = fetch_topic_summary(selected_title, lang_code)
111
  if main and main["summary"]:
@@ -113,19 +120,11 @@ if topic_input:
113
  st.markdown(f"### {main['title']}")
114
  st.write(main["summary"])
115
  st.markdown(f"[Read More β†’]({main['link']})", unsafe_allow_html=True)
116
-
117
- # TTS
118
- try:
119
- tts = gTTS(text=main["summary"], lang=lang_code)
120
- tts.save("summary.mp3")
121
- st.audio("summary.mp3", format="audio/mp3")
122
- except Exception as e:
123
- st.warning("πŸ”ˆ Text-to-speech not supported for this language or topic.")
124
  else:
125
  st.warning("Couldn't fetch the summary.")
126
  st.stop()
127
 
128
- # Related Topics
129
  st.subheader("πŸ”— Related Topics")
130
  related = fetch_related_topics(selected_title, lang_code)
131
  if related:
 
2
  import requests
3
  import urllib.parse
4
  from langdetect import detect
 
 
5
 
6
  st.set_page_config(page_title="WikiTrail", layout="wide")
7
 
8
  st.markdown("<h1 style='text-align: center;'>πŸ“š WikiTrail</h1>", unsafe_allow_html=True)
9
  st.markdown("<p style='text-align: center;'>Explore Wikipedia topics visually and get a summarized journey.</p>", unsafe_allow_html=True)
10
 
11
+ # Supported Languages
12
  languages = {
13
  "English": "en",
14
  "Hindi (ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦ΰ₯€)": "hi",
 
18
  lang_name = st.selectbox("🌐 Select Language", list(languages.keys()), index=0)
19
  lang_code = languages[lang_name]
20
 
21
+ # User input
22
  topic_input = st.text_input("Enter a topic (in English)", placeholder="e.g., India, Hyderabad, Gandhi")
23
 
24
  # Search matches
 
49
  matches = search(lang, top_english_title)
50
  return matches
51
 
52
+ # Summary fetch
53
  def fetch_topic_summary(title, lang):
54
  safe_title = urllib.parse.quote(title.replace(" ", "_"))
55
  url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{safe_title}"
 
75
  return [link['title'] for link in pages[0]['links']]
76
  return []
77
 
78
+ # Summary bullets
79
  def summarize_bullets(summaries, limit=3):
80
  unique = list(set(summaries))
81
  full = ' '.join(unique)
 
84
  top = cleaned[:limit]
85
  return ["β€’ " + s + "." for s in top] if top else ["No summary available."]
86
 
87
+ # Main logic
88
  if topic_input:
89
  with st.spinner("πŸ” Searching Wikipedia..."):
90
  summaries = []
91
 
 
92
  try:
93
  detected_lang = detect(topic_input)
94
  except:
 
100
  st.error(f"No matching pages found in {lang_name}. Try a different topic.")
101
  st.stop()
102
 
103
+ # Store selected article in session to rerun on change
104
+ if "selected_title" not in st.session_state or st.session_state.last_topic != topic_input:
105
+ st.session_state.selected_title = matches[0]
106
+ st.session_state.last_topic = topic_input
107
 
108
+ selected_title = st.selectbox("πŸ”Ž Select best matching article:", matches, index=matches.index(st.session_state.selected_title) if st.session_state.selected_title in matches else 0)
109
+
110
+ # Update session if user changes the dropdown
111
+ if selected_title != st.session_state.selected_title:
112
+ st.session_state.selected_title = selected_title
113
+ st.rerun()
114
+
115
+ # Main topic summary
116
  st.subheader("πŸ”· Main Topic")
117
  main = fetch_topic_summary(selected_title, lang_code)
118
  if main and main["summary"]:
 
120
  st.markdown(f"### {main['title']}")
121
  st.write(main["summary"])
122
  st.markdown(f"[Read More β†’]({main['link']})", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
123
  else:
124
  st.warning("Couldn't fetch the summary.")
125
  st.stop()
126
 
127
+ # Related topics
128
  st.subheader("πŸ”— Related Topics")
129
  related = fetch_related_topics(selected_title, lang_code)
130
  if related: