thoristhor commited on
Commit
ee83059
1 Parent(s): ca04f47

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ If no api_key, then None is returned.
20
+ """
21
+ if api_key:
22
+ os.environ["OPENAI_API_KEY"] = api_key
23
+ chain = load_chain()
24
+ os.environ["OPENAI_API_KEY"] = ""
25
+ return chain
26
+
27
+ class ChatWrapper:
28
+
29
+ def __init__(self):
30
+ self.lock = Lock()
31
+ def __call__(
32
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
33
+ ):
34
+ """Execute the chat functionality."""
35
+ self.lock.acquire()
36
+ try:
37
+ history = history or []
38
+ # If chain is None, that is because no API key was provided.
39
+ if chain is None:
40
+ history.append((inp, "Please paste your OpenAI key to use"))
41
+ return history, history
42
+ # Set OpenAI key
43
+ import openai
44
+ openai.api_key = api_key
45
+ # Run chain and append input.
46
+ output = chain.run(input=inp)
47
+ history.append((inp, output))
48
+ except Exception as e:
49
+ raise e
50
+ finally:
51
+ self.lock.release()
52
+ return history, history
53
+
54
+ chat = ChatWrapper()
55
+
56
+ block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
57
+
58
+ with block:
59
+ with gr.Row():
60
+ gr.Markdown("<h3><center>LangChain Demo</center></h3>")
61
+
62
+ openai_api_key_textbox = gr.Textbox(
63
+ placeholder="Paste your OpenAI API key (sk-...)",
64
+ show_label=False,
65
+ lines=1,
66
+ type="password",
67
+ )
68
+
69
+ chatbot = gr.Chatbot()
70
+
71
+ with gr.Row():
72
+ message = gr.Textbox(
73
+ label="Treat it like ChatGPT",
74
+ placeholder="Buat soalan darjah enam tentang biologi",
75
+ lines=1,
76
+ )
77
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
78
+
79
+ gr.Examples(
80
+ examples=[
81
+ "Siapakah PM Malaysia",
82
+ "create multiple choice question around chair?",
83
+ "Whats 2 + 2?",
84
+ ],
85
+ inputs=message,
86
+ )
87
+
88
+
89
+
90
+ state = gr.State()
91
+ agent_state = gr.State()
92
+
93
+ submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
94
+ message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
95
+
96
+ openai_api_key_textbox.change(
97
+ set_openai_api_key,
98
+ inputs=[openai_api_key_textbox],
99
+ outputs=[agent_state],
100
+ )
101
+
102
+ block.launch(debug=True)