Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ from typing import Optional, Tuple
|
|
3 |
|
4 |
import gradio as gr
|
5 |
from langchain.agents import create_pandas_dataframe_agent
|
6 |
-
from langchain.llms import
|
7 |
import pandas as pd
|
8 |
from threading import Lock
|
9 |
|
@@ -13,20 +13,20 @@ def load_data():
|
|
13 |
|
14 |
def load_chain():
|
15 |
"""Logic for loading the chain you want to use should go here."""
|
16 |
-
llm =
|
17 |
-
|
18 |
-
chain = create_pandas_dataframe_agent(llm, trans)
|
19 |
return chain
|
20 |
|
21 |
-
|
|
|
22 |
"""Set the api key and return chain.
|
23 |
|
24 |
If no api_key, then None is returned.
|
25 |
"""
|
26 |
if api_key:
|
27 |
-
os.environ["
|
28 |
chain = load_chain()
|
29 |
-
os.environ["
|
30 |
return chain
|
31 |
|
32 |
class ChatWrapper:
|
@@ -42,11 +42,11 @@ class ChatWrapper:
|
|
42 |
history = history or []
|
43 |
# If chain is None, that is because no API key was provided.
|
44 |
if chain is None:
|
45 |
-
history.append((inp, "Please paste your
|
46 |
return history, history
|
47 |
-
# Set
|
48 |
-
import
|
49 |
-
|
50 |
# Run chain and append input.
|
51 |
output = chain.run(input=inp)
|
52 |
history.append((inp, output))
|
@@ -64,8 +64,8 @@ with block:
|
|
64 |
with gr.Row():
|
65 |
gr.Markdown("<h3><center>LangChain Demo</center></h3>")
|
66 |
|
67 |
-
|
68 |
-
placeholder="Paste your
|
69 |
show_label=False,
|
70 |
lines=1,
|
71 |
type="password",
|
@@ -76,16 +76,16 @@ with block:
|
|
76 |
with gr.Row():
|
77 |
message = gr.Textbox(
|
78 |
label="What's your question?",
|
79 |
-
placeholder="What
|
80 |
lines=1,
|
81 |
)
|
82 |
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
83 |
|
84 |
gr.Examples(
|
85 |
examples=[
|
86 |
-
"
|
87 |
-
"What
|
88 |
-
"
|
89 |
],
|
90 |
inputs=message,
|
91 |
)
|
@@ -99,12 +99,12 @@ with block:
|
|
99 |
state = gr.State()
|
100 |
agent_state = gr.State()
|
101 |
|
102 |
-
submit.click(chat, inputs=[
|
103 |
-
message.submit(chat, inputs=[
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
inputs=[
|
108 |
outputs=[agent_state],
|
109 |
)
|
110 |
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
from langchain.agents import create_pandas_dataframe_agent
|
6 |
+
from langchain.llms import OpenAI
|
7 |
import pandas as pd
|
8 |
from threading import Lock
|
9 |
|
|
|
13 |
|
14 |
def load_chain():
|
15 |
"""Logic for loading the chain you want to use should go here."""
|
16 |
+
llm = OpenAI(temperature=0)
|
17 |
+
chain = create_pandas_dataframe_agent(llm, load_data())
|
|
|
18 |
return chain
|
19 |
|
20 |
+
|
21 |
+
def set_openai_api_key(api_key: str):
|
22 |
"""Set the api key and return chain.
|
23 |
|
24 |
If no api_key, then None is returned.
|
25 |
"""
|
26 |
if api_key:
|
27 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
28 |
chain = load_chain()
|
29 |
+
os.environ["OPENAI_API_KEY"] = ""
|
30 |
return chain
|
31 |
|
32 |
class ChatWrapper:
|
|
|
42 |
history = history or []
|
43 |
# If chain is None, that is because no API key was provided.
|
44 |
if chain is None:
|
45 |
+
history.append((inp, "Please paste your OpenAI key to use"))
|
46 |
return history, history
|
47 |
+
# Set OpenAI key
|
48 |
+
import openai
|
49 |
+
openai.api_key = api_key
|
50 |
# Run chain and append input.
|
51 |
output = chain.run(input=inp)
|
52 |
history.append((inp, output))
|
|
|
64 |
with gr.Row():
|
65 |
gr.Markdown("<h3><center>LangChain Demo</center></h3>")
|
66 |
|
67 |
+
openai_api_key_textbox = gr.Textbox(
|
68 |
+
placeholder="Paste your OpenAI API key (sk-...)",
|
69 |
show_label=False,
|
70 |
lines=1,
|
71 |
type="password",
|
|
|
76 |
with gr.Row():
|
77 |
message = gr.Textbox(
|
78 |
label="What's your question?",
|
79 |
+
placeholder="What's the answer to life, the universe, and everything?",
|
80 |
lines=1,
|
81 |
)
|
82 |
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
83 |
|
84 |
gr.Examples(
|
85 |
examples=[
|
86 |
+
"Hi! How's it going?",
|
87 |
+
"What should I do tonight?",
|
88 |
+
"Whats 2 + 2?",
|
89 |
],
|
90 |
inputs=message,
|
91 |
)
|
|
|
99 |
state = gr.State()
|
100 |
agent_state = gr.State()
|
101 |
|
102 |
+
submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
103 |
+
message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
104 |
|
105 |
+
openai_api_key_textbox.change(
|
106 |
+
set_openai_api_key,
|
107 |
+
inputs=[openai_api_key_textbox],
|
108 |
outputs=[agent_state],
|
109 |
)
|
110 |
|