from transformers import AutoAdapterModel, AutoTokenizer

model_name = "allenai/specter_plus_plus"
adapter_name = "allenai/specter2_classification"

model = AutoAdapterModel.from_pretrained(model_name)
model.load_adapter(adapter_name, source="hf")
tokenizer = AutoTokenizer.from_pretrained(model_name)

def find_expertise(text):
    inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
    with model.unfreeze_adapters():
        outputs = model.generate(**inputs)
        embeddings = outputs.last_hidden_state[:, 0, :]
        most_similar_papers = model.most_similar(embeddings)
        most_similar_areas = model.most_similar(embeddings, filter_by_area=True)
        return (
            model.get_skills(most_similar_papers[0]["paper_id"]),
            most_similar_papers[0]["paper_id"],
            most_similar_areas[0]["area_id"],
            model.get_specific_expertise(most_similar_papers[0]["paper_id"])
        )

# Example usage
text = "I am a computer scientist who specializes in natural language processing."
skills, expert_niche, expert_area_niche, specific_expertise = find_expertise(text)

print(f"Skills: {skills}")
print(f"Expert niche: {expert_niche}")
print(f"Expert area niche: {expert_area_niche}")
print(f"Specific expertise: {specific_expertise}")
Fetching 4 files: 100%|██████████| 4/4 [00:00<?, ?it/s]
Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[11], line 26
     24 # Example usage
     25 text = "I am a computer scientist who specializes in natural language processing."
---> 26 skills, expert_niche, expert_area_niche, specific_expertise = find_expertise(text)
     28 print(f"Skills: {skills}")
     29 print(f"Expert niche: {expert_niche}")

Cell In[11], line 12, in find_expertise(text)
     10 def find_expertise(text):
     11     inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
---> 12     with model.unfreeze_adapters():
     13         outputs = model.generate(**inputs)
     14         embeddings = outputs.last_hidden_state[:, 0, :]

File ~\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py:1614, in Module.__getattr__(self, name)
   1612     if name in modules:
   1613         return modules[name]
-> 1614 raise AttributeError("'{}' object has no attribute '{}'".format(
   1615  Write your response using less than 600 words.
user