tts / app.py
Spooke's picture
Update app.py
14833ab verified
import requests
def search_duckduckgo(query):
url = f"https://api.duckduckgo.com/?q={query}&format=json"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if 'AbstractText' in data:
return data['AbstractText']
elif 'RelatedTopics' in data and len(data['RelatedTopics']) > 0:
return data['RelatedTopics'][0]['Text']
return "Sorry, I couldn't find an answer."
from gtts import gTTS
import IPython.display as ipd
import io
def text_to_speech(text, lang='en'):
# Create a gTTS object
tts = gTTS(text=text, lang=lang, slow=False)
# Save the speech to a BytesIO object
speech_bytes = io.BytesIO()
tts.write_to_fp(speech_bytes)
speech_bytes.seek(0)
# Play the speech
audio = ipd.Audio(speech_bytes.read(), autoplay=True)
ipd.display(audio)
# Example usage
question = input("Ask me something: ")
answer = search_duckduckgo(question)
print(answer)
# Example usage
text_to_speech(answer)