sean1 commited on
Commit
71e5459
·
1 Parent(s): 3a05e19

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Optional, Tuple
3
+
4
+ import gradio as gr
5
+ from langchain.chains 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 chain."""
11
+ return pd.read_parquet("data/part.17.parquet")
12
+
13
+ def load_chain(trans: pd.DataFrame) -> create_pandas_dataframe_agent:
14
+ """Logic for loading the chain you want to use should go here."""
15
+ llm = Anthropic(temperature=0)
16
+ trans = load_data()
17
+ return create_pandas_dataframe_agent(llm,
18
+ trans,
19
+ verbose=True)
20
+
21
+ def set_anthropic_api_key(api_key: str):
22
+ """Set the api key and return chain.
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
+
32
+ def chat(
33
+ inp: str, history: Optional[Tuple[str, str]], chain: Optional[create_pandas_dataframe_agent]
34
+ ):
35
+ """Execute the chat functionality."""
36
+ history = history or []
37
+ # If chain is None, that is because no API key was provided.
38
+ if chain is None:
39
+ history.append((inp, "Please paste your Anthropic key to use"))
40
+ return history, history
41
+ # Run chain and append input.
42
+ output = chain.run(input=inp)
43
+ history.append((inp, output))
44
+ return history, history
45
+
46
+
47
+ block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
48
+
49
+ with block:
50
+ with gr.Row():
51
+ gr.Markdown("<h3><center>LangChain Demo</center></h3>")
52
+
53
+ anthropic_api_key_textbox = gr.Textbox(
54
+ placeholder="Paste your Anthropic API key (sk-...)",
55
+ show_label=False,
56
+ lines=1,
57
+ type="password",
58
+ )
59
+
60
+ chatbot = gr.Chatbot()
61
+
62
+ with gr.Row():
63
+ message = gr.Textbox(
64
+ label="What's your question?",
65
+ placeholder="What was the average gas price on 2019-01-22?",
66
+ lines=1,
67
+ )
68
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
69
+
70
+ gr.Examples(
71
+ examples=[
72
+ "Which to_address spent the most gas?",
73
+ "What was the average gas price on 2019-01-22?",
74
+ "How many unique addresses were sending transactions on 2019-01-22?",
75
+ ],
76
+ inputs=message,
77
+ )
78
+
79
+ gr.HTML("Demo application of a LangChain chain.")
80
+
81
+ gr.HTML(
82
+ "<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
83
+ )
84
+
85
+ state = gr.State()
86
+ agent_state = gr.State()
87
+
88
+ submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
89
+ message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
90
+
91
+ anthropic_api_key_textbox.change(
92
+ set_anthropic_api_key,
93
+ inputs=[anthropic_api_key_textbox],
94
+ outputs=[agent_state],
95
+ )
96
+
97
+ block.launch(debug=True)