bertugmirasyedi commited on
Commit
cd3609c
·
1 Parent(s): 94b6bc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -4,7 +4,7 @@ from fastapi import FastAPI
4
  app = FastAPI(docs_url="/")
5
 
6
  @app.get("/search={query}&similarity={similarity}")
7
- def search(query, similarity=False):
8
  import time
9
  import requests
10
 
@@ -12,7 +12,7 @@ def search(query, similarity=False):
12
 
13
  # Set the API endpoint and query parameters
14
  url = "https://www.googleapis.com/books/v1/volumes"
15
- params = {"q": str(query), "printType": "books", "maxResults": 1}
16
 
17
  # Send a GET request to the API with the specified parameters
18
  response = requests.get(url, params=params)
@@ -64,7 +64,7 @@ def search(query, similarity=False):
64
  pyalex.config.email = "[email protected]"
65
 
66
  # Define a pager object with the same query
67
- pager = Works().search(str(query)).paginate(per_page=1, n_max=1)
68
 
69
  # Generate a list of the results
70
  openalex_results = list(pager)
@@ -111,7 +111,7 @@ def search(query, similarity=False):
111
  },
112
  {
113
  "role": "user",
114
- "content": f"Recommend me 1 books about {query}. Your response should be like: 'title: <title>, author: <author>, publisher: <publisher>, summary: <summary>'",
115
  },
116
  ],
117
  )
@@ -175,7 +175,7 @@ def search(query, similarity=False):
175
 
176
  # Combine title, description, and publisher into a single string
177
  combined_data = [
178
- f"{title} {description} {publisher}"
179
  for title, description, publisher in zip(titles, descriptions, publishers)
180
  ]
181
 
@@ -240,14 +240,14 @@ def search(query, similarity=False):
240
  ]
241
 
242
  # Get the predicted labels
243
- classes = [zs_classifier(doc, candidate_labels) for doc in combined_data]
244
 
245
  # Calculate the elapsed time
246
  end_time = time.time()
247
  runtime = f"{end_time - start_time:.2f} seconds"
248
 
249
  # Calculate the similarity between the books
250
- if similarity:
251
  from sentence_transformers import util
252
 
253
  sentence_transformer = SentenceTransformer("all-MiniLM-L6-v2")
@@ -268,6 +268,8 @@ def search(query, similarity=False):
268
  "sorted_by_similarity": similarity_sorted[0][1:],
269
  }
270
  )
 
 
271
 
272
  # Create a list of dictionaries to store the results
273
  results = []
 
4
  app = FastAPI(docs_url="/")
5
 
6
  @app.get("/search={query}&similarity={similarity}")
7
+ def search(query, similarity="false"):
8
  import time
9
  import requests
10
 
 
12
 
13
  # Set the API endpoint and query parameters
14
  url = "https://www.googleapis.com/books/v1/volumes"
15
+ params = {"q": str(query), "printType": "books", "maxResults": 30}
16
 
17
  # Send a GET request to the API with the specified parameters
18
  response = requests.get(url, params=params)
 
64
  pyalex.config.email = "[email protected]"
65
 
66
  # Define a pager object with the same query
67
+ pager = Works().search(str(query)).paginate(per_page=10, n_max=10)
68
 
69
  # Generate a list of the results
70
  openalex_results = list(pager)
 
111
  },
112
  {
113
  "role": "user",
114
+ "content": f"Recommend me 10 books about {query}. Your response should be like: 'title: <title>, author: <author>, publisher: <publisher>, summary: <summary>'",
115
  },
116
  ],
117
  )
 
175
 
176
  # Combine title, description, and publisher into a single string
177
  combined_data = [
178
+ f"The book's title is {title.title()}. It is published by {publisher.title()}. This book is about {description}"
179
  for title, description, publisher in zip(titles, descriptions, publishers)
180
  ]
181
 
 
240
  ]
241
 
242
  # Get the predicted labels
243
+ classes = zs_classifier(combined_data, candidate_labels)
244
 
245
  # Calculate the elapsed time
246
  end_time = time.time()
247
  runtime = f"{end_time - start_time:.2f} seconds"
248
 
249
  # Calculate the similarity between the books
250
+ if similarity != "false":
251
  from sentence_transformers import util
252
 
253
  sentence_transformer = SentenceTransformer("all-MiniLM-L6-v2")
 
268
  "sorted_by_similarity": similarity_sorted[0][1:],
269
  }
270
  )
271
+ else:
272
+ similar_books = [{"sorted_by_similarity": []} for i in range(len(titles))]
273
 
274
  # Create a list of dictionaries to store the results
275
  results = []