Spaces:
Sleeping
Sleeping
# from pprint import pprint | |
import requests | |
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
class Item(BaseModel): | |
text: str | |
app = FastAPI() | |
def format_response(word_meanings): | |
formatted_strings = [] | |
for entry in word_meanings: | |
word = entry['word'] | |
meanings = entry.get('meanings', []) | |
noun_meanings = '\n'.join( | |
[f"Meaning (noun) - {definition['definition']}" for meaning in meanings if meaning['partOfSpeech'] == 'noun' | |
for definition in meaning['definitions']]) | |
verb_meanings = '\n'.join( | |
[f"Meaning (verb) - {definition['definition']}" for meaning in meanings if meaning['partOfSpeech'] == 'verb' | |
for definition in meaning['definitions']]) | |
formatted_string = f"Word: {word}\n{noun_meanings}\n{verb_meanings}" | |
formatted_strings.append(formatted_string) | |
return formatted_strings | |
async def get_word_meaning(item: Item): | |
# print("text-", item.text) | |
try: | |
response = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{item.text}") | |
response.raise_for_status() | |
data = response.json() | |
data_formatted = format_response(data) | |
if data_formatted: | |
formatted_message = "\n".join(data_formatted) | |
return {"message": formatted_message} | |
else: | |
raise HTTPException(status_code=404, detail="No meaning found") | |
except requests.exceptions.RequestException: | |
return {"message": "Failed to retrieve word meaning. Please try again later."} | |
except requests.exceptions.JSONDecodeError: | |
return {"message": "Failed to process word meaning response. Please try again later."} | |
except HTTPException as e: | |
return {"message": e.detail}, e.status_code | |
async def get_dog_facts(): | |
# print("Requesting dog fact...") | |
try: | |
response = requests.get("http://dog-api.kinduff.com/api/facts") | |
response.raise_for_status() | |
data = response.json() | |
# pprint(data) | |
fact = data.get("facts", [])[0] | |
if fact: | |
return {"message": fact} | |
else: | |
raise HTTPException(status_code=404, detail="No dog facts found") | |
except requests.exceptions.RequestException: | |
return {"message": "Failed to retrieve dog facts. Please try again later."} | |
except requests.exceptions.JSONDecodeError: | |
return {"message": "Failed to process dog facts response. Please try again later."} | |
except HTTPException as e: | |
return {"message": e.detail}, e.status_code | |
async def get_anime_quotes(): | |
# print("Requesting Anime quote...") | |
try: | |
response = requests.get("https://animechan.xyz/api/random") | |
response.raise_for_status() | |
data = response.json() | |
formatted_string = f"Anime: {data['anime']}\nCharacter: {data['character']}\nQuote: \"{data['quote']}\"" | |
# print("formatted-", formatted_string) | |
if formatted_string: | |
return {"message": formatted_string} | |
else: | |
raise HTTPException(status_code=404, detail="No anime quotes found") | |
except requests.exceptions.RequestException: | |
return {"message": "Failed to retrieve anime quotes. Please try again later."} | |
except requests.exceptions.JSONDecodeError: | |
return {"message": "Failed to process anime quotes response. Please try again later."} | |
except HTTPException as e: | |
return {"message": e.detail}, e.status_code | |
async def get_boredom_act(): | |
# print("Requesting Bordom activity...") | |
try: | |
response = requests.get("https://www.boredapi.com/api/activity/") | |
response.raise_for_status() | |
data = response.json() | |
# pprint(data) | |
fact = data.get("activity", []) | |
if fact: | |
return {"message": fact} | |
else: | |
raise HTTPException(status_code=404, detail="No boredom activity found") | |
except requests.exceptions.RequestException: | |
return {"message": "Failed to retrieve boredom activity. Please try again later."} | |
except requests.exceptions.JSONDecodeError: | |
return {"message": "Failed to process boredom activity response. Please try again later."} | |
except HTTPException as e: | |
return {"message": e.detail}, e.status_code | |