Spaces:
Running
Running
Update wikipedia.py
Browse files- wikipedia.py +119 -137
wikipedia.py
CHANGED
@@ -1,137 +1,119 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import wikipediaapi
|
3 |
-
import datetime
|
4 |
-
from reportlab.lib.pagesizes import letter
|
5 |
-
from reportlab.pdfgen import canvas
|
6 |
-
import tempfile
|
7 |
-
from gtts import gTTS
|
8 |
-
import speech_recognition as sr
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
pdf
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
pdf.drawString(30, y_position, f"
|
35 |
-
y_position -=
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
st.sidebar.
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
st.
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
text_to_speech(summary, tts_filename, lang=lang_code)
|
121 |
-
st.audio(tts_filename, format="audio/mp3")
|
122 |
-
|
123 |
-
# Save chat history as PDF
|
124 |
-
file_name = st.sidebar.text_input("File Name to Save Chat", value="chat_history")
|
125 |
-
if st.sidebar.button("Save Chat as PDF"):
|
126 |
-
pdf_path = save_chat_history_as_pdf(st.session_state.chat_history, file_name)
|
127 |
-
with open(pdf_path, "rb") as pdf_file:
|
128 |
-
st.sidebar.download_button("Download PDF", pdf_file, file_name=f"{file_name}.pdf", mime="application/pdf")
|
129 |
-
|
130 |
-
# Display favorites
|
131 |
-
st.sidebar.write("### Favorites")
|
132 |
-
for i, (fav_query, fav_summary) in enumerate(st.session_state.favorites, 1):
|
133 |
-
st.sidebar.write(f"**{i}. {fav_query}**")
|
134 |
-
st.sidebar.write(fav_summary[:100] + "...")
|
135 |
-
|
136 |
-
if __name__ == "__main__":
|
137 |
-
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import wikipediaapi
|
3 |
+
import datetime
|
4 |
+
from reportlab.lib.pagesizes import letter
|
5 |
+
from reportlab.pdfgen import canvas
|
6 |
+
import tempfile
|
7 |
+
from gtts import gTTS
|
8 |
+
import speech_recognition as sr
|
9 |
+
|
10 |
+
def get_wikipedia_summary(query, lang_code, char_limit, summary_level):
|
11 |
+
user_agent = "Mozilla/5.0"
|
12 |
+
wiki = wikipediaapi.Wikipedia(language=lang_code, extract_format=wikipediaapi.ExtractFormat.WIKI, user_agent=user_agent)
|
13 |
+
page = wiki.page(query)
|
14 |
+
if not page.exists():
|
15 |
+
return "Page not found."
|
16 |
+
if summary_level == "Brief":
|
17 |
+
return page.summary[:char_limit]
|
18 |
+
elif summary_level == "Detailed":
|
19 |
+
return page.summary
|
20 |
+
elif summary_level == "Bullet Points":
|
21 |
+
points = page.summary.split('. ')
|
22 |
+
return '\n'.join(f"- {p.strip()}" for p in points if p)[:char_limit]
|
23 |
+
|
24 |
+
def save_chat_history_as_pdf(chat_history, file_name):
|
25 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
27 |
+
pdf = canvas.Canvas(tmp_file.name, pagesize=letter)
|
28 |
+
pdf.setTitle(file_name)
|
29 |
+
pdf.drawString(30, 750, f"{file_name} - Saved on {timestamp}")
|
30 |
+
y_position = 720
|
31 |
+
for query, response in chat_history:
|
32 |
+
pdf.drawString(30, y_position, f"User: {query}")
|
33 |
+
y_position -= 20
|
34 |
+
pdf.drawString(30, y_position, f"Bot: {response}")
|
35 |
+
y_position -= 40
|
36 |
+
if y_position < 40:
|
37 |
+
pdf.showPage()
|
38 |
+
y_position = 750
|
39 |
+
pdf.save()
|
40 |
+
return tmp_file.name
|
41 |
+
|
42 |
+
def text_to_speech(text, filename, lang="en"):
|
43 |
+
tts = gTTS(text=text, lang=lang)
|
44 |
+
tts.save(filename)
|
45 |
+
return filename
|
46 |
+
|
47 |
+
def voice_search(lang_code):
|
48 |
+
recognizer = sr.Recognizer()
|
49 |
+
with sr.Microphone() as source:
|
50 |
+
st.write("Listening...")
|
51 |
+
audio = recognizer.listen(source)
|
52 |
+
try:
|
53 |
+
query = recognizer.recognize_google(audio, language=lang_code)
|
54 |
+
st.success(f"You said: {query}")
|
55 |
+
return query
|
56 |
+
except sr.UnknownValueError:
|
57 |
+
st.error("Could not understand the audio.")
|
58 |
+
return None
|
59 |
+
except sr.RequestError as e:
|
60 |
+
st.error(f"Could not request results; {e}")
|
61 |
+
return None
|
62 |
+
|
63 |
+
def main():
|
64 |
+
st.title("Wikipedia Summary & Text-to-Speech")
|
65 |
+
|
66 |
+
lang_map = {
|
67 |
+
"English": "en",
|
68 |
+
"Spanish": "es",
|
69 |
+
"Chinese": "zh",
|
70 |
+
"Hindi": "hi",
|
71 |
+
"Telugu": "te"
|
72 |
+
}
|
73 |
+
selected_lang = st.sidebar.selectbox("Wikipedia Language", list(lang_map.keys()), key="language_selector")
|
74 |
+
summary_levels = ["Brief", "Detailed", "Bullet Points"]
|
75 |
+
summary_level = st.sidebar.selectbox("Summarization Level", summary_levels)
|
76 |
+
char_limit = st.sidebar.slider("Character Limit", min_value=100, max_value=2000, value=500, step=100)
|
77 |
+
|
78 |
+
if "chat_history" not in st.session_state:
|
79 |
+
st.session_state.chat_history = []
|
80 |
+
if "favorites" not in st.session_state:
|
81 |
+
st.session_state.favorites = []
|
82 |
+
|
83 |
+
query = st.text_input("Enter a topic to search on Wikipedia:")
|
84 |
+
|
85 |
+
if st.button("Voice Search"):
|
86 |
+
lang_code = lang_map[selected_lang]
|
87 |
+
voice_query = voice_search(lang_code)
|
88 |
+
if voice_query:
|
89 |
+
query = voice_query
|
90 |
+
|
91 |
+
if query:
|
92 |
+
lang_code = lang_map[selected_lang]
|
93 |
+
summary = get_wikipedia_summary(query, lang_code, char_limit, summary_level)
|
94 |
+
st.markdown(f"### Summary for: {query}")
|
95 |
+
st.write(summary)
|
96 |
+
st.session_state.chat_history.append((query, summary))
|
97 |
+
|
98 |
+
if st.button("Add to Favorites"):
|
99 |
+
st.session_state.favorites.append((query, summary))
|
100 |
+
st.success("Added to favorites!")
|
101 |
+
|
102 |
+
tts_filename = f"{query}_speech.mp3"
|
103 |
+
if st.button("Play Text-to-Speech"):
|
104 |
+
text_to_speech(summary, tts_filename, lang=lang_code)
|
105 |
+
st.audio(tts_filename, format="audio/mp3")
|
106 |
+
|
107 |
+
file_name = st.sidebar.text_input("File Name to Save Chat", value="chat_history")
|
108 |
+
if st.sidebar.button("Save Chat as PDF"):
|
109 |
+
pdf_path = save_chat_history_as_pdf(st.session_state.chat_history, file_name)
|
110 |
+
with open(pdf_path, "rb") as pdf_file:
|
111 |
+
st.sidebar.download_button("Download PDF", pdf_file, file_name=f"{file_name}.pdf", mime="application/pdf")
|
112 |
+
|
113 |
+
st.sidebar.write("### Favorites")
|
114 |
+
for i, (fav_query, fav_summary) in enumerate(st.session_state.favorites, 1):
|
115 |
+
st.sidebar.write(f"**{i}. {fav_query}**")
|
116 |
+
st.sidebar.write(fav_summary[:100] + "...")
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|