Spaces:
Sleeping
Sleeping
thoristhor
commited on
Commit
β’
7bb6784
1
Parent(s):
5ff6970
Update app.py
Browse files
app.py
CHANGED
@@ -2,98 +2,7 @@ import os
|
|
2 |
from typing import Optional, Tuple
|
3 |
import gradio as gr
|
4 |
import pinecone
|
5 |
-
from
|
6 |
-
from langchain.vectorstores import Pinecone
|
7 |
-
from query_data import get_chain
|
8 |
-
from threading import Lock
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
"""Set the api key and return chain.
|
13 |
-
If no api_key, then None is returned.
|
14 |
-
"""
|
15 |
-
if api_key:
|
16 |
-
os.environ["OPENAI_API_KEY"] = api_key
|
17 |
-
chain = get_chain(vectorstore)
|
18 |
-
return chain
|
19 |
-
|
20 |
-
class ChatWrapper:
|
21 |
-
|
22 |
-
def __init__(self):
|
23 |
-
self.lock = Lock()
|
24 |
-
def __call__(
|
25 |
-
self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain
|
26 |
-
):
|
27 |
-
"""Execute the chat functionality."""
|
28 |
-
self.lock.acquire()
|
29 |
-
try:
|
30 |
-
history = history or []
|
31 |
-
# If chain is None, that is because no API key was provided.
|
32 |
-
if chain is None:
|
33 |
-
history.append((inp, "Please paste your OpenAI key to use"))
|
34 |
-
return history, history
|
35 |
-
# Set OpenAI key
|
36 |
-
import openai
|
37 |
-
openai.api_key = api_key
|
38 |
-
# Run chain and append input.
|
39 |
-
output = chain({"question": inp, "chat_history": history})["answer"]
|
40 |
-
history.append((inp, output))
|
41 |
-
except Exception as e:
|
42 |
-
raise e
|
43 |
-
finally:
|
44 |
-
self.lock.release()
|
45 |
-
return history, history
|
46 |
-
|
47 |
-
chat = ChatWrapper()
|
48 |
-
|
49 |
-
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
50 |
-
|
51 |
-
with block:
|
52 |
-
with gr.Row():
|
53 |
-
gr.Markdown("<h3><center>Chat-Your-Data (State-of-the-Union)</center></h3>")
|
54 |
-
|
55 |
-
openai_api_key_textbox = gr.Textbox(
|
56 |
-
placeholder="Paste your OpenAI API key (sk-...)",
|
57 |
-
show_label=False,
|
58 |
-
lines=1,
|
59 |
-
type="password",
|
60 |
-
)
|
61 |
-
|
62 |
-
chatbot = gr.Chatbot()
|
63 |
-
|
64 |
-
with gr.Row():
|
65 |
-
message = gr.Textbox(
|
66 |
-
label="What's your question?",
|
67 |
-
placeholder="Ask questions about the most recent state of the union",
|
68 |
-
lines=1,
|
69 |
-
)
|
70 |
-
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
71 |
-
|
72 |
-
gr.Examples(
|
73 |
-
examples=[
|
74 |
-
"What did the president say about Kentaji Brown Jackson",
|
75 |
-
"Did he mention Stephen Breyer?",
|
76 |
-
"What was his stance on Ukraine",
|
77 |
-
],
|
78 |
-
inputs=message,
|
79 |
-
)
|
80 |
-
|
81 |
-
gr.HTML("Demo application of a LangChain chain.")
|
82 |
-
|
83 |
-
gr.HTML(
|
84 |
-
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a></center>"
|
85 |
-
)
|
86 |
-
|
87 |
-
state = gr.State()
|
88 |
-
agent_state = gr.State()
|
89 |
-
|
90 |
-
submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
91 |
-
message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
92 |
-
|
93 |
-
openai_api_key_textbox.change(
|
94 |
-
set_openai_api_key,
|
95 |
-
inputs=[openai_api_key_textbox],
|
96 |
-
outputs=[agent_state],
|
97 |
-
)
|
98 |
-
|
99 |
-
block.launch(debug=True)
|
|
|
2 |
from typing import Optional, Tuple
|
3 |
import gradio as gr
|
4 |
import pinecone
|
5 |
+
from gpt_index import GPTIndexMemory, GPTPineconeIndex
|
|
|
|
|
|
|
6 |
|
7 |
+
os.environ['OPENAI_API_KEY']
|
8 |
+
os.environ['PINECONE_API_KEY']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|