dylanmeca commited on
Commit
2f9df39
·
1 Parent(s): 65456a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -28
app.py CHANGED
@@ -1,38 +1,39 @@
1
  import openai
2
  import gradio as gr
3
 
4
- model_id = "gpt-3.5-turbo"
5
- conversation = []
6
 
7
- # Getting responses using the OpenAI API
8
- def answer_chatgpt(api_key, message, history):
9
- history = history or []
10
- prompt = f"{message}"
11
- conversation.append({"role": "user", "content": f"{prompt}"})
12
- # OPENAI API KEY
13
- openai.api_key = api_key
14
- response = openai.ChatCompletion.create(
15
- model=model_id,
16
- messages=conversation
17
- )
18
- # Displaying the answer on the screen
19
- answer = response["choices"][0]["message"]["content"]
20
- history.append((message, answer))
21
- conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
22
- return history, history
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- def Clean():
26
- global history
27
- global conversation
28
- history = []
29
- conversation = []
30
-
31
  # User input
32
  block = gr.Blocks()
 
33
 
34
  with block:
35
- gr.Markdown("""<h1><center>🤖 ChatGPT-Assistant 🐍</center></h1>
36
  <p><center>ChatGPT-Assistant is a chatbot that uses the gpt-3.5-turbo model</center></p>
37
  """)
38
  api_key = gr.Textbox(type="password", label="Enter your OpenAI API key here")
@@ -40,9 +41,9 @@ with block:
40
  message = gr.Textbox(label="Message")
41
  state = gr.State()
42
  submit = gr.Button("Send")
43
- submit.click(answer_chatgpt, inputs=[api_key, message, state], outputs=[chatbot, state])
44
  clean = gr.Button("Clean")
45
- clean.click(Clean)
46
  gr.Examples(
47
  examples=["Write a poem about artificial intelligence",
48
  "What could the future be like?",
@@ -55,4 +56,4 @@ with block:
55
  inputs=message
56
  )
57
 
58
- block.launch(debug=True)
 
1
  import openai
2
  import gradio as gr
3
 
4
+ class chatgpt:
 
5
 
6
+ def __init__(self):
7
+ self.model_id = "gpt-3.5-turbo"
8
+ self.conversation = []
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # Getting responses using the OpenAI API
11
+ def answer_chatgpt(self, api_key, message, history):
12
+ self.history = history or []
13
+ prompt = f"{message}"
14
+ self.conversation.append({"role": "user", "content": f"{prompt}"})
15
+ # OPENAI API KEY
16
+ openai.api_key = api_key
17
+ response = openai.ChatCompletion.create(
18
+ model=self.model_id,
19
+ messages=self.conversation
20
+ )
21
+ # Displaying the answer on the screen
22
+ answer = response["choices"][0]["message"]["content"]
23
+ self.history.append((message, answer))
24
+ self.conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
25
+ return self.history, self.history
26
+
27
+ def Clean(self):
28
+ self.history.clear()
29
+ self.conversation.clear()
30
 
 
 
 
 
 
 
31
  # User input
32
  block = gr.Blocks()
33
+ chatgpt = chatgpt()
34
 
35
  with block:
36
+ gr.Markdown("""<h1><center>🤖 ChatGPT-Assistant ðŸ</center></h1>
37
  <p><center>ChatGPT-Assistant is a chatbot that uses the gpt-3.5-turbo model</center></p>
38
  """)
39
  api_key = gr.Textbox(type="password", label="Enter your OpenAI API key here")
 
41
  message = gr.Textbox(label="Message")
42
  state = gr.State()
43
  submit = gr.Button("Send")
44
+ submit.click(chatgpt.answer_chatgpt, inputs=[api_key, message, state], outputs=[chatbot, state])
45
  clean = gr.Button("Clean")
46
+ clean.click(chatgpt.Clean)
47
  gr.Examples(
48
  examples=["Write a poem about artificial intelligence",
49
  "What could the future be like?",
 
56
  inputs=message
57
  )
58
 
59
+ block.launch(debug=True)