|
|
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'): |
|
|
|
|
|
tts = gTTS(text=text, lang=lang, slow=False) |
|
|
|
|
|
|
|
|
speech_bytes = io.BytesIO() |
|
|
tts.write_to_fp(speech_bytes) |
|
|
speech_bytes.seek(0) |
|
|
|
|
|
|
|
|
audio = ipd.Audio(speech_bytes.read(), autoplay=True) |
|
|
ipd.display(audio) |
|
|
|
|
|
|
|
|
question = input("Ask me something: ") |
|
|
answer = search_duckduckgo(question) |
|
|
print(answer) |
|
|
|
|
|
|
|
|
text_to_speech(answer) |