adeshg commited on
Commit
82d2259
1 Parent(s): 33ea38b

Upload 2 files

Browse files
Files changed (2) hide show
  1. gradio_langchain.py +96 -0
  2. requirements.txt +3 -0
gradio_langchain.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ gradio_langchain.py:
3
+ Assignment#8 - Lancgchain and gradio example
4
+ '''
5
+
6
+ import os
7
+ from typing import Optional, Tuple
8
+
9
+ import gradio as gr
10
+ from langchain.chains import ConversationChain
11
+ from langchain.llms import OpenAI
12
+ from threading import Lock
13
+
14
+ import openai
15
+
16
+ """Logic for loading the chain you want to use should go here."""
17
+ def load_chain():
18
+ llm = OpenAI(temperature=0)
19
+ chain = ConversationChain(llm=llm)
20
+ return chain
21
+
22
+ def set_openai_api_key(api_key: str):
23
+ """Set the api key and return chain. If no api_key, then None is returned."""
24
+
25
+ if api_key:
26
+ os.environ["OPENAI_API_KEY"] = api_key
27
+ chain = load_chain()
28
+ os.environ["OPENAI_API_KEY"] = ""
29
+ return chain
30
+
31
+ class ChatWrapper:
32
+ """ main class to start chain, create / append history. it also acquires lock mechanism """
33
+
34
+ def __init__(self):
35
+ self.lock = Lock()
36
+ def __call__(
37
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
38
+ ):
39
+ """Execute the chat functionality."""
40
+ self.lock.acquire()
41
+ try:
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
+
49
+ openai.api_key = api_key
50
+ # Run chain and append input.
51
+ output = chain.run(input=inp)
52
+ history.append((inp, output))
53
+ except Exception as e:
54
+ raise e
55
+ finally:
56
+ self.lock.release()
57
+ return history, history
58
+
59
+ chat = ChatWrapper()
60
+
61
+ block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
62
+
63
+ with block:
64
+ with gr.Row():
65
+ gr.Markdown("<h3><center>LangChain / Gradio example </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",
72
+ )
73
+
74
+ chatbot = gr.Chatbot()
75
+
76
+ with gr.Row():
77
+ message = gr.Textbox(
78
+ label="What's your question?",
79
+ placeholder="Please enter your question here",
80
+ lines=1,
81
+ )
82
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
83
+
84
+ state = gr.State()
85
+ agent_state = gr.State()
86
+
87
+ submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
88
+ message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
89
+
90
+ openai_api_key_textbox.change(
91
+ set_openai_api_key,
92
+ inputs=[openai_api_key_textbox],
93
+ outputs=[agent_state],
94
+ )
95
+
96
+ block.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ gradio
3
+ langchain