Spaces:
Sleeping
Sleeping
File size: 3,640 Bytes
1ddb5bb da76316 1ddb5bb da76316 1ddb5bb 72f4061 1ddb5bb 82fd84a 1ddb5bb 29212c7 1ddb5bb 82fd84a 6f48f60 82fd84a 1ddb5bb bfb9104 82fd84a bfb9104 82fd84a 6f48f60 82fd84a 72f4061 bfb9104 1ddb5bb 82fd84a 1ddb5bb 72f4061 db547d9 1ddb5bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import os
import gradio as gr
from cactus.agent import Cactus
from huggingface_hub import InferenceClient
MODEL_API_KEY_MAP = {
"gpt-3.5-turbo": "OPENAI_API_KEY",
"gpt-4": "OPENAI_API_KEY",
"gemini-pro": "GOOGLE_API_KEY",
"claude-3-haiku-20240307": "ANTHROPIC_API_KEY",
"claude-3-opus-20240229": "ANTHROPIC_API_KEY",
"claude-3-sonnet-20240229": "ANTHROPIC_API_KEY",
}
TITLE = """CACTUS 🌵: Chemistry Agent Connecting Tool-Usage to Science"""
DESC = """
CACTUS is an innovative tool-augmented language model designed to assist researchers and chemists in various chemistry-related tasks. By integrating state-of-the-art language models with a suite of powerful cheminformatics tools, CACTUS provides an intelligent and efficient solution for exploring chemical space, predicting molecular properties, and accelerating drug discovery. Just as the cactus thrives in the harsh desert environment, adapting to limited resources and extreme conditions, CACTUS has been implemented by Pacific Northwest National Laboratory (PNNL) Scientists to navigate the complex landscape of chemical data and extract valuable insights.
"""
CITE = """**Citation**
If you use CACTUS in your research, please cite our preprint:
```bibtex
@article{mcnaughton2024cactus,
title={CACTUS: Chemistry Agent Connecting Tool-Usage to Science},
author={Andrew D. McNaughton and Gautham Ramalaxmi and Agustin Kruel and Carter R. Knutson and Rohith A. Varikoti and Neeraj Kumar},
year={2024},
eprint={2405.00972},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
```"""
def agent_fn(prompt, model_name, api_key=None):
env_key_name = MODEL_API_KEY_MAP.get(model_name)
if env_key_name:
if api_key:
os.environ[env_key_name] = api_key
else:
return f"Error: API key not found for {model_name}. Please provide it."
try:
cactus_model = Cactus(model_name, model_type="api")
response = cactus_model.run(prompt)
return response
except Exception as e:
return f"Error: {e}"
with gr.Blocks(title=TITLE) as iface:
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("# " + TITLE)
gr.Markdown(DESC)
with gr.Row():
model_name_dropdown = gr.Dropdown(
list(MODEL_API_KEY_MAP.keys()), label="Select Model"
)
api_key_input = gr.Textbox(label="Enter API Key", type="password")
prompt_input = gr.Textbox(
lines=2, placeholder="Enter your cheminformatics question..."
)
with gr.Row(): # Row for sample questions
sample_questions = [
"What is the molecular weight of the SMILES: CCO ?",
"Does the SMILES: C1CCOC1 , trigger the PAINS filter?",
"Identify the molecular weight, logP, and Topological Polar Surface Area (TPSA) of the molecule with the SMILES notation CC(=O)NC1=CC=C(O)C=C1",
]
for question in sample_questions:
gr.Button(question).click(
lambda q=question: gr.update(value=q),
None,
prompt_input,
)
with gr.Row():
submit_button = gr.Button("Submit", variant="primary")
response_output = gr.Textbox(label="Agent Response")
gr.Markdown(CITE)
# Event Triggers
model_name_dropdown.change(
lambda x: gr.update(visible=MODEL_API_KEY_MAP.get(x) is not None),
model_name_dropdown,
api_key_input,
)
submit_button.click(
agent_fn,
[prompt_input, model_name_dropdown, api_key_input],
response_output,
)
iface.launch()
|