davidepanza commited on
Commit
51c08bb
·
verified ·
1 Parent(s): 4b5fbc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py CHANGED
@@ -55,6 +55,44 @@ def list_tables():
55
  except Exception as e:
56
  return {"error": str(e)}
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  @app.post("/api/search")
59
  def search_books(book_request: BookRequest):
60
  if not book_request.query:
@@ -77,6 +115,7 @@ def search_books(book_request: BookRequest):
77
  .to_list())
78
 
79
  return {"results": results}
 
80
 
81
 
82
  @app.get("/debug/env")
 
55
  except Exception as e:
56
  return {"error": str(e)}
57
 
58
+ @app.post("/api/search")
59
+ def search_books(book_request: BookRequest):
60
+ if not book_request.query:
61
+ raise HTTPException(status_code=400, detail="Query string is required")
62
+
63
+ try:
64
+ print(f"1. Starting search for query: '{book_request.query}'")
65
+
66
+ print("2. Generating embeddings...")
67
+ query_embedding = encode_query(book_request.query)
68
+ print(f"3. Embeddings generated successfully, shape: {query_embedding.shape}")
69
+
70
+ print("4. Connecting to database table...")
71
+ table = db.open_table(os.getenv("LANCEDB_TABLE", "book_db"))
72
+ print("5. Database table opened successfully")
73
+
74
+ print("6. Starting vector search...")
75
+ results = (table.search(query_embedding)
76
+ .select([
77
+ "id", "title", "primary_author", "description",
78
+ "publisher", "published_date", "page_count",
79
+ "primary_category", "avg_rating", "ratings_count",
80
+ "thumbnail_url", "preview_link", "list_price", "buy_link",
81
+ "_distance"
82
+ ])
83
+ .limit(book_request.limit)
84
+ .to_list())
85
+
86
+ print(f"7. Search completed successfully, found {len(results)} results")
87
+ return {"results": results}
88
+
89
+ except Exception as e:
90
+ print(f"ERROR: Search failed at step with error: {str(e)}")
91
+ import traceback
92
+ traceback.print_exc()
93
+ raise HTTPException(status_code=500, detail=f"Search failed: {str(e)}")
94
+
95
+ """
96
  @app.post("/api/search")
97
  def search_books(book_request: BookRequest):
98
  if not book_request.query:
 
115
  .to_list())
116
 
117
  return {"results": results}
118
+ """
119
 
120
 
121
  @app.get("/debug/env")