Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,39 @@
|
|
1 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import TFAutoModelForCausalLM, AutoTokenizer
|
4 |
import openai
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
import os
|
7 |
-
|
8 |
-
#
|
9 |
-
load_dotenv()
|
10 |
-
|
11 |
-
# access the value of the OPENAI_API_KEY environment variable
|
12 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
13 |
-
|
14 |
-
# install required packages
|
15 |
-
subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv", "openai"])
|
16 |
|
17 |
def openai_chat(prompt):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
def chatbot(input, history=[]):
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
# define Gradio interface
|
35 |
-
inputs = [
|
36 |
-
gr.inputs.Textbox(label="Input"),
|
37 |
-
gr.outputs.Label(type="hidden", label="Chat history", default=[])
|
38 |
-
]
|
39 |
-
outputs = [
|
40 |
-
gr.outputs.Textbox(label="Output"),
|
41 |
-
gr.outputs.Label(type="hidden", label="Updated chat history")
|
42 |
-
]
|
43 |
-
|
44 |
-
iface = gr.Interface(fn=chatbot,
|
45 |
-
inputs=inputs,
|
46 |
-
outputs=outputs,
|
47 |
-
title="OpenAI Chatbot",
|
48 |
-
description="A chatbot powered by OpenAI's GPT-3",
|
49 |
-
theme="compact")
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
1 |
import subprocess
|
2 |
+
subprocess.check_call(["pip", "install", "-q openai"])
|
3 |
+
subprocess.check_call(["pip", "install", "-q gradio", "transformers","python-dotenv","--quiet gradio"])
|
4 |
+
|
5 |
+
#!pip install -q openai
|
6 |
+
#!pip install -q gradio
|
7 |
+
#!pip install gradio
|
8 |
+
#!pip install --quiet gradio
|
9 |
+
#!pip install transformers
|
10 |
+
#!pip install python-dotenv
|
11 |
+
|
12 |
import gradio as gr
|
13 |
from transformers import TFAutoModelForCausalLM, AutoTokenizer
|
14 |
import openai
|
15 |
+
|
16 |
from dotenv import load_dotenv
|
17 |
import os
|
18 |
+
load_dotenv() # load environment variables from .env file
|
19 |
+
api_key = os.getenv("OPENAI_API_KEY") # access the value of the OPENAI_API_KEY environment variable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def openai_chat(prompt):
|
22 |
+
completions = openai.Completion.create(
|
23 |
+
engine="text-davinci-003",
|
24 |
+
prompt=prompt,
|
25 |
+
max_tokens=1024,
|
26 |
+
n=1,
|
27 |
+
temperature=0.5,
|
28 |
+
)
|
29 |
+
|
30 |
+
message = completions.choices[0].text
|
31 |
+
return message.strip()
|
|
|
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)
|