p-baleine commited on
Commit
0d58bdf
β€’
1 Parent(s): 453f410

add gradio sample app.

Browse files
Files changed (2) hide show
  1. app.py +107 -0
  2. requirements.txt +12 -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,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.0.161
2
+ pydantic==1.10.7
3
+ joblib==1.2.0
4
+ arxiv==1.4.7
5
+ pdfminer.six==20221105
6
+ tqdm==4.65.0
7
+ faiss-cpu==1.7.4
8
+ google-search-results==2.4.2
9
+ openai==0.27.6
10
+ tiktoken==0.3.3
11
+ spacy==3.5.2
12
+ en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0-py3-none-any.whl