dishav2 commited on
Commit
c5ef960
1 Parent(s): eb5de67

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>Canvas QnA Chatbot</center></h3>")
62
+
63
+ openai_api_key_textbox = gr.Textbox(
64
+ placeholder="Paste your OpenAI API key",
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="Enter Your Graded Discussion",
75
+ placeholder="Enter the instructions of your Canvas discussion and rubric",
76
+ lines=1,
77
+ )
78
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
79
+
80
+
81
+ gr.HTML("Canvas Discussion Automated Grader")
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)