Kunal08 commited on
Commit
62a9edf
·
1 Parent(s): 8150f08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -29
app.py CHANGED
@@ -2,16 +2,14 @@ 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 = []
@@ -22,9 +20,8 @@ with open('amazon_help_prepared.jsonl', 'r', encoding='utf-8') as 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,
@@ -37,26 +34,24 @@ def openai_create(prompt, api_key):
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)
 
 
2
  import requests
3
  import openai
4
  import gradio as gr
 
5
 
6
+ openai.api_key = None
 
7
 
 
8
  start_sequence = "\nAI:"
9
  restart_sequence = "\nHuman:"
10
 
11
+ import json
12
+
13
  url = 'https://archive.org/download/amazon_help_prepared/amazon_help_prepared.jsonl'
14
  response = requests.get(url)
15
  data = []
 
20
  data.append(json.loads(line))
21
  prompt = data
22
 
23
+ def openai_creat(prompt, api_key):
24
+ openai.api_key = api_key
 
25
  response = openai.Completion.create(
26
  model='text-davinci-003',
27
  prompt=prompt,
 
34
  )
35
  return response.choices[0].text
36
 
37
+ def chatbot(input, api_key):
38
+ output = openai_creat(input, api_key)
39
+ return output
 
 
40
 
 
41
  api_key_input = gr.inputs.Textbox(label="OpenAI API Key")
42
+ text_input = gr.inputs.Textbox(label="Type your question here")
43
+ output_text = gr.outputs.Textbox(label="Chatbot response")
44
+
45
+ title_html = "<h2 style='text-align:center;font-weight:bold;color:black;'>Amazon Vendor Help Centre</h2>"
46
+ desc_html = "<p style='text-align:center;'>Ask our AI chatbot any question related to Amazon vendor and get instant response!</p>"
47
+
48
+ chatbot_interface = gr.Interface(
49
+ fn=chatbot,
50
+ inputs=[text_input, api_key_input],
51
+ outputs=output_text,
52
+ title=title_html,
53
+ description=desc_html,
54
+ allow_screenshot=False
55
+ )
56
+
57
+ chatbot_interface.launch(debug=True)