Rijgersberg commited on
Commit
8ec9b57
·
verified ·
1 Parent(s): 5e429c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -1,5 +1,7 @@
 
1
  import os
2
 
 
3
  from openai import OpenAI
4
  import gradio as gr
5
 
@@ -15,14 +17,44 @@ MODELS = [
15
  ]
16
 
17
 
 
 
 
 
 
 
 
18
  def generate(message, history, model, system_prompt,
19
- temperature=1.0, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0):
20
-
21
  history_openai_format = [{"role": "system", "content": system_prompt}]
22
  for user, assistant in history:
23
- history_openai_format.append({"role": "user", "content": user})
24
- history_openai_format.append({"role": "assistant", "content": assistant})
25
- history_openai_format.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  response = client.chat.completions.create(model=model,
28
  messages=history_openai_format,
@@ -40,6 +72,7 @@ def generate(message, history, model, system_prompt,
40
 
41
 
42
  chat_interface = gr.ChatInterface(
 
43
  title='💬 Private ChatGPT',
44
  description='Chat with OpenAI models using their official API. OpenAI <a href="https://platform.openai.com/docs/concepts">promises</a> not to train on input or output of API calls.',
45
  fn=generate,
@@ -76,14 +109,15 @@ chat_interface = gr.ChatInterface(
76
  step=0.05,
77
  value=0.),
78
  ],
79
- textbox=gr.Textbox(container=False,
80
- show_label=False,
81
- label="Message",
82
- placeholder="Type een bericht...",
83
- scale=7),
 
84
  additional_inputs_accordion=gr.Accordion(label="Instellingen", open=False),
85
  show_progress="full",
86
- submit_btn="Genereer",
87
  stop_btn="Stop",
88
  retry_btn="🔄 Opnieuw",
89
  undo_btn="↩️ Ongedaan maken",
 
1
+ import base64
2
  import os
3
 
4
+ from gradio_client.utils import get_mimetype
5
  from openai import OpenAI
6
  import gradio as gr
7
 
 
17
  ]
18
 
19
 
20
+ def process_image(data):
21
+ with open(data['path'], "rb") as image_file:
22
+ b64image = base64.b64encode(image_file.read()).decode('utf-8')
23
+
24
+ return "data:" + data['mime_type'] + ";base64," + b64image
25
+
26
+
27
  def generate(message, history, model, system_prompt,
28
+ temperature=1.0, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0):
29
+ # history
30
  history_openai_format = [{"role": "system", "content": system_prompt}]
31
  for user, assistant in history:
32
+ if isinstance(user, tuple): # there were files
33
+ content = [
34
+ {"type": "image_url",
35
+ # for some reason you don't get the same image format in history as in message
36
+ "image_url": {"url": process_image({'path': filepath,
37
+ 'mime_type': get_mimetype(filepath)})}}
38
+ for filepath in user]
39
+
40
+ history_openai_format.append(
41
+ {"role": "user", "content": content})
42
+ else: # there was just text
43
+ history_openai_format.append({"role": "user", "content": user})
44
+
45
+ if assistant is not None:
46
+ history_openai_format.append({"role": "assistant", "content": assistant})
47
+
48
+ # new message
49
+ content = [{"type": "text",
50
+ "text": message['text']}]
51
+
52
+ for file in message['files']:
53
+ content.append({"type": "image_url",
54
+ "image_url": {"url": process_image(file)}})
55
+
56
+ history_openai_format.append(
57
+ {"role": "user", "content": content})
58
 
59
  response = client.chat.completions.create(model=model,
60
  messages=history_openai_format,
 
72
 
73
 
74
  chat_interface = gr.ChatInterface(
75
+ multimodal=True,
76
  title='💬 Private ChatGPT',
77
  description='Chat with OpenAI models using their official API. OpenAI <a href="https://platform.openai.com/docs/concepts">promises</a> not to train on input or output of API calls.',
78
  fn=generate,
 
109
  step=0.05,
110
  value=0.),
111
  ],
112
+ textbox=gr.MultimodalTextbox(
113
+ show_label=False,
114
+ label="Message",
115
+ placeholder="Type een bericht...",
116
+ scale=7,
117
+ ),
118
  additional_inputs_accordion=gr.Accordion(label="Instellingen", open=False),
119
  show_progress="full",
120
+ submit_btn=None,
121
  stop_btn="Stop",
122
  retry_btn="🔄 Opnieuw",
123
  undo_btn="↩️ Ongedaan maken",