File size: 2,666 Bytes
676f49f
 
 
16f624c
467717c
2f9df39
 
 
5ce9388
2f9df39
 
2dbae7a
 
 
 
 
 
 
 
 
 
 
 
 
 
2f9df39
e1d4f6b
 
 
2f9df39
 
 
de949fb
16f624c
676f49f
2c34c42
2f9df39
2c34c42
 
16f624c
2c34c42
 
a87eb00
 
e1d4f6b
 
2c34c42
a87eb00
2c34c42
 
2f9df39
5ce9388
de949fb
60bc894
b50a40d
 
 
 
 
b9abf53
c853040
b9abf53
b50a40d
60bc894
2c34c42
16f624c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import openai
import gradio as gr

class chatgpt: 

      def __init__(self):
          self.model_id = "gpt-3.5-turbo"
          self.conversation = []

      # Getting responses using the OpenAI API
      def answer_chatgpt(self, api_key, message, history):
          self.history = history or []
          prompt = f"{message}"
          self.conversation.append({"role": "user", "content": f"{prompt}"})
          # OPENAI API KEY
          openai.api_key = api_key
          response = openai.ChatCompletion.create(
              model=self.model_id,
              messages=self.conversation
          )
          # Displaying the answer on the screen
          answer = response["choices"][0]["message"]["content"]
          self.history.append((message, answer))
          self.conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
          return self.history, self.history

      def system_message(self, systemmessage):
          self.conversation.append({"role": "system", "content": f"{systemmessage}"})

      def Clean(self):
          self.history.clear()
          self.conversation.clear()
          return self.history, self.history
      
# User input
block = gr.Blocks()
chatgpt = chatgpt()

with block:
    gr.Markdown("""<h1><center>πŸ€– ChatGPT-Assistant 🐍</center></h1>
                   <p><center>ChatGPT-Assistant is a chatbot that uses the gpt-3.5-turbo model</center></p>
    """)
    api_key = gr.Textbox(type="password", label="Enter your OpenAI API key here", placeholder="sk-...0VYO")
    system = gr.Textbox(label="System message", placeholder="You are a helpful assistant.")
    sub = gr.Button("Send")
    sub.click(chatgpt.system_message, inputs=[system])
    chatbot = gr.Chatbot()
    message = gr.Textbox(label="Message", placeholder="Hi, how are things ?")
    state = gr.State()
    submit = gr.Button("Send")
    submit.click(chatgpt.answer_chatgpt, inputs=[api_key, message, state], outputs=[chatbot, state])
    clean = gr.Button("Clean")
    clean.click(chatgpt.Clean, outputs=[chatbot, state])
    gr.Examples(
        examples=["Write a poem about artificial intelligence",
                  "What could the future be like?",
                  "If x+1=20, what is the value of x?",
                  "Write a story that gives a message",
                  "What programming language is the most used in the industry?",
                  "How can I be more productive?",
                  "Create me a training schedule to train from home",
                  "Sums up everything we've talked about"],
        inputs=message
    )

block.launch(debug=True)