Skip to content

Commit

Permalink
fix warning and reload
Browse files Browse the repository at this point in the history
  • Loading branch information
rbs333 committed Jul 18, 2024
1 parent 28d81ee commit 19ad80d
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 86 deletions.
31 changes: 1 addition & 30 deletions backend/arxivsearch/api/routes/papers.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
import asyncio
import numpy as np
import logging
from fastapi import FastAPI
from contextlib import asynccontextmanager

from fastapi import APIRouter, Query, Depends
from redis.asyncio import Redis

from redisvl.index import AsyncSearchIndex
from redisvl.query import VectorQuery, FilterQuery, CountQuery

from arxivsearch import config
from arxivsearch.db import redis_helpers
from arxivsearch.utils.embeddings import Embeddings
from arxivsearch.utils.embeddings import embeddings
from arxivsearch.schema.similarity import (
PaperSimilarityRequest,
UserTextSimilarityRequest,
SearchResponse,
VectorSearchResponse,
)


logger = logging.getLogger(__name__)


# class DB:
# client = None
# schema = None
# index = None


# db = DB()

# Initialize embeddings once
embeddings = Embeddings()

# client = Redis.from_url(config.REDIS_URL)
# schema = redis_helpers.get_schema()
# index = AsyncSearchIndex(schema, client)


# @asynccontextmanager
# async def lifespan(app: FastAPI):
# db.client = Redis.from_url(config.REDIS_URL)
# db.schema = redis_helpers.get_schema()
# db.index = AsyncSearchIndex(db.schema, db.client)
# yield
# db.client.aclose()


# Initialize the API router
router = APIRouter()

Expand Down
41 changes: 5 additions & 36 deletions backend/arxivsearch/db/redis_helpers.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import os
import logging
from typing import List
from redis.asyncio import Redis, ConnectionPool
from redis.asyncio import Redis
from arxivsearch import config
from redisvl.schema import IndexSchema
from redisvl.index import AsyncSearchIndex, SearchIndex
from redisvl.query.filter import Tag, FilterExpression
from contextlib import asynccontextmanager

logger = logging.getLogger(__name__)


async def get_async_client():
async with Redis.from_url(config.REDIS_URL) as session:
yield session
await session.aclose()


print("\n getting in pool \n")
dir_path = os.path.dirname(os.path.realpath(__file__))
schema = IndexSchema.from_yaml(os.path.join(dir_path, "index.yaml"))
client = Redis.from_url(config.REDIS_URL)
global_index = None
# client = get_async_client(


def get_schema():
Expand All @@ -35,33 +26,11 @@ def get_index():
return SearchIndex.from_yaml(os.path.join(dir_path, "index.yaml"))


# async def get_async_client():
# return Redis.from_url(config.REDIS_URL)


async def get_async_index():
try:
# schema = IndexSchema.from_yaml(os.path.join(dir_path, "index.yaml"))
# client = Redis.from_url(config.REDIS_URL)
global global_index
if not global_index:
global_index = AsyncSearchIndex(schema, client)
yield global_index
# yield AsyncSearchIndex(schema, client)

finally:
# await global_index.client.aclose()
pass
# yield AsyncSearchIndex(schema, client)
# await client.aclose()
# async with Redis.from_pool(pool) as session:
# print("using session")
# index = AsyncSearchIndex(schema, session)
# yield index
# await index.client.aclose()

# yield index
# await index.client.aclose()
global global_index
if not global_index:
global_index = AsyncSearchIndex(schema, client)
yield global_index


def build_filter_expression(
Expand Down
2 changes: 1 addition & 1 deletion backend/arxivsearch/schema/similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UserTextSimilarityRequest(BaseRequest):


class Paper(BaseModel):
paper_id: str = Field(alias="id")
paper_id: str # = Field(alias="id")
authors: str
categories: str
year: str
Expand Down
2 changes: 1 addition & 1 deletion backend/arxivsearch/tests/utils/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def seed_test_db():

index = redis_helpers.get_index()
index.connect(redis_url=config.REDIS_URL)
index.load(data=papers, id_field="id")
index.load(data=papers, id_field="paper_id")
return papers
4 changes: 2 additions & 2 deletions backend/arxivsearch/tests/utils/test_vectors.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"id": "1234.5678",
"paper_id": "1234.5678",
"title": "Exploring the Universe of Deep Learning",
"authors": "Jane Doe",
"year": "3000",
Expand Down Expand Up @@ -3342,7 +3342,7 @@
]
},
{
"id": "8765.4321",
"paper_id": "8765.4321",
"title": "Exploring the Galaxy of Deep Learning",
"authors": "John Doe",
"year": "2021",
Expand Down
3 changes: 3 additions & 0 deletions backend/arxivsearch/utils/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ async def get(self, provider: str, text: str):
return self.co_vectorizer.embed(
text, input_type="search_query", preprocess=preprocess_text
)


embeddings = Embeddings()
6 changes: 4 additions & 2 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ export const getPapers = async (limit = 15, skip = 0, years: string[] = [], cate
// get papers from Redis through the FastAPI backend


export const getSemanticallySimilarPapers = async (paper_id: string,
export const getSemanticallySimilarPapers = async (
paper_id: string,
years: string[],
categories: string[],
provider: string,
search = 'KNN',
limit = 15) => {
limit = 15
) => {
console.log(paper_id);

let body = {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/config/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const BASE_URL: string = '';
export const MASTER_URL: string = '/api/v1/papers/';
export const EMAIL = "[email protected]"
3 changes: 2 additions & 1 deletion frontend/src/views/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import { EMAIL } from '../config'
import '../styles/Footer.css';

export const Footer = () => {
Expand All @@ -8,7 +9,6 @@ export const Footer = () => {
<div>
All Redis software used in this demo is licensed according to the <a href="https://redis.io/docs/stack/license/" > Redis Stack License. </a>
</div>
<div>-</div>
<div>
<a href="https://github.com/redis-developer/redis-ai-resources">
Redis AI Resources
Expand All @@ -26,6 +26,7 @@ export const Footer = () => {
Vector Search Docs
</a>
</div>
<div>contact: {EMAIL}</div>
</div>
</footer>
);
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/views/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BASE_URL } from "../config";
import { BASE_URL, EMAIL } from "../config";
import Tooltip from '@mui/material/Tooltip';
import '../styles/Header.css';


/* eslint-disable jsx-a11y/anchor-is-valid */
export const Header = () => {
return (
Expand All @@ -25,7 +25,9 @@ export const Header = () => {
className="header-icon-link"
></img>
</a>
<a className="btn header-cta">Talk with us!</a>
<Tooltip title={`${EMAIL}`} arrow>
<a className="btn header-cta" href={`mailto:${EMAIL}`}>Talk with us!</a>
</Tooltip>
</div>
</div>
</header>
Expand Down
19 changes: 9 additions & 10 deletions frontend/src/views/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ export const Home = (props: Props) => {
const {
target: { value },
} = event;
setSkip(0);
setYears(
// On autofill we get a stringified value.
typeof value === 'string' ? value.split(',') : value,
);
setSkip(0);
console.log(years)
)
};
return (
<FormControl sx={{ m: 1, width: "35%", marginLeft: 0 }}>
Expand All @@ -139,7 +138,7 @@ export const Home = (props: Props) => {
}

function CategoryOptions() {
const handleChange = (event: SelectChangeEvent<typeof years>) => {
const handleChange = (event: SelectChangeEvent<typeof categories>) => {
const {
target: { value },
} = event;
Expand All @@ -148,7 +147,6 @@ export const Home = (props: Props) => {
typeof value === 'string' ? value.split(',') : value,
);
setSkip(0);
console.log(years)
};
return (
<FormControl sx={{ m: 1, width: "45%" }}>
Expand Down Expand Up @@ -198,13 +196,13 @@ export const Home = (props: Props) => {
}
};

// Execute this one when the component loads up
useEffect(() => {
setPapers([]);
setCategories([]);
setYears([]);
queryPapers();
}, []);
}, [categories])

useEffect(() => {
queryPapers();
}, [years])

return (
<>
Expand Down Expand Up @@ -257,6 +255,7 @@ export const Home = (props: Props) => {
<div className='home-cards'>
{papers.map((paper) => (
<Card
key={paper.paper_id}
title={paper.title}
authors={paper.authors}
paperId={paper.paper_id}
Expand Down

0 comments on commit 19ad80d

Please sign in to comment.