Spaces:
Sleeping
Sleeping
thoristhor
commited on
Commit
β’
9acfdb2
1
Parent(s):
f324902
Update app.py
Browse files
app.py
CHANGED
@@ -12,97 +12,34 @@ OPENAI_API_KEY=os.environ.get("OPENAI_API_KEY")
|
|
12 |
PINECONE_API_KEY=os.environ.get("PINECONE_API_KEY")
|
13 |
PINECONE_ENV=os.environ.get("PINECONE_ENV")
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
self.lock.acquire()
|
38 |
-
try:
|
39 |
-
history = history or []
|
40 |
-
# If chain is None, that is because no API key was provided.
|
41 |
-
if chain is None:
|
42 |
-
history.append((inp, "Please paste your OpenAI key to use"))
|
43 |
-
return history, history
|
44 |
-
# Set OpenAI key
|
45 |
-
import openai
|
46 |
-
openai.api_key = api_key
|
47 |
-
# Run chain and append input.
|
48 |
-
output = chain({"question": inp, "chat_history": history})["answer"]
|
49 |
-
history.append((inp, output))
|
50 |
-
except Exception as e:
|
51 |
-
raise e
|
52 |
-
finally:
|
53 |
-
self.lock.release()
|
54 |
-
return history, history
|
55 |
-
|
56 |
-
chat = ChatWrapper()
|
57 |
-
|
58 |
-
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
59 |
-
|
60 |
-
with block:
|
61 |
-
with gr.Row():
|
62 |
-
gr.Markdown("<h3><center>Chat-Your-Data (State-of-the-Union)</center></h3>")
|
63 |
-
|
64 |
-
openai_api_key_textbox = gr.Textbox(
|
65 |
-
placeholder="Paste your OpenAI API key (sk-...)",
|
66 |
-
show_label=False,
|
67 |
-
lines=1,
|
68 |
-
type="password",
|
69 |
-
)
|
70 |
-
|
71 |
chatbot = gr.Chatbot()
|
|
|
72 |
|
73 |
with gr.Row():
|
74 |
-
|
75 |
-
label="What's your question?",
|
76 |
-
placeholder="Ask questions about the most recent state of the union",
|
77 |
-
lines=1,
|
78 |
-
)
|
79 |
-
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
80 |
-
|
81 |
-
gr.Examples(
|
82 |
-
examples=[
|
83 |
-
"What did the president say about Kentaji Brown Jackson",
|
84 |
-
"Did he mention Stephen Breyer?",
|
85 |
-
"What was his stance on Ukraine",
|
86 |
-
],
|
87 |
-
inputs=message,
|
88 |
-
)
|
89 |
-
|
90 |
-
gr.HTML("Demo application of a LangChain chain.")
|
91 |
-
|
92 |
-
gr.HTML(
|
93 |
-
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a></center>"
|
94 |
-
)
|
95 |
-
|
96 |
-
state = gr.State()
|
97 |
-
agent_state = gr.State()
|
98 |
-
|
99 |
-
submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
100 |
-
message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
101 |
|
102 |
-
|
103 |
-
set_openai_api_key,
|
104 |
-
inputs=[openai_api_key_textbox],
|
105 |
-
outputs=[agent_state],
|
106 |
-
)
|
107 |
|
108 |
-
|
|
|
12 |
PINECONE_API_KEY=os.environ.get("PINECONE_API_KEY")
|
13 |
PINECONE_ENV=os.environ.get("PINECONE_ENV")
|
14 |
|
15 |
+
pindex=pinecone.Index("sethgodin")
|
16 |
+
pinedex=GPTPineconeIndex([], pinecone_index=pindex)
|
17 |
+
|
18 |
+
tools = [
|
19 |
+
Tool(
|
20 |
+
name = "GPT Index",
|
21 |
+
func=lambda q: str(pinedex.query(q)),
|
22 |
+
description="useful for when you want to answer questions about the author. The input to this tool should be a complete english sentence.",
|
23 |
+
return_direct=True
|
24 |
+
),
|
25 |
+
]
|
26 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
27 |
+
llm=OpenAI(temperature=0)
|
28 |
+
agent_chain = initialize_agent(tools, llm, agent="conversational-react-description", memory=memory)
|
29 |
+
|
30 |
+
def predict(input, history=[]):
|
31 |
+
# generate a response
|
32 |
+
history = agent_chain.run(input=input)
|
33 |
+
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
34 |
+
return response, history
|
35 |
+
|
36 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
chatbot = gr.Chatbot()
|
38 |
+
state = gr.State([])
|
39 |
|
40 |
with gr.Row():
|
41 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
txt.submit(predict, [txt, state], [chatbot, state])
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
demo.launch()
|