Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,19 +29,33 @@ def set_anthropic_api_key(api_key: str):
|
|
29 |
return agent
|
30 |
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
):
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
return history, history
|
41 |
-
|
42 |
-
|
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}")
|