Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,16 @@ import os
|
|
2 |
from typing import Optional, Tuple
|
3 |
|
4 |
import gradio as gr
|
5 |
-
from langchain.
|
6 |
from langchain.llms import Anthropic
|
7 |
import pandas as pd
|
8 |
|
9 |
def load_data():
|
10 |
-
"""Load the data you want to use for the
|
11 |
return pd.read_parquet("data/part.17.parquet")
|
12 |
|
13 |
-
def
|
14 |
-
"""Logic for loading the
|
15 |
llm = Anthropic(temperature=0)
|
16 |
trans = load_data()
|
17 |
return create_pandas_dataframe_agent(llm,
|
@@ -19,27 +19,27 @@ def load_chain(trans: pd.DataFrame) -> create_pandas_dataframe_agent:
|
|
19 |
verbose=True)
|
20 |
|
21 |
def set_anthropic_api_key(api_key: str):
|
22 |
-
"""Set the api key and return
|
23 |
If no api_key, then None is returned.
|
24 |
"""
|
25 |
if api_key:
|
26 |
os.environ["ANTHROPIC_API_KEY"] = api_key
|
27 |
-
|
28 |
os.environ["ANTHROPIC_API_KEY"] = ""
|
29 |
-
return
|
30 |
|
31 |
|
32 |
def chat(
|
33 |
-
inp: str, history: Optional[Tuple[str, str]],
|
34 |
):
|
35 |
"""Execute the chat functionality."""
|
36 |
history = history or []
|
37 |
-
# If
|
38 |
-
if
|
39 |
history.append((inp, "Please paste your Anthropic key to use"))
|
40 |
return history, history
|
41 |
-
# Run
|
42 |
-
output =
|
43 |
history.append((inp, output))
|
44 |
return history, history
|
45 |
|
@@ -76,7 +76,7 @@ with block:
|
|
76 |
inputs=message,
|
77 |
)
|
78 |
|
79 |
-
gr.HTML("Demo application of a LangChain
|
80 |
|
81 |
gr.HTML(
|
82 |
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
|
|
|
2 |
from typing import Optional, Tuple
|
3 |
|
4 |
import gradio as gr
|
5 |
+
from langchain.agents import create_pandas_dataframe_agent
|
6 |
from langchain.llms import Anthropic
|
7 |
import pandas as pd
|
8 |
|
9 |
def load_data():
|
10 |
+
"""Load the data you want to use for the agent."""
|
11 |
return pd.read_parquet("data/part.17.parquet")
|
12 |
|
13 |
+
def load_agent(trans: pd.DataFrame) -> create_pandas_dataframe_agent:
|
14 |
+
"""Logic for loading the agent we want to use."""
|
15 |
llm = Anthropic(temperature=0)
|
16 |
trans = load_data()
|
17 |
return create_pandas_dataframe_agent(llm,
|
|
|
19 |
verbose=True)
|
20 |
|
21 |
def set_anthropic_api_key(api_key: str):
|
22 |
+
"""Set the api key and return agent.
|
23 |
If no api_key, then None is returned.
|
24 |
"""
|
25 |
if api_key:
|
26 |
os.environ["ANTHROPIC_API_KEY"] = api_key
|
27 |
+
agent = load_agent()
|
28 |
os.environ["ANTHROPIC_API_KEY"] = ""
|
29 |
+
return agent
|
30 |
|
31 |
|
32 |
def chat(
|
33 |
+
inp: str, history: Optional[Tuple[str, str]], agent: Optional[create_pandas_dataframe_agent]
|
34 |
):
|
35 |
"""Execute the chat functionality."""
|
36 |
history = history or []
|
37 |
+
# If agent is None, that is because no API key was provided.
|
38 |
+
if agent is None:
|
39 |
history.append((inp, "Please paste your Anthropic key to use"))
|
40 |
return history, history
|
41 |
+
# Run agent and append input.
|
42 |
+
output = agent.run(input=inp)
|
43 |
history.append((inp, output))
|
44 |
return history, history
|
45 |
|
|
|
76 |
inputs=message,
|
77 |
)
|
78 |
|
79 |
+
gr.HTML("Demo application of a LangChain agent.")
|
80 |
|
81 |
gr.HTML(
|
82 |
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
|