Heerrrs commited on
Commit
ea93dc4
·
1 Parent(s): bc7ad9c

Added master.py and Dockerfile

Browse files
Files changed (3) hide show
  1. DockerFile +10 -0
  2. master.py +113 -0
  3. requirements.txt +4 -0
DockerFile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY master.py /app/
6
+ COPY requirements.txt /app/
7
+
8
+ RUN pip install -r requirements.txt
9
+
10
+ CMD ["uvicorn", "master:app", "--host", "0.0.0.0", "--port", "7860"]
master.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from fastapi import FastAPI
2
+ # from fastapi.middleware.cors import CORSMiddleware
3
+ # from pydantic import BaseModel
4
+ # from sentence_transformers import SentenceTransformer, util
5
+
6
+ # app = FastAPI()
7
+
8
+ # app.add_middleware(
9
+ # CORSMiddleware,
10
+ # allow_origins=["http://localhost:5173"],
11
+ # allow_credentials=True,
12
+ # allow_methods=["*"],
13
+ # allow_headers=["*"],
14
+ # )
15
+
16
+ # model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
17
+
18
+ # class Profile(BaseModel):
19
+ # name: str
20
+ # budget: float
21
+ # lifestyle: dict
22
+ # interests: list
23
+
24
+ # class CompatibilityRequest(BaseModel):
25
+ # user_profile: Profile
26
+ # candidate_profiles: list[Profile]
27
+
28
+ # @app.post("/compute_compatibility")
29
+ # def compute_compatibility(data: CompatibilityRequest):
30
+ # scores = []
31
+ # user_text = f"Budget: {data.user_profile.budget}, Lifestyle: {data.user_profile.lifestyle}, Interests: {', '.join(data.user_profile.interests)}"
32
+ # user_embedding = model.encode(user_text, convert_to_tensor=True)
33
+
34
+ # for candidate in data.candidate_profiles:
35
+ # candidate_text = f"Budget: {candidate.budget}, Lifestyle: {candidate.lifestyle}, Interests: {', '.join(candidate.interests)}"
36
+ # candidate_embedding = model.encode(candidate_text, convert_to_tensor=True)
37
+
38
+ # similarity_score = util.pytorch_cos_sim(user_embedding, candidate_embedding).item()
39
+ # match_reasons = []
40
+
41
+ # if similarity_score > 0.7:
42
+ # match_reasons.append("Strong compatibility based on overall profile match")
43
+ # elif similarity_score > 0.4:
44
+ # match_reasons.append("Moderate compatibility with some common aspects")
45
+ # else:
46
+ # match_reasons.append("Low compatibility due to differing aspects")
47
+
48
+ # scores.append({
49
+ # "profile": candidate.name,
50
+ # "compatibility": round(similarity_score * 100),
51
+ # "matchReasons": match_reasons
52
+ # })
53
+
54
+ # return {"all_matches": scores}
55
+
56
+ # if __name__ == "__main__":
57
+ # import uvicorn
58
+ # uvicorn.run("master:app", host="127.0.0.1", port=8000, reload=True)
59
+
60
+ from fastapi import FastAPI
61
+ from fastapi.middleware.cors import CORSMiddleware
62
+ from pydantic import BaseModel
63
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
64
+ import torch
65
+
66
+ app = FastAPI()
67
+
68
+ app.add_middleware(
69
+ CORSMiddleware,
70
+ allow_origins=["http://localhost:5173"],
71
+ allow_credentials=True,
72
+ allow_methods=["*"],
73
+ allow_headers=["*"],
74
+ )
75
+
76
+ model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
77
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
78
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
79
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
80
+
81
+ class Profile(BaseModel):
82
+ name: str
83
+ budget: float
84
+ lifestyle: dict
85
+ interests: list
86
+
87
+ class CompatibilityRequest(BaseModel):
88
+ user_profile: Profile
89
+ candidate_profiles: list[Profile]
90
+
91
+ @app.post("/compute_compatibility")
92
+ def compute_compatibility(data: CompatibilityRequest):
93
+ scores = []
94
+ user_text = f"Budget: {data.user_profile.budget}, Lifestyle: {data.user_profile.lifestyle}, Interests: {', '.join(data.user_profile.interests)}"
95
+
96
+ for candidate in data.candidate_profiles:
97
+ candidate_text = f"Budget: {candidate.budget}, Lifestyle: {candidate.lifestyle}, Interests: {', '.join(candidate.interests)}"
98
+
99
+ prompt = f"Compare the following profiles and rate their compatibility from 0 to 100:\nUser: {user_text}\nCandidate: {candidate_text}\nCompatibility Score:"
100
+ response = generator(prompt, max_length=50, do_sample=True)
101
+ compatibility_score = int(''.join(filter(str.isdigit, response[0]["generated_text"])))
102
+
103
+ scores.append({
104
+ "profile": candidate.name,
105
+ "compatibility": compatibility_score,
106
+ "matchReasons": f"Generated by Llama-3 based on textual profile similarities"
107
+ })
108
+
109
+ return {"all_matches": scores}
110
+
111
+ if __name__ == "__main__":
112
+ import uvicorn
113
+ uvicorn.run("master:app", host="127.0.0.1", port=8000, reload=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch