vickyvigneshmass commited on
Commit
f2ba171
·
verified ·
1 Parent(s): 9baf958

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from transformers import CLIPModel, CLIPProcessor
3
+ import torch
4
+
5
+ # Initialize FastAPI
6
+ app = FastAPI()
7
+
8
+ # Load CLIP model and processor from Hugging Face
9
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
10
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
11
+
12
+ # Load and process document
13
+ with open("test.txt", "r", encoding="utf-8") as f:
14
+ sentences = [line.strip() for line in f if line.strip()]
15
+
16
+ # Encode document sentences
17
+ with torch.no_grad():
18
+ sentence_inputs = processor(text=sentences, return_tensors="pt", padding=True, truncation=True)
19
+ sentence_embeddings = model.get_text_features(**sentence_inputs)
20
+
21
+ @app.get("/")
22
+ def welcome():
23
+ return {"message": "CLIP-based Document Retrieval Service is Running!"}
24
+
25
+ @app.get("/search")
26
+ def search(text: str = Query(..., description="Enter your query"), top_k: int = 5):
27
+ with torch.no_grad():
28
+ query_inputs = processor(text=[text], return_tensors="pt", padding=True, truncation=True)
29
+ query_embedding = model.get_text_features(**query_inputs)
30
+
31
+ # Compute cosine similarity
32
+ scores = torch.nn.functional.cosine_similarity(query_embedding, sentence_embeddings)[0]
33
+ top_indices = torch.topk(scores, k=top_k).indices
34
+
35
+ results = [{
36
+ "matched_sentence": sentences[i],
37
+ "similarity_score": round(scores[i].item(), 3)
38
+ } for i in top_indices]
39
+
40
+ return {
41
+ "query": text,
42
+ "top_matches": results
43
+ }