File size: 3,091 Bytes
676f49f
 
 
16f624c
467717c
2f9df39
 
22e38b1
5ce9388
2f9df39
 
2dbae7a
 
 
 
 
 
 
 
 
 
 
 
 
 
2f9df39
b4c564b
0352c7d
 
 
2f9df39
 
 
9f0ebe8
de949fb
16f624c
676f49f
2c34c42
2f9df39
2c34c42
 
16f624c
2c34c42
 
63c10af
0352c7d
b4c564b
 
2c34c42
63c10af
2c34c42
879bbad
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
67
68
import openai
import gradio as gr

class chatgpt: 

      def __init__(self):
          self.model_id = "gpt-3.5-turbo"
          self.conversation = [{"role": "system", "content": "You are a friendly assistant who uses casual language and humor, in your conversations you often use emojis to reflect your mood."},]

      # 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, systemm):
          self.conversation.clear()
          self.conversation = [{"role": "system", "content": f"{systemm}"},]

      def Clean(self):
          self.history.clear()
          self.conversation.clear()
          self.conversation = [{"role": "system", "content": "You are a friendly assistant who uses casual language and humor, in your conversations you often use emojis to reflect your mood."},]
          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")
    systemm = gr.Textbox(label="System message", placeholder="You are a helpful assistant.")
    submit_system = gr.Button("Send system message")
    submit_system.click(chatgpt.system_message, inputs=[systemm])
    chatbot = gr.Chatbot()
    message = gr.Textbox(label="Message", placeholder="Hi, how are things ?")
    state = gr.State()
    submit = gr.Button("Send message")
    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)