Spaces:
Sleeping

File size: 3,347 Bytes
ec73764
 
18d644f
 
8921c91
28e1aad
18d644f
38b0566
 
28e1aad
38b0566
28e1aad
 
cea9b88
8921c91
28e1aad
8a2bbb5
28e1aad
18d644f
 
cea9b88
 
 
8312f3f
18d644f
cea9b88
 
 
 
 
 
 
18d644f
 
8921c91
cea9b88
 
18d644f
cea9b88
 
 
 
18d644f
 
8921c91
 
18d644f
8a2bbb5
18d644f
 
 
cea9b88
18d644f
cea9b88
18d644f
 
8a2bbb5
18d644f
 
8921c91
 
8312f3f
8921c91
18d644f
cea9b88
 
 
 
8921c91
 
 
 
 
 
 
cea9b88
 
 
 
 
8921c91
 
cea9b88
 
8921c91
 
ec73764
 
8a2bbb5
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
import gradio as gr
from transformers import pipeline
from huggingface_hub import InferenceClient
import os

# --- Model and Client Setup ---

# βœ… Locally Hosted Model (google/flan-t5-base)
# A stable, instruction-tuned model known to work on free CPU tiers.
try:
    local_generator = pipeline("text2text-generation", model="google/flan-t5-base")
except Exception as e:
    print(f"Failed to load local model: {e}")
    local_generator = None

# βœ… API-based Model (Llama-3.1-8B-Instruct)
api_token = os.environ.get("HF")
api_client = InferenceClient("meta-llama/Llama-3.1-8B-Instruct", token=api_token)

# --- Single Prediction Function ---
def generate_cover_letter(resume_text, job_description, model_choice):
    if not resume_text or not job_description:
        return "⚠️ Please provide both a resume and a job description."

    prompt = (
        f"You are a cover letter writing assistant. Write a professional cover letter "
        f"that highlights how the applicant's resume is a perfect fit for the job description. "
        f"Be concise and professional. The applicant's resume is below:\n\n"
        f"Resume:\n{resume_text}\n\n"
        f"The job description is below:\n\n"
        f"Job Description:\n{job_description}\n\n"
        f"Cover Letter:"
    )

    if model_choice == "Locally Hosted":
        if not local_generator:
            return "❌ Local model failed to load. Check Space logs."
        try:
            # The local model uses a specific prompt format
            input_text = f"resume: {resume_text} job_description: {job_description}"
            cover_letter = local_generator(input_text, max_new_tokens=500)[0]['generated_text']
            return cover_letter.strip()
        except Exception as e:
            return f"❌ Local model error: {e}"

    elif model_choice == "API-Based":
        if not api_token:
            return "❌ API token not found. Please set the 'HF' secret in your Space settings."
        try:
            response = api_client.text_generation(
                prompt=prompt,
                max_new_tokens=500,
                do_sample=True,
                temperature=0.7,
                return_full_text=False
            )
            return response.strip()
        except Exception as e:
            return f"❌ API Error: {e}"

    else:
        return "⚠️ Please select a model version."

# --- Gradio UI ---
with gr.Blocks(title="Cover Letter Generator") as demo:
    gr.Markdown("# πŸš€ AI-Powered Cover Letter Generator")
    gr.Markdown("Select a model to generate a custom cover letter from your resume and a job description.")

    with gr.Row():
        model_dropdown = gr.Dropdown(
            ["Locally Hosted", "API-Based"],
            label="Select Model",
            value="Locally Hosted"
        )
    with gr.Row():
        resume_input = gr.Textbox(lines=5, label="Your Resume (Text)")
        job_description_input = gr.Textbox(lines=5, label="Job Description (Text)")
    
    submit_btn = gr.Button("Generate Cover Letter", variant="primary")
    output_textbox = gr.Textbox(label="Generated Cover Letter", lines=10)

    submit_btn.click(
        fn=generate_cover_letter,
        inputs=[resume_input, job_description_input, model_dropdown],
        outputs=output_textbox
    )

if __name__ == "__main__":
    demo.launch()