Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
#import requests
|
| 6 |
+
#from dotenv import load_dotenv
|
| 7 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
#def configure():
|
| 10 |
+
#load_dotenv()
|
| 11 |
+
#openai.api_key = os.getenv
|
| 12 |
+
|
| 13 |
+
start_sequence = "\nAI:"
|
| 14 |
+
restart_sequence = "\nHuman: "
|
| 15 |
+
|
| 16 |
+
# Define the conversation prompt
|
| 17 |
+
conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
|
| 18 |
+
|
| 19 |
+
# Define the function to generate a response from the GPT model
|
| 20 |
+
def openai_chat_history(input, history):
|
| 21 |
+
history = history or []
|
| 22 |
+
if input.strip() != "":
|
| 23 |
+
s = list(sum(history, ()))
|
| 24 |
+
s.append(input)
|
| 25 |
+
inp = ' '.join(s)
|
| 26 |
+
output = openai_create(inp)
|
| 27 |
+
history.append((input, output))
|
| 28 |
+
return history[-1][1]
|
| 29 |
+
else:
|
| 30 |
+
return ""
|
| 31 |
+
|
| 32 |
+
def openai_create(prompt):
|
| 33 |
+
response = openai.Completion.create(
|
| 34 |
+
model="text-davinci-003",
|
| 35 |
+
prompt=prompt,
|
| 36 |
+
temperature=0.9,
|
| 37 |
+
max_tokens=150,
|
| 38 |
+
top_p=1,
|
| 39 |
+
frequency_penalty=0,
|
| 40 |
+
presence_penalty=0.6,
|
| 41 |
+
stop=["Human:", "AI:"]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return response.choices[0].text
|
| 45 |
+
|
| 46 |
+
block = gr.Interface(
|
| 47 |
+
fn=openai_chat_history,
|
| 48 |
+
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
|
| 49 |
+
outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
|
| 50 |
+
)
|
| 51 |
+
block.launch()
|