edu-app-mvp / utils /api_utils.py
1yahoo's picture
Upload folder using huggingface_hub
b5dbe9d verified
Raw
History Blame Contribute Delete
2.3 kB
import requests
import os
import json
from config import HF_API_TOKEN
from models import get_model_config
def call_hf_model(prompt, task="text_generation"):
"""
Call Hugging Face Inference API with improved error handling and response parsing.
"""
if not HF_API_TOKEN:
return {"error": "API Token missing. Please set HF_API_TOKEN in .env file."}
model_cfg = get_model_config(task)
model_id = model_cfg["id"]
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
# Parameters to ensure better text generation
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": 800,
"temperature": 0.7,
"top_p": 0.9,
"return_full_text": False
}
}
try:
response = requests.post(api_url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
# Handle different response formats from HF API
if isinstance(result, list) and len(result) > 0:
if "generated_text" in result[0]:
return result[0]["generated_text"].strip()
return result[0]
return result
except requests.exceptions.HTTPError as e:
if response.status_code == 503:
return {"error": "Model is currently loading. Please try again in a few seconds."}
return {"error": f"HTTP Error: {str(e)}"}
except Exception as e:
return {"error": f"Connection Error: {str(e)}"}
def generate_educational_content(prompt_type, **kwargs):
"""
Helper to generate content based on prompts.json templates.
"""
try:
# Get absolute path to data/prompts.json
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
prompts_path = os.path.join(base_dir, "data", "prompts.json")
with open(prompts_path, "r", encoding="utf-8") as f:
prompts = json.load(f)
template = prompts.get(prompt_type, "")
formatted_prompt = template.format(**kwargs)
return call_hf_model(formatted_prompt)
except Exception as e:
return {"error": f"Prompt Error: {str(e)}"}