Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Initialize more lightweight models
|
| 8 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=-1) # Use CPU
|
| 9 |
+
sentence_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 10 |
+
|
| 11 |
+
# Simple in-memory user storage (replace with proper database in production)
|
| 12 |
+
users = {}
|
| 13 |
+
|
| 14 |
+
def get_embedding(text):
|
| 15 |
+
return sentence_model.encode(text)
|
| 16 |
+
|
| 17 |
+
def calculate_job_match(job_description, user_data):
|
| 18 |
+
job_embedding = get_embedding(job_description)
|
| 19 |
+
user_embedding = get_embedding(user_data)
|
| 20 |
+
similarity = cosine_similarity([job_embedding], [user_embedding])[0][0]
|
| 21 |
+
return similarity
|
| 22 |
+
|
| 23 |
+
def register(username, password, email):
|
| 24 |
+
if username in users:
|
| 25 |
+
return "Username already exists"
|
| 26 |
+
users[username] = {"password": password, "email": email, "user_data": ""}
|
| 27 |
+
return "Registered successfully"
|
| 28 |
+
|
| 29 |
+
def login(username, password):
|
| 30 |
+
if username not in users or users[username]["password"] != password:
|
| 31 |
+
return "Invalid username or password"
|
| 32 |
+
return "Logged in successfully"
|
| 33 |
+
|
| 34 |
+
def update_profile(username, email, user_data):
|
| 35 |
+
if username not in users:
|
| 36 |
+
return "User not found"
|
| 37 |
+
users[username]["email"] = email
|
| 38 |
+
users[username]["user_data"] = user_data
|
| 39 |
+
return "Profile updated successfully"
|
| 40 |
+
|
| 41 |
+
def generate_text(prompt, max_length=150, min_length=50):
|
| 42 |
+
summary = summarizer(prompt, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text']
|
| 43 |
+
return summary
|
| 44 |
+
|
| 45 |
+
def generate_cv(username, job_description):
|
| 46 |
+
if username not in users:
|
| 47 |
+
return "User not found"
|
| 48 |
+
user_data = users[username]["user_data"]
|
| 49 |
+
match_score = calculate_job_match(job_description, user_data)
|
| 50 |
+
|
| 51 |
+
prompt = f"Generate a CV based on the following job description: {job_description}\nUser data: {user_data}\nJob match score: {match_score:.2f}"
|
| 52 |
+
generated_cv = generate_text(prompt, max_length=300, min_length=100)
|
| 53 |
+
|
| 54 |
+
return f"Generated CV:\n\n{generated_cv}\n\nJob Match Score: {match_score:.2f}"
|
| 55 |
+
|
| 56 |
+
def generate_cover_letter(username, job_description):
|
| 57 |
+
if username not in users:
|
| 58 |
+
return "User not found"
|
| 59 |
+
user_data = users[username]["user_data"]
|
| 60 |
+
match_score = calculate_job_match(job_description, user_data)
|
| 61 |
+
|
| 62 |
+
prompt = f"Generate a cover letter based on the following job description: {job_description}\nUser data: {user_data}\nJob match score: {match_score:.2f}"
|
| 63 |
+
cover_letter = generate_text(prompt, max_length=250, min_length=100)
|
| 64 |
+
|
| 65 |
+
return f"Generated Cover Letter:\n\n{cover_letter}\n\nJob Match Score: {match_score:.2f}"
|
| 66 |
+
|
| 67 |
+
def prepare_interview(username, job_description):
|
| 68 |
+
if username not in users:
|
| 69 |
+
return "User not found"
|
| 70 |
+
user_data = users[username]["user_data"]
|
| 71 |
+
match_score = calculate_job_match(job_description, user_data)
|
| 72 |
+
|
| 73 |
+
prompt = f"Generate 5 potential interview questions based on the following job description: {job_description}\nUser data: {user_data}\nJob match score: {match_score:.2f}"
|
| 74 |
+
interview_questions = generate_text(prompt, max_length=200, min_length=100)
|
| 75 |
+
|
| 76 |
+
return f"Potential Interview Questions:\n\n{interview_questions}\n\nJob Match Score: {match_score:.2f}"
|
| 77 |
+
|
| 78 |
+
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("# Advanced Personalized CV Generator")
|
| 80 |
+
|
| 81 |
+
with gr.Tab("Register"):
|
| 82 |
+
register_username = gr.Textbox(label="Username")
|
| 83 |
+
register_password = gr.Textbox(label="Password", type="password")
|
| 84 |
+
register_email = gr.Textbox(label="Email")
|
| 85 |
+
register_button = gr.Button("Register")
|
| 86 |
+
register_output = gr.Textbox(label="Output")
|
| 87 |
+
register_button.click(register, inputs=[register_username, register_password, register_email], outputs=register_output)
|
| 88 |
+
|
| 89 |
+
with gr.Tab("Login"):
|
| 90 |
+
login_username = gr.Textbox(label="Username")
|
| 91 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 92 |
+
login_button = gr.Button("Login")
|
| 93 |
+
login_output = gr.Textbox(label="Output")
|
| 94 |
+
login_button.click(login, inputs=[login_username, login_password], outputs=login_output)
|
| 95 |
+
|
| 96 |
+
with gr.Tab("Update Profile"):
|
| 97 |
+
update_username = gr.Textbox(label="Username")
|
| 98 |
+
update_email = gr.Textbox(label="Email")
|
| 99 |
+
update_user_data = gr.Textbox(label="Professional Information")
|
| 100 |
+
update_button = gr.Button("Update Profile")
|
| 101 |
+
update_output = gr.Textbox(label="Output")
|
| 102 |
+
update_button.click(update_profile, inputs=[update_username, update_email, update_user_data], outputs=update_output)
|
| 103 |
+
|
| 104 |
+
with gr.Tab("Generate CV"):
|
| 105 |
+
cv_username = gr.Textbox(label="Username")
|
| 106 |
+
cv_job_description = gr.Textbox(label="Job Description")
|
| 107 |
+
cv_button = gr.Button("Generate CV")
|
| 108 |
+
cv_output = gr.Textbox(label="Generated CV")
|
| 109 |
+
cv_button.click(generate_cv, inputs=[cv_username, cv_job_description], outputs=cv_output)
|
| 110 |
+
|
| 111 |
+
with gr.Tab("Generate Cover Letter"):
|
| 112 |
+
cl_username = gr.Textbox(label="Username")
|
| 113 |
+
cl_job_description = gr.Textbox(label="Job Description")
|
| 114 |
+
cl_button = gr.Button("Generate Cover Letter")
|
| 115 |
+
cl_output = gr.Textbox(label="Generated Cover Letter")
|
| 116 |
+
cl_button.click(generate_cover_letter, inputs=[cl_username, cl_job_description], outputs=cl_output)
|
| 117 |
+
|
| 118 |
+
with gr.Tab("Prepare for Interview"):
|
| 119 |
+
int_username = gr.Textbox(label="Username")
|
| 120 |
+
int_job_description = gr.Textbox(label="Job Description")
|
| 121 |
+
int_button = gr.Button("Generate Interview Questions")
|
| 122 |
+
int_output = gr.Textbox(label="Interview Questions")
|
| 123 |
+
int_button.click(prepare_interview, inputs=[int_username, int_job_description], outputs=int_output)
|
| 124 |
+
|
| 125 |
+
demo.launch()
|