sean1 commited on
Commit
d35962e
·
1 Parent(s): 3df06f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -29,19 +29,33 @@ def set_anthropic_api_key(api_key: str):
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
 
46
 
47
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
 
29
  return agent
30
 
31
 
32
+ class ChatWrapper:
33
+
34
+ def __init__(self):
35
+ self.lock = Lock()
36
+ def __call__(
37
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], agent: Optional[create_pandas_dataframe_agent]
38
+ ):
39
+ """Execute the chat functionality."""
40
+ self.lock.acquire()
41
+ try:
42
+ history = history or []
43
+ # If chain is None, that is because no API key was provided.
44
+ if agent is None:
45
+ history.append((inp, "Please paste your OpenAI key to use"))
46
+ return history, history
47
+ # Set OpenAI key
48
+ agent = set_anthropic_api_key(api_key)
49
+ # Run chain and append input.
50
+ output = agent.run(input=inp)
51
+ history.append((inp, output))
52
+ except Exception as e:
53
+ raise e
54
+ finally:
55
+ self.lock.release()
56
  return history, history
57
+
58
+ chat = ChatWrapper()
 
 
59
 
60
 
61
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")