#!/usr/bin/env python
# coding: utf-8
# In[35]:
import os
import jinja2
#import openai
import pdfkit
#import random
#import re
#import sys
import numpy as np
import gradio as gr
#from zipfile import ZipFile
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.output_parsers import ResponseSchema,PydanticOutputParser,StructuredOutputParser
from langchain import PromptTemplate
from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.output_parsers import CommaSeparatedListOutputParser
from pydantic import BaseModel, Field, validator
from typing import List
from langchain.text_splitter import CharacterTextSplitter,RecursiveCharacterTextSplitter
output_parser = CommaSeparatedListOutputParser()
from datetime import datetime
# In[2]:
c_splitter = CharacterTextSplitter(chunk_size = 450,chunk_overlap = 0,separator = ' ')
r_splitter = RecursiveCharacterTextSplitter(chunk_size = 80,chunk_overlap = 0,separators = ["\n\n"])
# In[3]:
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()
# In[56]:
#定义常用函数
#公司生成函数
def company_cr(target_company,language,key):
tar_com_prompt = """ List 6 competitors' name of {company} in {lang}.\n{format_instructions}
"""
chat_0_3 = ChatOpenAI(temperature = 0.0,openai_api_key = key)
pTem_tar_c = ChatPromptTemplate.from_template(tar_com_prompt)
prompt_c = pTem_tar_c.format_messages(company = target_company,lang = language,format_instructions = format_instructions)
#print(prompt_c)
mock_exp_company = chat_0_3(prompt_c)
#print(mock_exp_company)
mock_exp_company_fin = mock_exp_company.content.split(',')
return mock_exp_company_fin
#职位生成函数
def job_cr(target_job,language,key):
tar_job_prompt = """ List 2 similar names of this {job} in {lang}.\n{format_instructions}
"""
chat_0_3 = ChatOpenAI(temperature = 0.0,openai_api_key = key)
pTem_tar_j = ChatPromptTemplate.from_template(tar_job_prompt)
prompt_j = pTem_tar_j.format_messages(job = target_job,lang = language,format_instructions = format_instructions)
mock_exp_job = chat_0_3(prompt_j)
#print(mock_exp_job)
mock_exp_job_fin = mock_exp_job.content.split(',')
#print(mock_exp_job_fin)
return mock_exp_job_fin
#工作一级技能函数
def skill_pri_cr(job_req,key):
tar_skill_pri_prompt = """ Assume you are the recruiter, read the {job_requirement}, then list 20 tech terms you want to see on candidates resume.\n{format_instructions}
"""
chat_0_4 = ChatOpenAI(temperature = 0.0,model_name = 'gpt-4',openai_api_key = key)
pTem_tar_skill = ChatPromptTemplate.from_template(tar_skill_pri_prompt)
prompt_skill = pTem_tar_skill.format_messages(job_requirement = job_req,format_instructions = format_instructions)
mock_skill_pri = chat_0_4(prompt_skill)
#print(mock_skill_pri)
mock_skill_pri_fin = mock_skill_pri.content.split(',')
#print(mock_skill_pri_fin)
return mock_skill_pri_fin
#工作二级技能函数
def skill_sec_cr(skill_pri,language,key):
tar_skill_sec_prompt = """ Assume you are the recruiter, read the skill sets in {skill_pri}, list 5 related tech terms for each skill set you want to see on candidates resume in {lang}.\n{format_instructions}
"""
chat_0_4 = ChatOpenAI(temperature = 0.0,model_name = 'gpt-4',openai_api_key = key)
#chat_0_3 = ChatOpenAI(temperature = 0.0,openai_api_key = key)
pTem_tar_skill_sec = ChatPromptTemplate.from_template(tar_skill_sec_prompt)
prompt_skill_sec = pTem_tar_skill_sec.format_messages(skill_pri = skill_pri,lang = language,format_instructions = format_instructions)
#mock_skill_sec = chat_0_3(prompt_skill_sec)
mock_skill_sec = chat_0_4(prompt_skill_sec)
#print(mock_skill_sec)
mock_skill_sec_fin = mock_skill_sec.content.split(',')
#print(mock_skill_sec_fin)
return mock_skill_sec_fin
#社团生成函数
def soc_cr(target_job,language,key):
tar_soc_prompt = """ If a student want to become a {job_name} after graduation, list 5 possible school clubs or competitions the student wants to join in {lang}.\n{format_instructions}
"""
chat_0_4 = ChatOpenAI(temperature = 0.0,model_name = 'gpt-4',openai_api_key = key)
pTem_tar_soc = ChatPromptTemplate.from_template(tar_soc_prompt)
prompt_soc = pTem_tar_soc.format_messages(job_name = target_job,lang = language,format_instructions = format_instructions)
mock_exp_soc = chat_0_4(prompt_soc)
#print(mock_exp_soc)
mock_exp_soc_fin = mock_exp_soc.content.split(',')
#print(mock_exp_soc_fin)
return mock_exp_soc_fin
#工作简历生成函数
def work_exp(mock_company,mock_title,mock_skill,language,key):
working_exp_prompt = """ Write the working experience in {lang}. Considering my working experience in {company} as a {title}, and I am good at {skill_sec}, write a part of resume for me. A good resume reorganizes the skill set in 3 to 4 examples. Each example must include 60 to 80 words. A good resume must not use pronouns. Each example must have detail numbers to describe result. List each example in a new line without serial numbers. """
chat_0_4 = ChatOpenAI(temperature = 0.0,model_name = 'gpt-4',openai_api_key = key)
pTem_tar_working_exp = ChatPromptTemplate.from_template(working_exp_prompt)
prompt_working_exp = pTem_tar_working_exp.format_messages(company = mock_company,lang = language,title = mock_title,skill_sec = mock_skill)
mock_working_exp = chat_0_4(prompt_working_exp)
#print(mock_working_exp)
mock_working_exp_fin = r_splitter.split_text(mock_working_exp.content)
#print(mock_working_exp_fin)
result = [mock_working_exp_fin[0],mock_working_exp_fin[1],mock_working_exp_fin[2]]
#print(mock_working_exp_fin[3])
#print(result)
return (mock_working_exp_fin[0],mock_working_exp_fin[1],mock_working_exp_fin[2])
def club_exp(mock_soc,mock_skill_soc,language,key):
club_exp_prompt = """ Write the school experience in {lang}. Considering my school experience in {club}, and I am good at {skill_sec_club}, write a part of resume for me. A good resume reorganizes the skill set in 3 examples. Each example must include 60 to 80 words. A good resume must not use pronouns. Each example must have detail numbers to describe result. List each example in a new line without serial numbers. """
chat_0_4 = ChatOpenAI(temperature = 0.0,model_name = 'gpt-4',openai_api_key = key)
#n_range = range(0,80)
#rand_value = random.sample(n_range,4)
#print(rand_value)
mock_skill_soc_fin = np.array(mock_skill_soc)[rand_value]
pTem_tar_club_exp = ChatPromptTemplate.from_template(club_exp_prompt)
prompt_club_exp = pTem_tar_club_exp.format_messages(club = mock_soc,lang = language,skill_sec_club = mock_skill_soc_fin)
#print(pTem_tar_club_exp)
#print(prompt_club_exp)
mock_club_exp = chat_0_4(prompt_club_exp)
#print(mock_club_exp)
mock_club_exp_fin = r_splitter.split_text(mock_club_exp.content)
#print(mock_club_exp_fin)
#print(mock_club_exp_fin[0])
return mock_club_exp_fin
#定义履历生成函数
def skill_info(job_req,language,key):
# 生成Skillset
mock_skill_pri = skill_pri_cr(job_req,key)
# 生成2级Skillset
mock_skill_sec = skill_sec_cr(mock_skill_pri,language,key)
return mock_skill_sec
#简历展示函数
def resume_header(name,mobile,email,Gra_beg,Gra_end,Gra_sch,Gra_maj,underGra_beg,underGra_end,underGra_sch,underGra_maj):
header = f"""