eth-chatgpt / app.py
sean1's picture
Create app.py
71e5459
raw
history blame
2.98 kB
import os
from typing import Optional, Tuple
import gradio as gr
from langchain.chains import create_pandas_dataframe_agent
from langchain.llms import Anthropic
import pandas as pd
def load_data():
"""Load the data you want to use for the chain."""
return pd.read_parquet("data/part.17.parquet")
def load_chain(trans: pd.DataFrame) -> create_pandas_dataframe_agent:
"""Logic for loading the chain you want to use should go here."""
llm = Anthropic(temperature=0)
trans = load_data()
return create_pandas_dataframe_agent(llm,
trans,
verbose=True)
def set_anthropic_api_key(api_key: str):
"""Set the api key and return chain.
If no api_key, then None is returned.
"""
if api_key:
os.environ["ANTHROPIC_API_KEY"] = api_key
chain = load_chain()
os.environ["ANTHROPIC_API_KEY"] = ""
return chain
def chat(
inp: str, history: Optional[Tuple[str, str]], chain: Optional[create_pandas_dataframe_agent]
):
"""Execute the chat functionality."""
history = history or []
# If chain is None, that is because no API key was provided.
if chain is None:
history.append((inp, "Please paste your Anthropic key to use"))
return history, history
# Run chain and append input.
output = chain.run(input=inp)
history.append((inp, output))
return history, history
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
with block:
with gr.Row():
gr.Markdown("<h3><center>LangChain Demo</center></h3>")
anthropic_api_key_textbox = gr.Textbox(
placeholder="Paste your Anthropic API key (sk-...)",
show_label=False,
lines=1,
type="password",
)
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(
label="What's your question?",
placeholder="What was the average gas price on 2019-01-22?",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.Examples(
examples=[
"Which to_address spent the most gas?",
"What was the average gas price on 2019-01-22?",
"How many unique addresses were sending transactions on 2019-01-22?",
],
inputs=message,
)
gr.HTML("Demo application of a LangChain chain.")
gr.HTML(
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain πŸ¦œοΈπŸ”—</a></center>"
)
state = gr.State()
agent_state = gr.State()
submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
anthropic_api_key_textbox.change(
set_anthropic_api_key,
inputs=[anthropic_api_key_textbox],
outputs=[agent_state],
)
block.launch(debug=True)