forked from hanjaek/Project_Master
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_app.py
More file actions
100 lines (82 loc) · 3.58 KB
/
flask_app.py
File metadata and controls
100 lines (82 loc) · 3.58 KB
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
# 필요 패키지 설치
# pip install flask
# pip install flask-cors
# pip install SQLAlchemy
# pip install mysql-connector-python
# pip install pandas
# pip install axios
from flask import Flask, jsonify, request
from flask_cors import CORS
from sqlalchemy import create_engine
import pandas as pd
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
# DB URL 설정
db_url = 'mysql+mysqlconnector://root:7070@localhost/project1'
engine = create_engine(db_url)
def get_matching_posts(user_id):
with engine.connect() as conn:
# 쿼리 실행 및 데이터 가져오기
query_member = "SELECT * FROM member"
query_project_info = "SELECT * FROM project_info WHERE status = 'active'"
# member 데이터 로드
m_df = pd.read_sql(query_member, conn)
print("Loaded member data:")
print(m_df.head())
# project_info 데이터 로드
p_df = pd.read_sql(query_project_info, conn)
print("Loaded project info data:")
print(p_df.head())
# 기술 스택 처리
m_df['interest_stack'] = m_df['interest_stack'].apply(lambda x: x.split(', ') if pd.notna(x) else [])
p_df['teck_stack'] = p_df['teck_stack'].apply(lambda x: x.split(', ') if pd.notna(x) else [])
# 새로운 사용자 필터링
matching_scores = []
if user_id == 'none':
return []
new_user = m_df[m_df['user_id'] == user_id].iloc[0]
print("new_user:", new_user)
# 매칭 점수 계산 함수
def calculate_match_score(member_tech_stack, post_tech_stack):
if not post_tech_stack:
return 0
return len(set(member_tech_stack) & set(post_tech_stack))
for post in p_df.itertuples():
print(f"Checking post: {post.project_seq}, Position: {post.position}")
print(f"user Checking job: {new_user.job}")
# 포지션이 일치하는 경우 우선 고려
score = calculate_match_score(new_user.interest_stack, post.teck_stack) # 사용 스킬 몇개 매칭되는 지
priority = 0
if new_user.job == post.position:
priority = 1
print(f"Calculated score for post {post.project_seq}: {score}")
matching_scores.append({ # 각각의 매치 점수 추가
'project_seq': post.project_seq, # project_seq 추가
'user_id': new_user.user_id,
'user_name': new_user.user_name,
'post_title': post.introduce_title,
'position': post.position,
'priority': priority,
'tech_stack_match_score': score,
'category': post.category,
'teck_stack': post.teck_stack,
'view': post.view_count,
'matched_skills': ', '.join(set(new_user.interest_stack) & set(post.teck_stack)),
'total_required_skills': len(post.teck_stack)
})
print("matching_scores:", matching_scores)
# 매치 점수 df 생성
match_df = pd.DataFrame(matching_scores)
# 매치 점수로 정렬 (높은순으로)
match_df = match_df.sort_values(by=['priority', 'tech_stack_match_score'], ascending=[False, False])
return match_df.to_dict(orient='records')
@app.route('/get_matching_posts', methods=['GET'])
def get_matching_posts_route():
user_id = request.args.get('user_id')
if user_id:
matching_posts = get_matching_posts(user_id)
return jsonify(matching_posts)
else:
return jsonify({"error": "Missing user_id"}), 400
if __name__ == '__main__':
app.run(port=2010)