sean1 commited on
Commit
d9e01bd
·
1 Parent(s): ce46abb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
app.py CHANGED
@@ -11,44 +11,43 @@ def load_data():
11
  """Load the data you want to use for the agent."""
12
  return pd.read_parquet("data/part.17.parquet")
13
 
14
- def load_agent(trans: pd.DataFrame) -> create_pandas_dataframe_agent:
15
- """Logic for loading the agent we want to use."""
16
  llm = Anthropic(temperature=0)
17
- trans = load_data()
18
- return create_pandas_dataframe_agent(llm,
19
- trans,
20
- verbose=True)
21
 
22
  def set_anthropic_api_key(api_key: str):
23
- """Set the api key and return agent.
 
24
  If no api_key, then None is returned.
25
  """
26
  if api_key:
27
  os.environ["ANTHROPIC_API_KEY"] = api_key
28
- agent = load_agent()
29
  os.environ["ANTHROPIC_API_KEY"] = ""
30
- return agent
31
-
32
 
33
  class ChatWrapper:
34
 
35
  def __init__(self):
36
  self.lock = Lock()
37
  def __call__(
38
- self, api_key: str, inp: str, history: Optional[Tuple[str, str]], agent: Optional[create_pandas_dataframe_agent]
39
  ):
40
  """Execute the chat functionality."""
41
  self.lock.acquire()
42
  try:
43
  history = history or []
44
  # If chain is None, that is because no API key was provided.
45
- if agent is None:
46
  history.append((inp, "Please paste your anthropic key to use"))
47
  return history, history
48
- # Set OpenAI key
49
- agent = set_anthropic_api_key(history)
 
50
  # Run chain and append input.
51
- output = agent.run(input=inp)
52
  history.append((inp, output))
53
  except Exception as e:
54
  raise e
@@ -58,7 +57,6 @@ class ChatWrapper:
58
 
59
  chat = ChatWrapper()
60
 
61
-
62
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
63
 
64
  with block:
@@ -66,7 +64,7 @@ with block:
66
  gr.Markdown("<h3><center>LangChain Demo</center></h3>")
67
 
68
  anthropic_api_key_textbox = gr.Textbox(
69
- placeholder="Paste your Anthropic API key (sk-...)",
70
  show_label=False,
71
  lines=1,
72
  type="password",
@@ -91,7 +89,7 @@ with block:
91
  inputs=message,
92
  )
93
 
94
- gr.HTML("Demo application of a LangChain agent.")
95
 
96
  gr.HTML(
97
  "<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
@@ -100,8 +98,8 @@ with block:
100
  state = gr.State()
101
  agent_state = gr.State()
102
 
103
- submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
104
- message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
105
 
106
  anthropic_api_key_textbox.change(
107
  set_anthropic_api_key,
@@ -109,4 +107,4 @@ with block:
109
  outputs=[agent_state],
110
  )
111
 
112
- block.launch(debug=True)
 
11
  """Load the data you want to use for the agent."""
12
  return pd.read_parquet("data/part.17.parquet")
13
 
14
+ def load_chain():
15
+ """Logic for loading the chain you want to use should go here."""
16
  llm = Anthropic(temperature=0)
17
+ chain = create_pandas_dataframe_agent(llm=llm, load_data())
18
+ return chain
 
 
19
 
20
  def set_anthropic_api_key(api_key: str):
21
+ """Set the api key and return chain.
22
+
23
  If no api_key, then None is returned.
24
  """
25
  if api_key:
26
  os.environ["ANTHROPIC_API_KEY"] = api_key
27
+ chain = load_chain()
28
  os.environ["ANTHROPIC_API_KEY"] = ""
29
+ return chain
 
30
 
31
  class ChatWrapper:
32
 
33
  def __init__(self):
34
  self.lock = Lock()
35
  def __call__(
36
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
37
  ):
38
  """Execute the chat functionality."""
39
  self.lock.acquire()
40
  try:
41
  history = history or []
42
  # If chain is None, that is because no API key was provided.
43
+ if chain is None:
44
  history.append((inp, "Please paste your anthropic key to use"))
45
  return history, history
46
+ # Set anthropic key
47
+ import anthropic
48
+ anthropic.api_key = api_key
49
  # Run chain and append input.
50
+ output = chain.run(input=inp)
51
  history.append((inp, output))
52
  except Exception as e:
53
  raise e
 
57
 
58
  chat = ChatWrapper()
59
 
 
60
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
61
 
62
  with block:
 
64
  gr.Markdown("<h3><center>LangChain Demo</center></h3>")
65
 
66
  anthropic_api_key_textbox = gr.Textbox(
67
+ placeholder="Paste your anthropic API key (sk-...)",
68
  show_label=False,
69
  lines=1,
70
  type="password",
 
89
  inputs=message,
90
  )
91
 
92
+ gr.HTML("Demo application of a LangChain chain.")
93
 
94
  gr.HTML(
95
  "<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
 
98
  state = gr.State()
99
  agent_state = gr.State()
100
 
101
+ submit.click(chat, inputs=[anthropic_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
102
+ message.submit(chat, inputs=[anthropic_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
103
 
104
  anthropic_api_key_textbox.change(
105
  set_anthropic_api_key,
 
107
  outputs=[agent_state],
108
  )
109
 
110
+ block.launch(debug=True)