from flask import Flask, request, jsonify, render_template from chains import Chain from portfolio import Portfolio from utils import clean_text from langchain_community.document_loaders import WebBaseLoader app = Flask(__name__) chain = Chain() portfolio = Portfolio() @app.route('/') def index(): return render_template('index.html') @app.route('/generate-email', methods=['POST']) def generate_email(): url = request.form.get('url') if not url: return jsonify({"error": "URL is required"}), 400 try: # Load the webpage content loader = WebBaseLoader([url]) data = clean_text(loader.load().pop().page_content) # Load the portfolio into the vector database portfolio.load_portfolio() # Extract jobs from the cleaned text (use the first job found) jobs = chain.extract_jobs(data) if not jobs: return jsonify({"error": "No jobs found on the provided URL"}), 404 # Generate a single email for the first job job = jobs[0] # Take the first job if multiple are found skills = job.get('skills', []) links = portfolio.query_links(skills) if not links: links = "No relevant portfolio links found." email = chain.write_mail(job, links) return jsonify({"email": email}) except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(debug=True)