test / llama_models.py
ngrigg's picture
Update model loading to use AutoModelForCausalLM
8270298
raw
history blame
1.25 kB
import os
from transformers import AutoTokenizer, AutoModelForCausalLM
import aiohttp
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
def load_model(model_name):
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name) # Use AutoModelForCausalLM for Llama
return tokenizer, model
async def process_text(model_name, text):
tokenizer, model = load_model(model_name)
prompt = f"Given the following company description, extract key products, geographies, and important keywords:\n\n{text}\n\nProducts, geographies, and keywords:"
async with aiohttp.ClientSession() as session:
async with session.post(f"https://api-inference.huggingface.co/models/{model_name}",
headers={"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"},
json={"inputs": prompt}) as response:
result = await response.json()
if isinstance(result, list) and len(result) > 0:
return result[0].get('generated_text', '').strip()
elif isinstance(result, dict):
return result.get('generated_text', '').strip()
else:
return str(result)