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