File size: 8,262 Bytes
41ea5e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from flask import Flask, render_template, request, jsonify, session
import re
import os
import json
import traceback
from dotenv import load_dotenv
from openai import AzureOpenAI

# Import your custom modules (make sure these are available)
try:
    from nlp_parsed import parse_recruiter_query, prompt_enhancer
    from SERP import query_making, serp_api_call
    from apify import apify_call
    from validate import validate_function, score_candidates
    from postgres_db import fetch_from_saral_data, check_completeness, data_input, cur, conn, store_prompt
    MODULES_AVAILABLE = True
except ImportError as e:
    print(f"Warning: Some modules not available: {e}")
    MODULES_AVAILABLE = False

load_dotenv()

app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY', 'your-secret-key-here')

# Mock functions for when modules aren't available (for testing)
def mock_parse_recruiter_query(query):
    return {
        "job_title": "Software Engineer",
        "skills": ["Python", "Flask"],
        "experience": "3-5",
        "location": "Mumbai",
        "work_preference": "Remote",
        "job_type": "Full-time",
        "is_indian": True
    }

def mock_prompt_enhancer(prompt):
    return f"Enhanced: {prompt} - Looking for skilled professionals"

def mock_query_making(parsed_data):
    return "https://linkedin.com/search", ["Mumbai", "Delhi"]

def mock_serp_api_call(query, start=0, results_per_page=10):
    return [f"https://linkedin.com/in/user{i}" for i in range(start, start + results_per_page)]

def mock_fetch_from_saral_data(serp_data, conn):
    return [], serp_data  # Return empty saral_data, all URLs as remaining

def mock_apify_call(serp_json):
    mock_profiles = []
    for i in range(min(5, len(serp_json))):
        mock_profiles.append({
            "fullName": f"John Doe {i+1}",
            "headline": "Software Engineer with 5+ years experience",
            "addressWithCountry": "Mumbai, India",
            "email": f"john{i+1}@example.com",
            "linkedinUrl": f"https://linkedin.com/in/johndoe{i+1}",
            "skills": [{"title": "Python"}, {"title": "Flask"}, {"title": "JavaScript"}],
            "about": "Experienced software developer with expertise in web technologies...",
            "experiences": [
                {
                    "title": "Senior Software Engineer",
                    "subtitle": "Tech Company",
                    "caption": "Jan 2020 - Present",
                    "description": [{"text": "Developed web applications using Python and Flask"}]
                }
            ],
            "profilePic": "https://via.placeholder.com/150",
            "is_complete": "Complete Profile"
        })
    return mock_profiles

def mock_validate_function(location, candidates):
    # Split candidates into matched and unmatched (80% matched, 20% unmatched)
    split_point = int(len(candidates) * 0.8)
    return candidates[:split_point], candidates[split_point:]

def mock_score_candidates(parsed_data, candidates):
    for i, candidate in enumerate(candidates):
        candidate['score'] = round(85 + (i % 15), 1)  # Scores between 85-100
    return candidates

def mock_data_input(candidates):
    pass

def mock_store_prompt(conn, prompt, parsed_data):
    pass

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/parse_query', methods=['POST'])
def parse_query():
    try:
        data = request.json
        user_input = data.get('query', '').strip()
        
        if not user_input:
            return jsonify({'error': 'Please enter a valid query'})
        
        if MODULES_AVAILABLE:
            parsed_data = parse_recruiter_query(user_input)
        else:
            parsed_data = mock_parse_recruiter_query(user_input)
        
        return jsonify({'success': True, 'parsed_data': parsed_data})
    
    except Exception as e:
        return jsonify({'error': f'Error parsing query: {str(e)}'})

@app.route('/enhance_prompt', methods=['POST'])
def enhance_prompt():
    try:
        data = request.json
        user_input = data.get('query', '').strip()
        
        if not user_input:
            return jsonify({'error': 'Please enter a valid query'})
        
        if MODULES_AVAILABLE:
            enhanced = prompt_enhancer(user_input)
        else:
            enhanced = mock_prompt_enhancer(user_input)
        
        return jsonify({'success': True, 'enhanced_query': enhanced})
    
    except Exception as e:
        return jsonify({'error': f'Error enhancing prompt: {str(e)}'})

@app.route('/search', methods=['POST'])
def search():
    try:
        data = request.json
        user_input = data.get('query', '').strip()
        current_page = data.get('page', 0)
        
        if not user_input:
            return jsonify({'error': 'Please enter a valid query'})
        
        # Parse query
        if MODULES_AVAILABLE:
            parsed_data = parse_recruiter_query(user_input)
            print(parsed_data)
        else:
            parsed_data = mock_parse_recruiter_query(user_input)
            print(parsed_data)
        
        if "error" in parsed_data:
            return jsonify({'error': parsed_data["error"]})
        
        if parsed_data.get("is_indian") == False:
            return jsonify({'error': 'Our platform is not allowing search outside of India'})
        
        # Store prompt
        if MODULES_AVAILABLE:
            store_prompt(conn, user_input, parsed_data)
        else:
            mock_store_prompt(None, user_input, parsed_data)
        
        # Get query and location
        if MODULES_AVAILABLE:
            query, location = query_making(parsed_data)
            print(query)
            
        else:
            query, location = mock_query_making(parsed_data)
            print(query)
            
        
        # Pagination
        results_per_page = 10
        start = current_page * results_per_page
        
        # Get SERP data
        if MODULES_AVAILABLE:
            serp_data = serp_api_call(query, start=start, results_per_page=results_per_page)
            saral_data, remain_urls = fetch_from_saral_data(serp_data, conn)
        else:
            serp_data = mock_serp_api_call(query, start=start, results_per_page=results_per_page)
            saral_data, remain_urls = mock_fetch_from_saral_data(serp_data, None)
        
        # Process remaining URLs with Apify
        apify_json = {}
        if len(remain_urls) >= 1:
            serp_json = {idx: url for idx, url in enumerate(remain_urls, start=1)}
            
            if MODULES_AVAILABLE:
                apify_json = apify_call(serp_json)
            else:
                apify_json = mock_apify_call(serp_json)
        
        # Combine data
        if apify_json:
            total_candidates = saral_data + apify_json
        else:
            total_candidates = saral_data
        
        # Store data
        if MODULES_AVAILABLE:
            data_input(total_candidates)
        else:
            mock_data_input(total_candidates)
        
        # Validate and score
        if MODULES_AVAILABLE:
            matched, unmatched = validate_function(location, total_candidates)
            matched = score_candidates(parsed_data, matched)
        else:
            matched, unmatched = mock_validate_function(location, total_candidates)
            matched = mock_score_candidates(parsed_data, matched)
        
        return jsonify({
            'success': True,
            'parsed_data': parsed_data,
            'matched_results': matched,
            'unmatched_results': unmatched,
            'current_page': current_page
        })
    
    except Exception as e:
        print(f"Error in search: {traceback.format_exc()}")
        return jsonify({'error': f'Search error: {str(e)}'})

# if __name__ == '__main__':
#     # Ensure templates directory exists
#     if not os.path.exists('templates'):
#         os.makedirs('templates')
    
#     app.run(debug=True, host='0.0.0.0', port=5000)