Spaces:
Running
Running
Commit
·
1ddb5bb
1
Parent(s):
055edff
Changing structure and adding cactus requirements
Browse files- app.py +53 -60
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,63 +1,56 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 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 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from cactus.agent import Cactus
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
+
|
| 7 |
+
MODEL_API_KEY_MAP = {
|
| 8 |
+
"gpt-3.5-turbo": "OPENAI_API_KEY",
|
| 9 |
+
"gpt-4": "OPENAI_API_KEY",
|
| 10 |
+
"claude-3-haiku-20240307": "ANTHROPIC_API_KEY",
|
| 11 |
+
"claude-3-opus-20240229": "ANTHROPIC_API_KEY",
|
| 12 |
+
"claude-3-sonnet-20240229": "ANTHROPIC_API_KEY",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def agent_fn(prompt, api_key=None):
|
| 17 |
+
env_key_name = MODEL_API_KEY_MAP.get(model_name)
|
| 18 |
+
if env_key_name:
|
| 19 |
+
if api_key:
|
| 20 |
+
os.environ[env_key_name] = api_key
|
| 21 |
+
else:
|
| 22 |
+
return f"Error: API key not found for {model_name}. Please provide it."
|
| 23 |
+
try:
|
| 24 |
+
cactus_model = Cactus(model_name, model_type="api")
|
| 25 |
+
response = cactus_model.run(prompt)
|
| 26 |
+
return response
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {e}"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as iface:
|
| 32 |
+
with gr.Row():
|
| 33 |
+
model_name_dropdown = gr.Dropdown(
|
| 34 |
+
list(MODEL_API_KEY_MAP.keys()), label="Select Model"
|
| 35 |
+
)
|
| 36 |
+
api_key_input = gr.Textbox(label="Enter API Key", type="password")
|
| 37 |
+
prompt_input = gr.Textbox(
|
| 38 |
+
lines=2, placeholder="Enter your cheminformatics question..."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
response_output = gr.Textbox(label="Agent Response")
|
| 42 |
+
|
| 43 |
+
# Event Triggers
|
| 44 |
+
model_name_dropdown.change(
|
| 45 |
+
lambda x: gr.update(visible=MODEL_API_KEY_MAP.get(x) is not None),
|
| 46 |
+
model_name_dropdown,
|
| 47 |
+
api_key_input,
|
| 48 |
+
)
|
| 49 |
+
api_key_input.change(
|
| 50 |
+
agent_fn, [prompt_input, model_name_dropdown, api_key_input], response_output
|
| 51 |
+
)
|
| 52 |
+
prompt_input.change(
|
| 53 |
+
agent_fn, [prompt_input, model_name_dropdown, api_key_input], response_output
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
huggingface_hub==0.22.2
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.22.2
|
| 2 |
+
git+https://github.com/pnnl/cactus.git@main#egg=cactus
|