Spaces:
Sleeping
Sleeping
File size: 4,432 Bytes
bb2bbbb |
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# 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
@app.post("/wordmeaning")
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
@app.get("/dogfacts")
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
@app.get("/animequotes")
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
@app.get("/boredomact")
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
|