Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
def search_duckduckgo(query):
|
| 4 |
+
url = f"https://api.duckduckgo.com/?q={query}&format=json"
|
| 5 |
+
response = requests.get(url)
|
| 6 |
+
if response.status_code == 200:
|
| 7 |
+
data = response.json()
|
| 8 |
+
if 'AbstractText' in data:
|
| 9 |
+
return data['AbstractText']
|
| 10 |
+
elif 'RelatedTopics' in data and len(data['RelatedTopics']) > 0:
|
| 11 |
+
return data['RelatedTopics'][0]['Text']
|
| 12 |
+
return "Sorry, I couldn't find an answer."
|
| 13 |
+
|
| 14 |
+
from gtts import gTTS
|
| 15 |
+
import IPython.display as ipd
|
| 16 |
+
import io
|
| 17 |
+
|
| 18 |
+
def text_to_speech(text, lang='en'):
|
| 19 |
+
# Create a gTTS object
|
| 20 |
+
tts = gTTS(text=text, lang=lang, slow=False)
|
| 21 |
+
|
| 22 |
+
# Save the speech to a BytesIO object
|
| 23 |
+
speech_bytes = io.BytesIO()
|
| 24 |
+
tts.write_to_fp(speech_bytes)
|
| 25 |
+
speech_bytes.seek(0)
|
| 26 |
+
|
| 27 |
+
# Play the speech
|
| 28 |
+
audio = ipd.Audio(speech_bytes.read(), autoplay=True)
|
| 29 |
+
ipd.display(audio)
|
| 30 |
+
|
| 31 |
+
# Example usage
|
| 32 |
+
question = input("Ask me something: ")
|
| 33 |
+
answer = search_duckduckgo(question)
|
| 34 |
+
print(answer)
|
| 35 |
+
|
| 36 |
+
# Example usage
|
| 37 |
+
text_to_speech(answer)
|