sathvikk commited on
Commit
b67b77e
Β·
verified Β·
1 Parent(s): 42ff39f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +45 -14
src/streamlit_app.py CHANGED
@@ -16,10 +16,28 @@ languages = {
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,8 +49,8 @@ def fetch_topic_summary(topic, lang):
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()
@@ -42,17 +60,27 @@ def fetch_related_topics(topic, lang):
42
  return []
43
 
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']}")
58
  if main["image"]:
@@ -61,13 +89,15 @@ if topic:
61
  st.markdown(f"[Read More β†’]({main['link']})", unsafe_allow_html=True)
62
  else:
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"]):
73
  if data["image"]:
@@ -77,5 +107,6 @@ if topic:
77
  else:
78
  st.info("No related topics found.")
79
 
 
80
  st.subheader("🧠 Combined Summary")
81
  st.success(simple_summary(summaries))
 
16
  lang_name = st.selectbox("🌐 Select Language", list(languages.keys()), index=0)
17
  lang_code = languages[lang_name]
18
 
19
+ topic_input = st.text_input("Enter a topic (in English)", placeholder="e.g., India")
20
 
21
+ def get_localized_title(english_title, lang):
22
+ """Search for the localized title in the target language."""
23
+ url = f"https://{lang}.wikipedia.org/w/api.php"
24
+ params = {
25
+ "action": "query",
26
+ "list": "search",
27
+ "srsearch": english_title,
28
+ "format": "json",
29
+ "origin": "*"
30
+ }
31
+ response = requests.get(url, params=params)
32
+ if response.status_code == 200:
33
+ data = response.json()
34
+ search_results = data.get("query", {}).get("search", [])
35
+ if search_results:
36
+ return search_results[0]["title"]
37
+ return None
38
+
39
+ def fetch_topic_summary(title, lang):
40
+ url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{title}"
41
  res = requests.get(url)
42
  if res.status_code == 200:
43
  data = res.json()
 
49
  }
50
  return None
51
 
52
+ def fetch_related_topics(title, lang):
53
+ url = f"https://{lang}.wikipedia.org/w/api.php?action=query&format=json&origin=*&titles={title}&prop=links&pllimit=5"
54
  res = requests.get(url)
55
  if res.status_code == 200:
56
  data = res.json()
 
60
  return []
61
 
62
  def simple_summary(all_summaries, limit=3):
63
+ unique_summaries = list(set(all_summaries))
64
+ full_text = ' '.join(unique_summaries)
65
+ sentences = full_text.replace('ΰ₯€', '.').replace('?', '.').replace('!', '.').split('.')
66
+ sentences = [s.strip() for s in sentences if s.strip()]
67
+ return '. '.join(sentences[:limit]) + '.' if sentences else "No content to summarize."
68
 
69
+ if topic_input:
70
+ with st.spinner("πŸ” Translating & Searching Wikipedia..."):
71
  summaries = []
72
 
73
+ # Step 1: Get localized title from English input
74
+ localized_title = topic_input if lang_code == "en" else get_localized_title(topic_input, lang_code)
75
+
76
+ if not localized_title:
77
+ st.error(f"No page found for '{topic_input}' in {lang_name}. Try a simpler or different topic.")
78
+ st.stop()
79
+
80
+ # Step 2: Show Main Topic
81
  st.subheader("πŸ”· Main Topic")
82
+ main = fetch_topic_summary(localized_title, lang_code)
83
+ if main and main["summary"]:
84
  summaries.append(main["summary"])
85
  st.markdown(f"### {main['title']}")
86
  if main["image"]:
 
89
  st.markdown(f"[Read More β†’]({main['link']})", unsafe_allow_html=True)
90
  else:
91
  st.warning("Couldn't fetch data for the main topic.")
92
+ st.stop()
93
 
94
+ # Step 3: Related Topics
95
  st.subheader("πŸ”— Related Topics")
96
+ related = fetch_related_topics(localized_title, lang_code)
97
  if related:
98
  for rel in related:
99
  data = fetch_topic_summary(rel, lang_code)
100
+ if data and data["summary"] not in summaries:
101
  summaries.append(data["summary"])
102
  with st.expander(data["title"]):
103
  if data["image"]:
 
107
  else:
108
  st.info("No related topics found.")
109
 
110
+ # Step 4: Combined Summary
111
  st.subheader("🧠 Combined Summary")
112
  st.success(simple_summary(summaries))