File size: 1,018 Bytes
ada8b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)