Spaces:
Sleeping
Sleeping
Commit
·
7b4a17b
1
Parent(s):
cd3609c
Changed some loops to list comprehension
Browse files
app.py
CHANGED
@@ -1,8 +1,19 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
|
3 |
# Define the FastAPI app
|
4 |
app = FastAPI(docs_url="/")
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@app.get("/search={query}&similarity={similarity}")
|
7 |
def search(query, similarity="false"):
|
8 |
import time
|
@@ -12,7 +23,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":
|
16 |
|
17 |
# Send a GET request to the API with the specified parameters
|
18 |
response = requests.get(url, params=params)
|
@@ -141,7 +152,7 @@ def search(query, similarity="false"):
|
|
141 |
try:
|
142 |
# Parse the result
|
143 |
parsed_result = parse_result(result, ordered_keys=ordered_keys)
|
144 |
-
|
145 |
# Append the parsed result to the lists
|
146 |
titles.append(parsed_result["Title"])
|
147 |
authors.append(parsed_result["Author"])
|
@@ -155,7 +166,6 @@ def search(query, similarity="false"):
|
|
155 |
except IndexError:
|
156 |
break
|
157 |
|
158 |
-
|
159 |
### Prediction ###
|
160 |
from transformers import (
|
161 |
AutoTokenizer,
|
@@ -163,8 +173,7 @@ def search(query, similarity="false"):
|
|
163 |
AutoModelForSequenceClassification,
|
164 |
pipeline,
|
165 |
)
|
166 |
-
from sentence_transformers import SentenceTransformer
|
167 |
-
from sentence_transformers.util import cos_sim, dot_score
|
168 |
|
169 |
# Load the classifiers
|
170 |
# classifier = TextClassifier.load(
|
@@ -175,7 +184,7 @@ def search(query, similarity="false"):
|
|
175 |
|
176 |
# Combine title, description, and publisher into a single string
|
177 |
combined_data = [
|
178 |
-
f"The book's title is {title
|
179 |
for title, description, publisher in zip(titles, descriptions, publishers)
|
180 |
]
|
181 |
|
@@ -191,10 +200,10 @@ def search(query, similarity="false"):
|
|
191 |
# classes = [sentence.labels for sentence in sentences]
|
192 |
|
193 |
# Define the summarizer model and tokenizer
|
194 |
-
sum_tokenizer = AutoTokenizer.from_pretrained("
|
195 |
|
196 |
-
|
197 |
-
sum_model = AutoModelForSeq2SeqLM.from_pretrained("lidiya/bart-base-samsum")
|
198 |
|
199 |
summarizer_pipeline = pipeline(
|
200 |
"summarization",
|
@@ -240,7 +249,7 @@ def search(query, similarity="false"):
|
|
240 |
]
|
241 |
|
242 |
# Get the predicted labels
|
243 |
-
classes = zs_classifier(
|
244 |
|
245 |
# Calculate the elapsed time
|
246 |
end_time = time.time()
|
@@ -272,21 +281,20 @@ def search(query, similarity="false"):
|
|
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 = [
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
)
|
291 |
|
292 |
return results
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
|
4 |
# Define the FastAPI app
|
5 |
app = FastAPI(docs_url="/")
|
6 |
|
7 |
+
# Add the CORS middleware to the app
|
8 |
+
app.add_middleware(
|
9 |
+
CORSMiddleware,
|
10 |
+
allow_origins=["*"],
|
11 |
+
allow_credentials=True,
|
12 |
+
allow_methods=["*"],
|
13 |
+
allow_headers=["*"],
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
@app.get("/search={query}&similarity={similarity}")
|
18 |
def search(query, similarity="false"):
|
19 |
import time
|
|
|
23 |
|
24 |
# Set the API endpoint and query parameters
|
25 |
url = "https://www.googleapis.com/books/v1/volumes"
|
26 |
+
params = {"q": str(query), "printType": "books", "maxResults": 10}
|
27 |
|
28 |
# Send a GET request to the API with the specified parameters
|
29 |
response = requests.get(url, params=params)
|
|
|
152 |
try:
|
153 |
# Parse the result
|
154 |
parsed_result = parse_result(result, ordered_keys=ordered_keys)
|
155 |
+
|
156 |
# Append the parsed result to the lists
|
157 |
titles.append(parsed_result["Title"])
|
158 |
authors.append(parsed_result["Author"])
|
|
|
166 |
except IndexError:
|
167 |
break
|
168 |
|
|
|
169 |
### Prediction ###
|
170 |
from transformers import (
|
171 |
AutoTokenizer,
|
|
|
173 |
AutoModelForSequenceClassification,
|
174 |
pipeline,
|
175 |
)
|
176 |
+
from sentence_transformers import SentenceTransformer
|
|
|
177 |
|
178 |
# Load the classifiers
|
179 |
# classifier = TextClassifier.load(
|
|
|
184 |
|
185 |
# Combine title, description, and publisher into a single string
|
186 |
combined_data = [
|
187 |
+
f"The book's title is {title}. It is published by {publisher}. This book is about {description}"
|
188 |
for title, description, publisher in zip(titles, descriptions, publishers)
|
189 |
]
|
190 |
|
|
|
200 |
# classes = [sentence.labels for sentence in sentences]
|
201 |
|
202 |
# Define the summarizer model and tokenizer
|
203 |
+
sum_tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-xsum-12-6")
|
204 |
|
205 |
+
sum_model = AutoModelForSeq2SeqLM.from_pretrained("sshleifer/distilbart-xsum-12-6")
|
206 |
+
# sum_model = AutoModelForSeq2SeqLM.from_pretrained("lidiya/bart-base-samsum")
|
207 |
|
208 |
summarizer_pipeline = pipeline(
|
209 |
"summarization",
|
|
|
249 |
]
|
250 |
|
251 |
# Get the predicted labels
|
252 |
+
classes = [zs_classifier(doc, candidate_labels) for doc in combined_data]
|
253 |
|
254 |
# Calculate the elapsed time
|
255 |
end_time = time.time()
|
|
|
281 |
similar_books = [{"sorted_by_similarity": []} for i in range(len(titles))]
|
282 |
|
283 |
# Create a list of dictionaries to store the results
|
284 |
+
results = [
|
285 |
+
{
|
286 |
+
"id": i,
|
287 |
+
"title": titles[i],
|
288 |
+
"author": authors[i],
|
289 |
+
"publisher": publishers[i],
|
290 |
+
"image_link": images[i],
|
291 |
+
"labels": classes[i]["labels"][0:2],
|
292 |
+
"label_confidences": classes[i]["scores"][0:2],
|
293 |
+
"summary": summaries[i][0]["summary_text"],
|
294 |
+
"similar_books": similar_books[i]["sorted_by_similarity"],
|
295 |
+
"runtime": runtime,
|
296 |
+
}
|
297 |
+
for i in range(len(titles))
|
298 |
+
]
|
|
|
299 |
|
300 |
return results
|