Spaces:
Sleeping
Sleeping
Create chat.py
Browse files
chat.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install -q openai
|
2 |
+
!pip install -q gradio
|
3 |
+
!pip install gradio
|
4 |
+
!pip install --quiet gradio
|
5 |
+
!pip install transformers
|
6 |
+
!pip install python-dotenv
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from transformers import TFAutoModelForCausalLM, AutoTokenizer
|
10 |
+
import openai
|
11 |
+
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
import os
|
14 |
+
load_dotenv() # load environment variables from .env file
|
15 |
+
api_key = os.getenv("OPENAI_API_KEY") # access the value of the OPENAI_API_KEY environment variable
|
16 |
+
|
17 |
+
def openai_chat(prompt):
|
18 |
+
completions = openai.Completion.create(
|
19 |
+
engine="text-davinci-003",
|
20 |
+
prompt=prompt,
|
21 |
+
max_tokens=1024,
|
22 |
+
n=1,
|
23 |
+
temperature=0.5,
|
24 |
+
)
|
25 |
+
|
26 |
+
message = completions.choices[0].text
|
27 |
+
return message.strip()
|
28 |
+
|
29 |
+
def chatbot(input, history=[]):
|
30 |
+
output = openai_chat(input)
|
31 |
+
history.append((input, output))
|
32 |
+
return history, history
|
33 |
+
|
34 |
+
gr.Interface(fn = chatbot,
|
35 |
+
inputs = ["text",'state'],
|
36 |
+
outputs = ["chatbot",'state']).launch(debug = True)
|