Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import openai
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Set up OpenAI API key
|
| 8 |
+
openai.api_key = None # initialize the API key as None
|
| 9 |
+
|
| 10 |
+
# Define conversation start and restart sequences
|
| 11 |
+
start_sequence = "\nAI:"
|
| 12 |
+
restart_sequence = "\nHuman:"
|
| 13 |
+
|
| 14 |
+
# Load data from JSON file
|
| 15 |
+
url = 'https://archive.org/download/amazon_help_prepared/amazon_help_prepared.jsonl'
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
data = []
|
| 18 |
+
with open('amazon_help_prepared.jsonl', 'wb') as f:
|
| 19 |
+
f.write(response.content)
|
| 20 |
+
with open('amazon_help_prepared.jsonl', 'r', encoding='utf-8') as f:
|
| 21 |
+
for line in f:
|
| 22 |
+
data.append(json.loads(line))
|
| 23 |
+
prompt = data
|
| 24 |
+
|
| 25 |
+
# Define function to generate OpenAI response
|
| 26 |
+
def openai_create(prompt, api_key):
|
| 27 |
+
openai.api_key = api_key # set the API key
|
| 28 |
+
response = openai.Completion.create(
|
| 29 |
+
model='text-davinci-003',
|
| 30 |
+
prompt=prompt,
|
| 31 |
+
temperature=0.9,
|
| 32 |
+
max_tokens=400,
|
| 33 |
+
top_p=1,
|
| 34 |
+
frequency_penalty=0,
|
| 35 |
+
presence_penalty=0.6,
|
| 36 |
+
stop=[" Human:", " AI:"]
|
| 37 |
+
)
|
| 38 |
+
return response.choices[0].text
|
| 39 |
+
|
| 40 |
+
# Define chatbot function to keep track of conversation history
|
| 41 |
+
def chatbot(input, history=[], api_key=''):
|
| 42 |
+
output = openai_create(input, api_key)
|
| 43 |
+
history.append((input, output))
|
| 44 |
+
return history, history
|
| 45 |
+
|
| 46 |
+
# Set up UI inputs and outputs
|
| 47 |
+
api_key_input = gr.inputs.Textbox(label="OpenAI API Key")
|
| 48 |
+
chat_input = gr.inputs.Textbox(label="You:")
|
| 49 |
+
chat_output = gr.outputs.Textbox(label="Chatbot:")
|
| 50 |
+
|
| 51 |
+
# Define UI title and description
|
| 52 |
+
title = "<h1 style='text-align:center; font-weight: bold; color: #232f3e;'>Amazon Vendor Help Central</h1>"
|
| 53 |
+
description = "<p style='text-align:center; font-size: 18px; color: #333;'>Get help with common issues and questions about selling on Amazon. Our AI-powered chatbot is here to assist you 24/7.</p>"
|
| 54 |
+
|
| 55 |
+
# Create UI interface with title and description
|
| 56 |
+
gr.Interface(fn=chatbot,
|
| 57 |
+
inputs=[chat_input, "state", api_key_input],
|
| 58 |
+
outputs=[chat_output, "state"],
|
| 59 |
+
title=title,
|
| 60 |
+
description=description,
|
| 61 |
+
theme="compact"
|
| 62 |
+
).launch(debug=True)
|