Spaces:
Build error
Build error
| import gradio as gr | |
| from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
| import spacy | |
| # Load GPT-2 model and tokenizer from Hugging Face | |
| model_name = "gpt2" # You can experiment with different models, e.g., "t5-small" | |
| model = GPT2LMHeadModel.from_pretrained(model_name) | |
| tokenizer = GPT2Tokenizer.from_pretrained(model_name) | |
| # Load SpaCy's small English model for text processing | |
| nlp = spacy.load('en_core_web_sm') | |
| # Preprocess the input text | |
| def preprocess_text(text): | |
| # Process the text using SpaCy | |
| doc = nlp(text) | |
| processed_text = " ".join([token.text for token in doc if not token.is_stop and not token.is_punct]) | |
| return processed_text | |
| # Function to generate the cover letter based on input job description and user profile | |
| def generate_cover_letter(job_description, user_profile): | |
| # Combine job description and user profile into a single prompt | |
| prompt = f"Job Description: {job_description}\n\nUser Profile: {user_profile}\n\nCover Letter:" | |
| # Encode the prompt | |
| inputs = tokenizer(prompt, return_tensors="pt") | |
| # Generate the cover letter text | |
| outputs = model.generate(inputs['input_ids'], max_length=500, num_return_sequences=1, no_repeat_ngram_size=2, top_p=0.95, temperature=0.7) | |
| # Decode the generated text | |
| cover_letter = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return cover_letter | |
| # Create Gradio interface | |
| def gradio_interface(job_description, user_profile): | |
| return generate_cover_letter(job_description, user_profile) | |
| # Set up Gradio UI | |
| iface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs=[ | |
| gr.Textbox(label="Job Description", placeholder="Enter job description here...", lines=5), | |
| gr.Textbox(label="User Profile (Qualifications, Experience, Certificates)", placeholder="Enter your qualifications, experience, and certificates...", lines=5) | |
| ], | |
| outputs="text", | |
| title="Cover Letter Generator", | |
| description="Enter a job description and your profile, and the bot will generate a personalized cover letter." | |
| ) | |
| # Launch the interface (for Hugging Face deployment) | |
| iface.launch(share=True) | |