Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,110 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
from
|
| 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 |
-
role="user",
|
| 75 |
-
parts=[types.Part.from_text(text=prompt)],
|
| 76 |
-
)
|
| 77 |
-
]
|
| 78 |
-
config = types.GenerateContentConfig(response_mime_type="text/plain")
|
| 79 |
-
|
| 80 |
-
# Synchronous, non-streaming call:
|
| 81 |
-
response = client.models.generate_content(
|
| 82 |
-
model=model,
|
| 83 |
-
contents=contents,
|
| 84 |
-
config=config,
|
| 85 |
-
)
|
| 86 |
-
# The full text is in response.text
|
| 87 |
-
return response.text
|
| 88 |
-
|
| 89 |
-
@app.route('/')
|
| 90 |
-
def index():
|
| 91 |
-
return render_template_string(HTML)
|
| 92 |
-
|
| 93 |
-
@app.route('/generate', methods=['POST'])
|
| 94 |
-
def gen():
|
| 95 |
-
data = request.get_json(silent=True) or {}
|
| 96 |
-
prompt = data.get("prompt", "")
|
| 97 |
-
app.logger.info(f"Received prompt: {prompt!r}")
|
| 98 |
-
if not prompt:
|
| 99 |
-
return jsonify({"error": "No prompt provided"}), 400
|
| 100 |
try:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server.py
|
| 2 |
+
|
| 3 |
+
from typing import List, Dict, Optional, Tuple
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
from sklearn.neighbors import KDTree
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# Configurable number of scales
|
| 10 |
+
NUM_SCALES = 8
|
| 11 |
+
|
| 12 |
+
# Pydantic models
|
| 13 |
+
class Profile(BaseModel):
|
| 14 |
+
id: str
|
| 15 |
+
scales: List[float] = Field(..., min_items=NUM_SCALES, max_items=NUM_SCALES)
|
| 16 |
+
|
| 17 |
+
class Content(BaseModel):
|
| 18 |
+
id: str
|
| 19 |
+
scales: List[float] = Field(..., min_items=NUM_SCALES, max_items=NUM_SCALES)
|
| 20 |
+
metadata: Optional[Dict] = None
|
| 21 |
+
|
| 22 |
+
class Recommendation(BaseModel):
|
| 23 |
+
content_id: str
|
| 24 |
+
distance: float
|
| 25 |
+
|
| 26 |
+
# Abstract repository interface
|
| 27 |
+
class DataRepository:
|
| 28 |
+
def add_user(self, user: Profile): ...
|
| 29 |
+
def add_content(self, content: Content): ...
|
| 30 |
+
def get_user(self, user_id: str) -> Profile: ...
|
| 31 |
+
def query_region(self, mins: List[float], maxs: List[float]) -> List[Content]: ...
|
| 32 |
+
|
| 33 |
+
# In-memory implementation
|
| 34 |
+
class InMemoryRepo(DataRepository):
|
| 35 |
+
def __init__(self):
|
| 36 |
+
self.users: Dict[str, Profile] = {}
|
| 37 |
+
self.contents: Dict[str, Content] = {}
|
| 38 |
+
|
| 39 |
+
def add_user(self, user: Profile):
|
| 40 |
+
self.users[user.id] = user
|
| 41 |
+
|
| 42 |
+
def add_content(self, content: Content):
|
| 43 |
+
self.contents[content.id] = content
|
| 44 |
+
|
| 45 |
+
def get_user(self, user_id: str) -> Profile:
|
| 46 |
+
user = self.users.get(user_id)
|
| 47 |
+
if not user:
|
| 48 |
+
raise ValueError("User not found")
|
| 49 |
+
return user
|
| 50 |
+
|
| 51 |
+
def query_region(self, mins: List[float], maxs: List[float]) -> List[Content]:
|
| 52 |
+
# Filter contents by N-dimensional bounding box
|
| 53 |
+
return [
|
| 54 |
+
c for c in self.contents.values()
|
| 55 |
+
if all(mn <= v <= mx for v, mn, mx in zip(c.scales, mins, maxs))
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
# FastAPI setup
|
| 59 |
+
app = FastAPI()
|
| 60 |
+
repo = InMemoryRepo()
|
| 61 |
+
|
| 62 |
+
@app.post("/users", response_model=Profile)
|
| 63 |
+
def create_user(user: Profile):
|
| 64 |
+
repo.add_user(user)
|
| 65 |
+
return user
|
| 66 |
+
|
| 67 |
+
@app.post("/contents", response_model=Content)
|
| 68 |
+
def create_content(content: Content):
|
| 69 |
+
repo.add_content(content)
|
| 70 |
+
return content
|
| 71 |
+
|
| 72 |
+
@app.get("/recommendations/{user_id}", response_model=List[Recommendation])
|
| 73 |
+
def recommend(user_id: str, region_radius: float = 0.2, top_k: int = 5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
+
user = repo.get_user(user_id)
|
| 76 |
+
except ValueError:
|
| 77 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 78 |
+
|
| 79 |
+
# Build bounding region
|
| 80 |
+
mins = [max(0.0, v - region_radius) for v in user.scales]
|
| 81 |
+
maxs = [min(1.0, v + region_radius) for v in user.scales]
|
| 82 |
+
|
| 83 |
+
# Query region
|
| 84 |
+
candidates = repo.query_region(mins, maxs)
|
| 85 |
+
if not candidates:
|
| 86 |
+
return []
|
| 87 |
+
|
| 88 |
+
# Build KDTree over candidates to find nearest neighbors
|
| 89 |
+
mat = np.array([c.scales for c in candidates])
|
| 90 |
+
tree = KDTree(mat)
|
| 91 |
+
dist, idx = tree.query(np.array(user.scales).reshape(1, -1), k=min(top_k, len(candidates)))
|
| 92 |
+
|
| 93 |
+
return [
|
| 94 |
+
Recommendation(content_id=candidates[i].id, distance=float(d))
|
| 95 |
+
for d, i in zip(dist[0], idx[0])
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
# Sample test data when run directly
|
| 99 |
if __name__ == "__main__":
|
| 100 |
+
# Install dependencies: fastapi, uvicorn, numpy, scikit-learn, pydantic
|
| 101 |
+
import uvicorn
|
| 102 |
+
# Create random test users and content
|
| 103 |
+
repo.add_user(Profile(id="user1", scales=list(np.random.rand(NUM_SCALES))))
|
| 104 |
+
for i in range(1, 21):
|
| 105 |
+
repo.add_content(Content(
|
| 106 |
+
id=f"content{i}",
|
| 107 |
+
scales=list(np.random.rand(NUM_SCALES)),
|
| 108 |
+
metadata={"title": f"Item {i}"}
|
| 109 |
+
))
|
| 110 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|