Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,12 @@
|
|
1 |
import faiss
|
2 |
import numpy as np
|
3 |
from fastapi import FastAPI, Query
|
4 |
-
from fastapi.
|
5 |
from datasets import load_dataset
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
-
app.add_middleware(
|
11 |
-
CORSMiddleware,
|
12 |
-
allow_origins=["*"], # Change this to specific origins if needed for security
|
13 |
-
allow_credentials=True,
|
14 |
-
allow_methods=["*"],
|
15 |
-
allow_headers=["*"],
|
16 |
-
)
|
17 |
-
|
18 |
FIELDS = (
|
19 |
"full_name", "description", "watchers_count", "forks_count", "license",
|
20 |
"default_branch", "has_build_zig", "has_build_zig_zon", "fork",
|
@@ -56,12 +48,16 @@ scroll_data = {
|
|
56 |
@app.get("/infiniteScrollPackages/")
|
57 |
def infinite_scroll_packages(q: int = Query(0, ge=0)):
|
58 |
start = q * 10
|
59 |
-
|
|
|
|
|
60 |
|
61 |
@app.get("/infiniteScrollPrograms/")
|
62 |
def infinite_scroll_programs(q: int = Query(0, ge=0)):
|
63 |
start = q * 10
|
64 |
-
|
|
|
|
|
65 |
|
66 |
@app.get("/searchPackages/")
|
67 |
def search_packages(q: str):
|
@@ -72,10 +68,9 @@ def search_packages(q: str):
|
|
72 |
min_distance = distances[0][0]
|
73 |
threshold = min_distance * 1.5
|
74 |
results = [dataset[int(i)] for d, i in zip(distances[0], indices_[0]) if d <= threshold]
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
return final_thing
|
79 |
|
80 |
@app.get("/searchPrograms/")
|
81 |
def search_programs(q: str):
|
@@ -86,7 +81,6 @@ def search_programs(q: str):
|
|
86 |
min_distance = distances[0][0]
|
87 |
threshold = min_distance * 1.5
|
88 |
results = [dataset[int(i)] for d, i in zip(distances[0], indices_[0]) if d <= threshold]
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
return final_thing
|
|
|
1 |
import faiss
|
2 |
import numpy as np
|
3 |
from fastapi import FastAPI, Query
|
4 |
+
from fastapi.responses import JSONResponse
|
5 |
from datasets import load_dataset
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
FIELDS = (
|
11 |
"full_name", "description", "watchers_count", "forks_count", "license",
|
12 |
"default_branch", "has_build_zig", "has_build_zig_zon", "fork",
|
|
|
48 |
@app.get("/infiniteScrollPackages/")
|
49 |
def infinite_scroll_packages(q: int = Query(0, ge=0)):
|
50 |
start = q * 10
|
51 |
+
content = scroll_data["infiniteScrollPackages"][start : start + 10]
|
52 |
+
headers = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
|
53 |
+
return JSONResponse(content=content, headers=headers)
|
54 |
|
55 |
@app.get("/infiniteScrollPrograms/")
|
56 |
def infinite_scroll_programs(q: int = Query(0, ge=0)):
|
57 |
start = q * 10
|
58 |
+
content = scroll_data["infiniteScrollPrograms"][start : start + 10]
|
59 |
+
headers = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
|
60 |
+
return JSONResponse(content=content, headers=headers)
|
61 |
|
62 |
@app.get("/searchPackages/")
|
63 |
def search_packages(q: str):
|
|
|
68 |
min_distance = distances[0][0]
|
69 |
threshold = min_distance * 1.5
|
70 |
results = [dataset[int(i)] for d, i in zip(distances[0], indices_[0]) if d <= threshold]
|
71 |
+
content = results[:280] if len(results) > 280 else results
|
72 |
+
headers = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
|
73 |
+
return JSONResponse(content=content, headers=headers)
|
|
|
74 |
|
75 |
@app.get("/searchPrograms/")
|
76 |
def search_programs(q: str):
|
|
|
81 |
min_distance = distances[0][0]
|
82 |
threshold = min_distance * 1.5
|
83 |
results = [dataset[int(i)] for d, i in zip(distances[0], indices_[0]) if d <= threshold]
|
84 |
+
content = results[:280] if len(results) > 280 else results
|
85 |
+
headers = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"}
|
86 |
+
return JSONResponse(content=content, headers=headers)
|
|