ShermanAI commited on
Commit
517a606
·
1 Parent(s): 69fafb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -1,21 +1,21 @@
1
  import subprocess
2
  subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"])
3
  subprocess.check_call(["pip", "install", "-q", "openai"])
4
- #!pip install -q openai
5
- #!pip install -q gradio
6
- #!pip install gradio
7
- #!pip install --quiet gradio
8
- #!pip install transformers
9
- #!pip install python-dotenv
10
-
11
  import gradio as gr
12
  from transformers import TFAutoModelForCausalLM, AutoTokenizer
13
  import openai
14
-
15
  from dotenv import load_dotenv
16
  import os
17
- load_dotenv() # load environment variables from .env file
18
- api_key = os.getenv("OPENAI_API_KEY") # access the value of the OPENAI_API_KEY environment variable
 
 
 
 
 
 
 
19
 
20
  def openai_chat(prompt):
21
  completions = openai.Completion.create(
@@ -32,8 +32,24 @@ def openai_chat(prompt):
32
  def chatbot(input, history=[]):
33
  output = openai_chat(input)
34
  history.append((input, output))
35
- return history, history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- gr.Interface(fn = chatbot,
38
- inputs = ["text",'state'],
39
- outputs = ["chatbot",'state']).launch(debug = True)
 
1
  import subprocess
2
  subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"])
3
  subprocess.check_call(["pip", "install", "-q", "openai"])
4
+ import subprocess
 
 
 
 
 
 
5
  import gradio as gr
6
  from transformers import TFAutoModelForCausalLM, AutoTokenizer
7
  import openai
 
8
  from dotenv import load_dotenv
9
  import os
10
+
11
+ # load environment variables from .env file
12
+ load_dotenv()
13
+
14
+ # access the value of the OPENAI_API_KEY environment variable
15
+ api_key = os.getenv("OPENAI_API_KEY")
16
+
17
+ # install required packages
18
+ subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv", "openai"])
19
 
20
  def openai_chat(prompt):
21
  completions = openai.Completion.create(
 
32
  def chatbot(input, history=[]):
33
  output = openai_chat(input)
34
  history.append((input, output))
35
+ return output, history
36
+
37
+ # define Gradio interface
38
+ inputs = [
39
+ gr.inputs.Textbox(label="Input"),
40
+ gr.inputs.Hidden(label="Chat history", default=[])
41
+ ]
42
+ outputs = [
43
+ gr.outputs.Textbox(label="Output"),
44
+ gr.outputs.Hidden(label="Updated chat history")
45
+ ]
46
+
47
+ iface = gr.Interface(fn=chatbot,
48
+ inputs=inputs,
49
+ outputs=outputs,
50
+ title="OpenAI Chatbot",
51
+ description="A chatbot powered by OpenAI's GPT-3",
52
+ theme="compact")
53
 
54
+ # launch the interface
55
+ iface.launch()