musically / app.py
Suman2003's picture
Update app.py
afb9014 verified
Raw History Blame Contribute Delete
1.71 kB
import os
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from huggingface_hub import InferenceClient
app = FastAPI(title="Resume Builder AI API")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
HF_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient(token=HF_TOKEN)
# Switched to a lighter, faster model that won't timeout on the free tier
MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
class SummaryRequest(BaseModel):
job_title: str
skills: list[str]
experience_level: str
@app.get("/")
def read_root():
return {"status": "AI Microservice is running"}
@app.post("/generate-summary")
def generate_summary(req: SummaryRequest):
try:
prompt = (
f"<s>[INST] You are an expert resume writer. Write a compelling, professional "
f"resume summary (3-4 sentences max) for a {req.experience_level} {req.job_title}. "
f"Incorporate these key skills naturally: {', '.join(req.skills)}. "
f"Do not include any introductory text, just output the summary paragraph directly. [/INST]"
)
response = client.text_generation(
model=MODEL_ID,
prompt=prompt,
max_new_tokens=150,
temperature=0.7,
return_full_text=False
)
return {"success": True, "summary": response.strip()}
except Exception as e:
# Print the actual error to your Hugging Face Space logs
print(f"CRITICAL ERROR: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))