Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| def generate_cover_letter(resume, job_desc): | |
| prompt = f""" | |
| You are a career coach. Write a tailored, ATS-optimized, professional cover letter based on the resume and job description below. | |
| Resume: | |
| {resume} | |
| Job Description: | |
| {job_desc} | |
| The letter should be concise, engaging, and show why the candidate is a good fit. | |
| """ | |
| try: | |
| response = requests.post( | |
| "https://piapi.ai/api/deepseek", | |
| headers={"Content-Type": "application/json"}, | |
| json={ | |
| "prompt": prompt, | |
| "system": "You are a helpful assistant.", | |
| "temperature": 0.8 | |
| }, | |
| timeout=20 # prevent hanging | |
| ) | |
| # Check if the response body is valid JSON | |
| if response.status_code == 200: | |
| try: | |
| data = response.json() | |
| return data.get("response", "β οΈ API responded but didn't return any text.") | |
| except Exception as e: | |
| return f"β JSON decode failed β maybe HTML or empty: {str(e)}\nRaw response:\n{response.text}" | |
| else: | |
| return f"β API returned error code {response.status_code}:\n{response.text}" | |
| except requests.exceptions.RequestException as e: | |
| return f"β Request failed:\n{str(e)}" | |
| # Gradio UI | |
| gr.Interface( | |
| fn=generate_cover_letter, | |
| inputs=[ | |
| gr.Textbox(label="Paste Your Resume", lines=12, placeholder="Paste your resume here..."), | |
| gr.Textbox(label="Paste Job Description", lines=10, placeholder="Paste the job description here...") | |
| ], | |
| outputs=gr.Textbox(label="Generated Cover Letter"), | |
| title="AI Cover Letter Generator (DeepSeek LLM)", | |
| description="Paste your resume and job description. Click 'Submit' to generate a personalized, professional cover letter using DeepSeek via PiAPI (no key needed)." | |
| ).launch() | |