Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# from pprint import pprint
|
2 |
+
import requests
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
|
6 |
+
|
7 |
+
class Item(BaseModel):
|
8 |
+
text: str
|
9 |
+
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
|
14 |
+
def format_response(word_meanings):
|
15 |
+
formatted_strings = []
|
16 |
+
for entry in word_meanings:
|
17 |
+
word = entry['word']
|
18 |
+
meanings = entry.get('meanings', [])
|
19 |
+
noun_meanings = '\n'.join(
|
20 |
+
[f"Meaning (noun) - {definition['definition']}" for meaning in meanings if meaning['partOfSpeech'] == 'noun'
|
21 |
+
for definition in meaning['definitions']])
|
22 |
+
verb_meanings = '\n'.join(
|
23 |
+
[f"Meaning (verb) - {definition['definition']}" for meaning in meanings if meaning['partOfSpeech'] == 'verb'
|
24 |
+
for definition in meaning['definitions']])
|
25 |
+
formatted_string = f"Word: {word}\n{noun_meanings}\n{verb_meanings}"
|
26 |
+
formatted_strings.append(formatted_string)
|
27 |
+
return formatted_strings
|
28 |
+
|
29 |
+
|
30 |
+
@app.post("/wordmeaning")
|
31 |
+
async def get_word_meaning(item: Item):
|
32 |
+
# print("text-", item.text)
|
33 |
+
|
34 |
+
try:
|
35 |
+
response = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{item.text}")
|
36 |
+
response.raise_for_status()
|
37 |
+
|
38 |
+
data = response.json()
|
39 |
+
data_formatted = format_response(data)
|
40 |
+
|
41 |
+
if data_formatted:
|
42 |
+
formatted_message = "\n".join(data_formatted)
|
43 |
+
return {"message": formatted_message}
|
44 |
+
else:
|
45 |
+
raise HTTPException(status_code=404, detail="No meaning found")
|
46 |
+
|
47 |
+
except requests.exceptions.RequestException:
|
48 |
+
return {"message": "Failed to retrieve word meaning. Please try again later."}
|
49 |
+
|
50 |
+
except requests.exceptions.JSONDecodeError:
|
51 |
+
return {"message": "Failed to process word meaning response. Please try again later."}
|
52 |
+
|
53 |
+
except HTTPException as e:
|
54 |
+
return {"message": e.detail}, e.status_code
|
55 |
+
|
56 |
+
|
57 |
+
@app.get("/dogfacts")
|
58 |
+
async def get_dog_facts():
|
59 |
+
# print("Requesting dog fact...")
|
60 |
+
|
61 |
+
try:
|
62 |
+
response = requests.get("http://dog-api.kinduff.com/api/facts")
|
63 |
+
response.raise_for_status()
|
64 |
+
|
65 |
+
data = response.json()
|
66 |
+
# pprint(data)
|
67 |
+
fact = data.get("facts", [])[0]
|
68 |
+
if fact:
|
69 |
+
return {"message": fact}
|
70 |
+
else:
|
71 |
+
raise HTTPException(status_code=404, detail="No dog facts found")
|
72 |
+
|
73 |
+
except requests.exceptions.RequestException:
|
74 |
+
return {"message": "Failed to retrieve dog facts. Please try again later."}
|
75 |
+
|
76 |
+
except requests.exceptions.JSONDecodeError:
|
77 |
+
return {"message": "Failed to process dog facts response. Please try again later."}
|
78 |
+
|
79 |
+
except HTTPException as e:
|
80 |
+
return {"message": e.detail}, e.status_code
|
81 |
+
|
82 |
+
|
83 |
+
@app.get("/animequotes")
|
84 |
+
async def get_anime_quotes():
|
85 |
+
# print("Requesting Anime quote...")
|
86 |
+
|
87 |
+
try:
|
88 |
+
response = requests.get("https://animechan.xyz/api/random")
|
89 |
+
response.raise_for_status()
|
90 |
+
|
91 |
+
data = response.json()
|
92 |
+
formatted_string = f"Anime: {data['anime']}\nCharacter: {data['character']}\nQuote: \"{data['quote']}\""
|
93 |
+
|
94 |
+
# print("formatted-", formatted_string)
|
95 |
+
if formatted_string:
|
96 |
+
return {"message": formatted_string}
|
97 |
+
else:
|
98 |
+
raise HTTPException(status_code=404, detail="No anime quotes found")
|
99 |
+
|
100 |
+
except requests.exceptions.RequestException:
|
101 |
+
return {"message": "Failed to retrieve anime quotes. Please try again later."}
|
102 |
+
|
103 |
+
except requests.exceptions.JSONDecodeError:
|
104 |
+
return {"message": "Failed to process anime quotes response. Please try again later."}
|
105 |
+
|
106 |
+
except HTTPException as e:
|
107 |
+
return {"message": e.detail}, e.status_code
|
108 |
+
|
109 |
+
|
110 |
+
@app.get("/boredomact")
|
111 |
+
async def get_boredom_act():
|
112 |
+
# print("Requesting Bordom activity...")
|
113 |
+
|
114 |
+
try:
|
115 |
+
response = requests.get("https://www.boredapi.com/api/activity/")
|
116 |
+
response.raise_for_status()
|
117 |
+
|
118 |
+
data = response.json()
|
119 |
+
|
120 |
+
# pprint(data)
|
121 |
+
fact = data.get("activity", [])
|
122 |
+
if fact:
|
123 |
+
return {"message": fact}
|
124 |
+
else:
|
125 |
+
raise HTTPException(status_code=404, detail="No boredom activity found")
|
126 |
+
|
127 |
+
except requests.exceptions.RequestException:
|
128 |
+
return {"message": "Failed to retrieve boredom activity. Please try again later."}
|
129 |
+
|
130 |
+
except requests.exceptions.JSONDecodeError:
|
131 |
+
return {"message": "Failed to process boredom activity response. Please try again later."}
|
132 |
+
|
133 |
+
except HTTPException as e:
|
134 |
+
return {"message": e.detail}, e.status_code
|