Spaces:
Running
on
Zero
Running
on
Zero
Rijgersberg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,134 +1,42 @@
|
|
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 |
|
8 |
-
|
9 |
-
client = OpenAI(api_key=api_key)
|
10 |
-
|
11 |
-
MODELS = [
|
12 |
-
'gpt-4o',
|
13 |
-
'gpt-4o-mini',
|
14 |
-
'gpt-4',
|
15 |
-
'gpt-4-turbo',
|
16 |
-
'gpt-3.5-turbo',
|
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 |
-
for filepath in user:
|
35 |
-
mime_type = get_mimetype(filepath) or ''
|
36 |
-
if mime_type.startswith("image/"):
|
37 |
-
content.append(
|
38 |
-
{"type": "image_url",
|
39 |
-
# for some reason you don't get the same image format in history as in message
|
40 |
-
"image_url": {"url": process_image({'path': filepath,
|
41 |
-
'mime_type': get_mimetype(filepath)})}}
|
42 |
-
)
|
43 |
-
if content:
|
44 |
-
history_openai_format.append(
|
45 |
-
{"role": "user", "content": content})
|
46 |
-
else: # there was just text
|
47 |
-
history_openai_format.append({"role": "user", "content": user})
|
48 |
-
|
49 |
-
if assistant is not None:
|
50 |
-
history_openai_format.append({"role": "assistant", "content": assistant})
|
51 |
-
|
52 |
-
# new message
|
53 |
-
content = [{"type": "text",
|
54 |
-
"text": message['text']}]
|
55 |
-
|
56 |
-
for file in message['files']:
|
57 |
-
mime_type = get_mimetype(file['path']) or ''
|
58 |
-
if not mime_type.startswith('image/'):
|
59 |
-
raise gr.Error("Momenteel zijn alleen afbeeldingen ondersteund als bijlagen 💥!", duration=20)
|
60 |
-
content.append({"type": "image_url",
|
61 |
-
"image_url": {"url": process_image(file)}})
|
62 |
-
|
63 |
-
history_openai_format.append(
|
64 |
-
{"role": "user", "content": content})
|
65 |
-
|
66 |
-
response = client.chat.completions.create(model=model,
|
67 |
-
messages=history_openai_format,
|
68 |
-
temperature=temperature,
|
69 |
-
top_p=top_p,
|
70 |
-
frequency_penalty=frequency_penalty,
|
71 |
-
presence_penalty=presence_penalty,
|
72 |
-
stream=True)
|
73 |
-
|
74 |
-
partial_message = ""
|
75 |
-
for chunk in response:
|
76 |
-
if chunk.choices and chunk.choices[0].delta.content is not None:
|
77 |
-
partial_message += chunk.choices[0].delta.content
|
78 |
-
yield partial_message
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
fn=generate,
|
86 |
-
analytics_enabled=False,
|
87 |
-
chatbot=gr.Chatbot(
|
88 |
-
show_label=False,
|
89 |
-
show_copy_button=True,
|
90 |
-
scale=1),
|
91 |
-
additional_inputs=[
|
92 |
-
gr.Dropdown(label="Model",
|
93 |
-
choices=MODELS,
|
94 |
-
value=MODELS[0],
|
95 |
-
allow_custom_value=False),
|
96 |
-
gr.Textbox(label="System prompt",
|
97 |
-
value="Je bent een slimme, behulpzame assistent van Edwin Rijgersberg"),
|
98 |
-
gr.Slider(label="Temperature",
|
99 |
-
minimum=0.,
|
100 |
-
maximum=2.0,
|
101 |
-
step=0.05,
|
102 |
-
value=1.0),
|
103 |
-
gr.Slider(label="Top P",
|
104 |
-
minimum=0.,
|
105 |
-
maximum=1.0,
|
106 |
-
step=0.05,
|
107 |
-
value=1.0),
|
108 |
-
gr.Slider(label="Frequency penalty",
|
109 |
-
minimum=0.,
|
110 |
-
maximum=1.0,
|
111 |
-
step=0.05,
|
112 |
-
value=0.),
|
113 |
-
gr.Slider(label="Presence penalty",
|
114 |
-
minimum=0.,
|
115 |
-
maximum=1.0,
|
116 |
-
step=0.05,
|
117 |
-
value=0.),
|
118 |
-
],
|
119 |
-
textbox=gr.MultimodalTextbox(
|
120 |
-
file_types=['image'],
|
121 |
-
show_label=False,
|
122 |
-
label="Message",
|
123 |
-
placeholder="Type een bericht...",
|
124 |
-
scale=7,
|
125 |
-
),
|
126 |
-
additional_inputs_accordion=gr.Accordion(label="Instellingen", open=False),
|
127 |
-
show_progress="full",
|
128 |
-
submit_btn=None,
|
129 |
-
stop_btn="Stop",
|
130 |
-
retry_btn="🔄 Opnieuw",
|
131 |
-
undo_btn="↩️ Ongedaan maken",
|
132 |
-
clear_btn="🗑️ Wissen",
|
133 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
chat_interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import torch
|
5 |
|
6 |
+
model_name = "Qwen/Qwen2.5-7B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
torch_dtype=torch.bfloat16,
|
11 |
+
device_map="auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
)
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
+
|
15 |
+
@spaces.GPU
|
16 |
+
def generate(prompt, history):
|
17 |
+
messages = [
|
18 |
+
{"role": "system", "content": "Je bent een vriendelijke, behulpzame assistent."},
|
19 |
+
{"role": "user", "content": prompt}
|
20 |
+
]
|
21 |
+
text = tokenizer.apply_chat_template(
|
22 |
+
messages,
|
23 |
+
tokenize=False,
|
24 |
+
add_generation_prompt=True
|
25 |
+
)
|
26 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
27 |
+
|
28 |
+
generated_ids = model.generate(
|
29 |
+
**model_inputs,
|
30 |
+
max_new_tokens=512
|
31 |
+
)
|
32 |
+
generated_ids = [
|
33 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
34 |
+
]
|
35 |
+
|
36 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
37 |
+
return response
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
chat_interface = gr.ChatInterface()
|
42 |
chat_interface.launch(share=True)
|