Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +107 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import Optional, Tuple
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
from langchain.chains import ConversationChain
|
6 |
+
from langchain.llms import OpenAI
|
7 |
+
from threading import Lock
|
8 |
+
|
9 |
+
|
10 |
+
def load_chain():
|
11 |
+
"""Logic for loading the chain you want to use should go here."""
|
12 |
+
llm = OpenAI(temperature=0)
|
13 |
+
chain = ConversationChain(llm=llm)
|
14 |
+
return chain
|
15 |
+
|
16 |
+
|
17 |
+
def set_openai_api_key(api_key: str):
|
18 |
+
"""Set the api key and return chain.
|
19 |
+
|
20 |
+
If no api_key, then None is returned.
|
21 |
+
"""
|
22 |
+
if api_key:
|
23 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
24 |
+
chain = load_chain()
|
25 |
+
os.environ["OPENAI_API_KEY"] = ""
|
26 |
+
return chain
|
27 |
+
|
28 |
+
class ChatWrapper:
|
29 |
+
|
30 |
+
def __init__(self):
|
31 |
+
self.lock = Lock()
|
32 |
+
def __call__(
|
33 |
+
self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
|
34 |
+
):
|
35 |
+
"""Execute the chat functionality."""
|
36 |
+
self.lock.acquire()
|
37 |
+
try:
|
38 |
+
history = history or []
|
39 |
+
# If chain is None, that is because no API key was provided.
|
40 |
+
if chain is None:
|
41 |
+
history.append((inp, "Please paste your OpenAI key to use"))
|
42 |
+
return history, history
|
43 |
+
# Set OpenAI key
|
44 |
+
import openai
|
45 |
+
openai.api_key = api_key
|
46 |
+
# Run chain and append input.
|
47 |
+
output = chain.run(input=inp)
|
48 |
+
history.append((inp, output))
|
49 |
+
except Exception as e:
|
50 |
+
raise e
|
51 |
+
finally:
|
52 |
+
self.lock.release()
|
53 |
+
return history, history
|
54 |
+
|
55 |
+
chat = ChatWrapper()
|
56 |
+
|
57 |
+
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
58 |
+
|
59 |
+
with block:
|
60 |
+
with gr.Row():
|
61 |
+
gr.Markdown("<h3><center>LangChain Demo</center></h3>")
|
62 |
+
|
63 |
+
openai_api_key_textbox = gr.Textbox(
|
64 |
+
placeholder="Paste your OpenAI API key (sk-...)",
|
65 |
+
show_label=False,
|
66 |
+
lines=1,
|
67 |
+
type="password",
|
68 |
+
)
|
69 |
+
|
70 |
+
chatbot = gr.Chatbot()
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
message = gr.Textbox(
|
74 |
+
label="What's your question?",
|
75 |
+
placeholder="What's the answer to life, the universe, and everything?",
|
76 |
+
lines=1,
|
77 |
+
)
|
78 |
+
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
79 |
+
|
80 |
+
gr.Examples(
|
81 |
+
examples=[
|
82 |
+
"Hi! How's it going?",
|
83 |
+
"What should I do tonight?",
|
84 |
+
"Whats 2 + 2?",
|
85 |
+
],
|
86 |
+
inputs=message,
|
87 |
+
)
|
88 |
+
|
89 |
+
gr.HTML("Demo application of a LangChain chain.")
|
90 |
+
|
91 |
+
gr.HTML(
|
92 |
+
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a></center>"
|
93 |
+
)
|
94 |
+
|
95 |
+
state = gr.State()
|
96 |
+
agent_state = gr.State()
|
97 |
+
|
98 |
+
submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
99 |
+
message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
|
100 |
+
|
101 |
+
openai_api_key_textbox.change(
|
102 |
+
set_openai_api_key,
|
103 |
+
inputs=[openai_api_key_textbox],
|
104 |
+
outputs=[agent_state],
|
105 |
+
)
|
106 |
+
|
107 |
+
block.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|
3 |
+
langchain
|