-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
203 lines (135 loc) · 5.25 KB
/
main.py
File metadata and controls
203 lines (135 loc) · 5.25 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from fastapi import FastAPI, Query
from pydantic import BaseModel, HttpUrl
from pydantic import Field
from typing import Literal
from typing import Optional
from fastapi import Depends, Header, HTTPException, status
from PIL import Image
from io import BytesIO
import asyncio
import httpx
import random
import pytesseract
#cache
import time
CACHE = {}
CACHE_TTL = 300
CACHE_TIME = {}
###
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
#auth
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
API_KEY: str = "dev-secret" # fallback for local
class Config:
env_file = ".env"
settings = Settings()
async def require_api_key(x_api_key: str | None = Header(default=None)):
if x_api_key != settings.API_KEY:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or missing API key")
return True
# end of auth
# (temporary documnentation) This API allows you to grab any inspiritional quote directly from r/inspiration on reddit. Although most are images (AI will be implemented later to counteract this)
app = FastAPI()
class PostPreview(BaseModel):
id: str
title: str
score: int = Field(ge=0)
permalink: HttpUrl
author: Optional[str] = None
date: float
image: Optional[str] = None
image_quote: Optional[str] = None
class SubredditResponse(BaseModel):
subreddit: str
limit: int
posts: list[PostPreview]
sort: Literal["hot", "new", "top"]
count: int
async def image_to_bytes(img_url: str):
async with httpx.AsyncClient() as client:
response = await client.get(img_url)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
return img
async def clean_qoute(quote: str):
string = ""
prompt = f"""Rewrite this quote, remove any new break lines, and make it clean and fill in blanks
Max ~{300} characters. Return ONLY the final text (no preface/quotes).
Quote:
{quote}
"""
payload = {
"model": "phi3:3.8b",
"prompt": prompt,
"temperature": 0.2,
"stream": False
}
async with httpx.AsyncClient(timeout=30) as c:
r = await c.post("http://localhost:11434/api/generate", json=payload)
r.raise_for_status()
string = (r.json().get("response") or "").strip()
return string
async def fetch_posts(subreddit: str, limit: int, sort:Literal["hot", "new", "top"]):
url = f"https://www.reddit.com/r/{subreddit}/{sort}.json?limit={50}"
headers = {"User-Agent": "fastapi-learning-app/0.1"}
sets = ["png", "jpg", "jpeg", "webp"]
image = ""
NEW_POSTS = []
# add error handling later
# Include more types, for example if it's a gif then....its skipped
while len(NEW_POSTS) < 90:
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
response.raise_for_status()
data = response.json()
JSONDATA = data.get("data", {}).get("children", [])
after = data.get("data", {}).get("after")
for d in JSONDATA:
image = d.get("data", {}).get("url", "")
if image == ".jpg" or image == ".png":
image = image[-3:]
else:
image = image[-4:]
if image in sets:
NEW_POSTS.append(PostPreview.model_validate({
"title": d.get("data", {}).get("title", ""),
"score": d.get("data", {}).get("score", 0),
"id": d.get("data", {}).get("id", ""),
"permalink": "https://www.reddit.com" + d.get("data", {}).get("permalink", "/"),
"author": d.get("data", {}).get("author", ""),
"date": d.get("data", {}).get("created_utc", 0.0),
"image": d.get("data", {}).get("url", ""),
"image_quote": "error"
}).model_dump())
if not after:
break
url = f"https://www.reddit.com/r/{subreddit}/{sort}.json?limit={50}&after={after}"
await asyncio.sleep(3)
return NEW_POSTS
@app.get("/reddit/{subreddit}",
response_model=SubredditResponse,
summary="List posts from a subreddit, mainly r/inspiration",
description="N/A",
)
async def get_inspiration_from_reddit(subreddit: str, limit: int = Query(5, ge=1, le=25), sort: Literal["hot", "new", "top"] = "hot", _auth: bool = Depends(require_api_key)):
subreddit = subreddit.lower()
if subreddit in CACHE and time.time() - CACHE_TIME[subreddit] < CACHE_TTL:
posts = CACHE[subreddit]
else:
posts = await fetch_posts(subreddit, limit, sort)
CACHE[subreddit] = posts
CACHE_TIME[subreddit] = time.time()
random.shuffle(posts)
posts = posts[:limit]
#for entry in NEW_POSTS:
# quote = pytesseract.image_to_string(await image_to_bytes(entry["image"]))
# quote = await clean_qoute(quote)
#entry["image_quote"] = quote
return {
"subreddit": subreddit,
"limit": limit,
"posts": posts,
"sort": sort,
"count": len(posts),
}