Spaces:
Runtime error
Runtime error
ranga galla commited on
Upload main.py with huggingface_hub
Browse files
main.py
CHANGED
|
@@ -24,6 +24,13 @@ analytics = {
|
|
| 24 |
"started_at": datetime.now(timezone.utc).isoformat(),
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 28 |
HTTP_PROXY = os.getenv("HTTP_PROXY") or os.getenv("http_proxy") or os.getenv("https_proxy") or os.getenv("HTTPS_PROXY") or ""
|
| 29 |
|
|
@@ -75,6 +82,16 @@ class DictionaryResponse(BaseModel):
|
|
| 75 |
result: dict
|
| 76 |
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
@app.post("/api/lookup", response_model=DictionaryResponse)
|
| 79 |
async def lookup_word(query: WordQuery):
|
| 80 |
api_key = query.api_key.strip() or GROQ_API_KEY
|
|
@@ -147,6 +164,45 @@ async def get_stats():
|
|
| 147 |
}
|
| 148 |
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
@app.get("/stats")
|
| 151 |
async def stats_page():
|
| 152 |
return FileResponse("static/stats.html")
|
|
|
|
| 24 |
"started_at": datetime.now(timezone.utc).isoformat(),
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# --- Likes & Comments store (in-memory) ---
|
| 28 |
+
community = {
|
| 29 |
+
"likes": 0,
|
| 30 |
+
"liked_users": set(), # track by session id to prevent duplicates
|
| 31 |
+
"comments": [], # list of {name, text, time}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 35 |
HTTP_PROXY = os.getenv("HTTP_PROXY") or os.getenv("http_proxy") or os.getenv("https_proxy") or os.getenv("HTTPS_PROXY") or ""
|
| 36 |
|
|
|
|
| 82 |
result: dict
|
| 83 |
|
| 84 |
|
| 85 |
+
class CommentInput(BaseModel):
|
| 86 |
+
name: str = Field(..., min_length=1, max_length=50)
|
| 87 |
+
text: str = Field(..., min_length=1, max_length=500)
|
| 88 |
+
session_id: str = Field(default="")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class LikeInput(BaseModel):
|
| 92 |
+
session_id: str = Field(..., min_length=1, max_length=100)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
@app.post("/api/lookup", response_model=DictionaryResponse)
|
| 96 |
async def lookup_word(query: WordQuery):
|
| 97 |
api_key = query.api_key.strip() or GROQ_API_KEY
|
|
|
|
| 164 |
}
|
| 165 |
|
| 166 |
|
| 167 |
+
# --- Like & Comment APIs ---
|
| 168 |
+
@app.post("/api/like")
|
| 169 |
+
async def like_app(data: LikeInput):
|
| 170 |
+
if data.session_id not in community["liked_users"]:
|
| 171 |
+
community["liked_users"].add(data.session_id)
|
| 172 |
+
community["likes"] += 1
|
| 173 |
+
return {"likes": community["likes"], "liked": True}
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@app.delete("/api/like")
|
| 177 |
+
async def unlike_app(data: LikeInput):
|
| 178 |
+
if data.session_id in community["liked_users"]:
|
| 179 |
+
community["liked_users"].discard(data.session_id)
|
| 180 |
+
community["likes"] = max(0, community["likes"] - 1)
|
| 181 |
+
return {"likes": community["likes"], "liked": False}
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
@app.get("/api/community")
|
| 185 |
+
async def get_community(session_id: str = ""):
|
| 186 |
+
return {
|
| 187 |
+
"likes": community["likes"],
|
| 188 |
+
"liked": session_id in community["liked_users"],
|
| 189 |
+
"comments": community["comments"][-50:],
|
| 190 |
+
"comment_count": len(community["comments"]),
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
@app.post("/api/comment")
|
| 195 |
+
async def add_comment(data: CommentInput):
|
| 196 |
+
comment = {
|
| 197 |
+
"name": data.name.strip(),
|
| 198 |
+
"text": data.text.strip(),
|
| 199 |
+
"time": datetime.now(timezone.utc).isoformat(),
|
| 200 |
+
}
|
| 201 |
+
community["comments"].append(comment)
|
| 202 |
+
community["comments"] = community["comments"][-100:] # keep last 100
|
| 203 |
+
return {"comment": comment, "comment_count": len(community["comments"])}
|
| 204 |
+
|
| 205 |
+
|
| 206 |
@app.get("/stats")
|
| 207 |
async def stats_page():
|
| 208 |
return FileResponse("static/stats.html")
|