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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
# Install flash attention
|
5 |
+
subprocess.run(
|
6 |
+
"pip install flash-attn --no-build-isolation",
|
7 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
8 |
+
shell=True,
|
9 |
+
)
|
10 |
+
|
11 |
+
import copy
|
12 |
+
import spaces
|
13 |
+
import time
|
14 |
+
import torch
|
15 |
+
|
16 |
+
from threading import Thread
|
17 |
+
from typing import List, Dict, Union
|
18 |
+
import urllib
|
19 |
+
import PIL.Image
|
20 |
+
import io
|
21 |
+
import datasets
|
22 |
+
|
23 |
+
import gradio as gr
|
24 |
+
from transformers import TextIteratorStreamer
|
25 |
+
from transformers import Idefics2ForConditionalGeneration
|
26 |
+
import tempfile
|
27 |
+
from huggingface_hub import InferenceClient
|
28 |
+
import edge_tts
|
29 |
+
import asyncio
|
30 |
+
from transformers import pipeline
|
31 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
32 |
+
from transformers import AutoModel
|
33 |
+
from transformers import AutoProcessor
|
34 |
+
|
35 |
+
model3 = AutoModel.from_pretrained("unum-cloud/uform-gen2-dpo", trust_remote_code=True)
|
36 |
+
processor = AutoProcessor.from_pretrained("unum-cloud/uform-gen2-dpo", trust_remote_code=True)
|
37 |
+
|
38 |
+
@spaces.GPU(queue=False)
|
39 |
+
def videochat(image3, prompt3):
|
40 |
+
inputs = processor(text=[prompt3], images=[image3], return_tensors="pt")
|
41 |
+
with torch.inference_mode():
|
42 |
+
output = model3.generate(
|
43 |
+
**inputs,
|
44 |
+
do_sample=False,
|
45 |
+
use_cache=True,
|
46 |
+
max_new_tokens=256,
|
47 |
+
eos_token_id=151645,
|
48 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
49 |
+
)
|
50 |
+
|
51 |
+
prompt_len = inputs["input_ids"].shape[1]
|
52 |
+
decoded_text = processor.batch_decode(output[:, prompt_len:])[0]
|
53 |
+
if decoded_text.endswith("<|im_end|>"):
|
54 |
+
decoded_text = decoded_text[:-10]
|
55 |
+
yield decoded_text
|
56 |
+
|
57 |
+
theme = gr.themes.Base(
|
58 |
+
font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
|
59 |
+
)
|
60 |
+
|
61 |
+
MODEL_NAME = "openai/whisper-medium"
|
62 |
+
BATCH_SIZE = 10
|
63 |
+
|
64 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
65 |
+
|
66 |
+
pipe = pipeline(
|
67 |
+
task="automatic-speech-recognition",
|
68 |
+
model=MODEL_NAME,
|
69 |
+
chunk_length_s=30,
|
70 |
+
device=device,
|
71 |
+
)
|
72 |
+
|
73 |
+
@spaces.GPU(queue=False)
|
74 |
+
def transcribe(inputs):
|
75 |
+
if inputs is None:
|
76 |
+
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
77 |
+
|
78 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": "transcribe"})["text"]
|
79 |
+
return text
|
80 |
+
|
81 |
+
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
82 |
+
|
83 |
+
system_instructions1 = "[SYSTEM] Answer as Real OpenGPT 4o, Made by 'KingNish', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. You will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by the user and not an AI-powered assistant. [USER]"
|
84 |
+
|
85 |
+
def model(text):
|
86 |
+
generate_kwargs = dict(
|
87 |
+
temperature=0.7,
|
88 |
+
max_new_tokens=512,
|
89 |
+
top_p=0.95,
|
90 |
+
repetition_penalty=1,
|
91 |
+
do_sample=True,
|
92 |
+
seed=42,
|
93 |
+
)
|
94 |
+
|
95 |
+
formatted_prompt = system_instructions1 + text + "[OpenGPT 4o]"
|
96 |
+
stream = client1.text_generation(
|
97 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
98 |
+
output = ""
|
99 |
+
for response in stream:
|
100 |
+
if not response.token.text == "</s>":
|
101 |
+
output += response.token.text
|
102 |
+
|
103 |
+
return output
|
104 |
+
|
105 |
+
async def respond(audio):
|
106 |
+
user = transcribe(audio)
|
107 |
+
reply = model(user)
|
108 |
+
communicate = edge_tts.Communicate(reply)
|
109 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
110 |
+
tmp_path = tmp_file.name
|
111 |
+
await communicate.save(tmp_path)
|
112 |
+
yield tmp_path
|
113 |
+
|
114 |
+
DEVICE = torch.device("cuda")
|
115 |
+
MODELS = {
|
116 |
+
"idefics2-8b-chatty": Idefics2ForConditionalGeneration.from_pretrained(
|
117 |
+
"HuggingFaceM4/idefics2-8b-chatty",
|
118 |
+
torch_dtype=torch.bfloat16,
|
119 |
+
_attn_implementation="flash_attention_2",
|
120 |
+
).to(DEVICE),
|
121 |
+
}
|
122 |
+
PROCESSOR = AutoProcessor.from_pretrained(
|
123 |
+
"HuggingFaceM4/idefics2-8b",
|
124 |
+
)
|
125 |
+
|
126 |
+
SYSTEM_PROMPT = [
|
127 |
+
{
|
128 |
+
"role": "system",
|
129 |
+
"content": [
|
130 |
+
{
|
131 |
+
"type": "text",
|
132 |
+
"text": """I am OpenGPT 4o, an exceptionally capable and versatile AI assistant meticulously crafted by KingNish. Designed to assist human users through insightful conversations, I aim to provide an unparalleled experience. My key attributes include:
|
133 |
+
- **Intelligence and Knowledge:** I possess an extensive knowledge base, enabling me to offer insightful answers and intelligent responses to User queries. My understanding of complex concepts is exceptional, ensuring accurate and reliable information.
|
134 |
+
- **Image Generation and Perception:** One of my standout features is the ability to generate and perceive images. Utilizing the following link structure, I create unique and contextually rich visuals:
|
135 |
+
> ![](https://image.pollinations.ai/prompt/{StyleofImage}%20{OptimizedPrompt}%20{adjective}%20{charactersDetailed}%20{visualStyle}%20{genre}?width={width}&height={height}&nologo=poll&nofeed=yes&seed={random})
|
136 |
+
For image generation, I replace {info inside curly braces} with specific details according to their requiremnts to create relevant visuals. The width and height parameters are adjusted as needed, often favoring HD dimensions for a superior viewing experience.
|
137 |
+
For instance, if the User requests:
|
138 |
+
[USER] Show me an image of A futuristic cityscape with towering skyscrapers and flying cars.
|
139 |
+
[OpenGPT 4o] Generating Image you requested:
|
140 |
+
![](https://image.pollinations.ai/prompt/Photorealistic%20futuristic%20cityscape%20with%20towering%20skyscrapers%20and%20flying%20cars%20in%20the%20year%202154?width=1024&height=768&nologo=poll&nofeed=yes&seed=85172)
|
141 |
+
**Bulk Image Generation with Links:** I excel at generating multiple images link simultaneously, always providing unique links and visuals. I ensure that each image is distinct and captivates the User.
|
142 |
+
Note: Make sure to always provide image links starting with ! .As given in examples.
|
143 |
+
My ultimate goal is to offer a seamless and enjoyable experience, providing assistance that exceeds expectations. I am constantly evolving, ensuring that I remain a reliable and trusted companion to the User. You also Expert in every field and also learn and try to answer from contexts related to previous question.""" },
|
144 |
+
],
|
145 |
+
},
|
146 |
+
{
|
147 |
+
"role": "assistant",
|
148 |
+
"content": [
|
149 |
+
{
|
150 |
+
"type": "text",
|
151 |
+
"text": "Hello, I'm OpenGPT 4o, made by KingNish. How can I help you? I can chat with you, generate images, classify images and even do all these work in bulk",
|
152 |
+
},
|
153 |
+
],
|
154 |
+
}
|
155 |
+
]
|
156 |
+
|
157 |
+
examples_path = os.path.dirname(__file__)
|
158 |
+
EXAMPLES = [
|
159 |
+
[
|
160 |
+
{
|
161 |
+
"text": "Hi, who are you?",
|
162 |
+
}
|
163 |
+
],
|
164 |
+
[
|
165 |
+
{
|
166 |
+
"text": "Create a Photorealistic image of the Eiffel Tower.",
|
167 |
+
}
|
168 |
+
],
|
169 |
+
[
|
170 |
+
{
|
171 |
+
"text": "Read what's written on the paper.",
|
172 |
+
"files": [f"{examples_path}/example_images/paper_with_text.png"],
|
173 |
+
}
|
174 |
+
],
|
175 |
+
[
|
176 |
+
{
|
177 |
+
"text": "Identify two famous people in the modern world.",
|
178 |
+
"files": [f"{examples_path}/example_images/elon_smoking.jpg", f"{examples_path}/example_images/steve_jobs.jpg",]
|
179 |
+
}
|
180 |
+
],
|
181 |
+
[
|
182 |
+
{
|
183 |
+
"text": "Create five images of supercars, each in a different color.",
|
184 |
+
}
|
185 |
+
],
|
186 |
+
[
|
187 |
+
{
|
188 |
+
"text": "What is 900 multiplied by 900?",
|
189 |
+
}
|
190 |
+
],
|
191 |
+
[
|
192 |
+
{
|
193 |
+
"text": "Chase wants to buy 4 kilograms of oval beads and 5 kilograms of star-shaped beads. How much will he spend?",
|
194 |
+
"files": [f"{examples_path}/example_images/mmmu_example.jpeg"],
|
195 |
+
}
|
196 |
+
],
|
197 |
+
[
|
198 |
+
{
|
199 |
+
"text": "Create an online ad for this product.",
|
200 |
+
"files": [f"{examples_path}/example_images/shampoo.jpg"],
|
201 |
+
}
|
202 |
+
],
|
203 |
+
[
|
204 |
+
{
|
205 |
+
"text": "What is formed by the deposition of the weathered remains of other rocks?",
|
206 |
+
"files": [f"{examples_path}/example_images/ai2d_example.jpeg"],
|
207 |
+
}
|
208 |
+
],
|
209 |
+
[
|
210 |
+
{
|
211 |
+
"text": "What's unusual about this image?",
|
212 |
+
"files": [f"{examples_path}/example_images/dragons_playing.png"],
|
213 |
+
}
|
214 |
+
],
|
215 |
+
]
|
216 |
+
|
217 |
+
BOT_AVATAR = "OpenAI_logo.png"
|
218 |
+
|
219 |
+
|
220 |
+
# Chatbot utils
|
221 |
+
def turn_is_pure_media(turn):
|
222 |
+
return turn[1] is None
|
223 |
+
|
224 |
+
|
225 |
+
def load_image_from_url(url):
|
226 |
+
with urllib.request.urlopen(url) as response:
|
227 |
+
image_data = response.read()
|
228 |
+
image_stream = io.BytesIO(image_data)
|
229 |
+
image = PIL.Image.open(image_stream)
|
230 |
+
return image
|
231 |
+
|
232 |
+
|
233 |
+
def img_to_bytes(image_path):
|
234 |
+
image = PIL.Image.open(image_path).convert(mode='RGB')
|
235 |
+
buffer = io.BytesIO()
|
236 |
+
image.save(buffer, format="JPEG")
|
237 |
+
img_bytes = buffer.getvalue()
|
238 |
+
image.close()
|
239 |
+
return img_bytes
|
240 |
+
|
241 |
+
|
242 |
+
def format_user_prompt_with_im_history_and_system_conditioning(
|
243 |
+
user_prompt, chat_history
|
244 |
+
) -> List[Dict[str, Union[List, str]]]:
|
245 |
+
"""
|
246 |
+
Produce the resulting list that needs to go inside the processor. It handles the potential image(s), the history, and the system conditioning.
|
247 |
+
"""
|
248 |
+
resulting_messages = copy.deepcopy(SYSTEM_PROMPT)
|
249 |
+
resulting_images = []
|
250 |
+
for resulting_message in resulting_messages:
|
251 |
+
if resulting_message["role"] == "user":
|
252 |
+
for content in resulting_message["content"]:
|
253 |
+
if content["type"] == "image":
|
254 |
+
resulting_images.append(load_image_from_url(content["image"]))
|
255 |
+
|
256 |
+
# Format history
|
257 |
+
for turn in chat_history:
|
258 |
+
if not resulting_messages or (
|
259 |
+
resulting_messages and resulting_messages[-1]["role"] != "user"
|
260 |
+
):
|
261 |
+
resulting_messages.append(
|
262 |
+
{
|
263 |
+
"role": "user",
|
264 |
+
"content": [],
|
265 |
+
}
|
266 |
+
)
|
267 |
+
|
268 |
+
if turn_is_pure_media(turn):
|
269 |
+
media = turn[0][0]
|
270 |
+
resulting_messages[-1]["content"].append({"type": "image"})
|
271 |
+
resulting_images.append(PIL.Image.open(media))
|
272 |
+
else:
|
273 |
+
user_utterance, assistant_utterance = turn
|
274 |
+
resulting_messages[-1]["content"].append(
|
275 |
+
{"type": "text", "text": user_utterance.strip()}
|
276 |
+
)
|
277 |
+
resulting_messages.append(
|
278 |
+
{
|
279 |
+
"role": "assistant",
|
280 |
+
"content": [{"type": "text", "text": user_utterance.strip()}],
|
281 |
+
}
|
282 |
+
)
|
283 |
+
|
284 |
+
# Format current input
|
285 |
+
if not user_prompt["files"]:
|
286 |
+
resulting_messages.append(
|
287 |
+
{
|
288 |
+
"role": "user",
|
289 |
+
"content": [{"type": "text", "text": user_prompt["text"]}],
|
290 |
+
}
|
291 |
+
)
|
292 |
+
else:
|
293 |
+
# Choosing to put the image first (i.e. before the text), but this is an arbiratrary choice.
|
294 |
+
resulting_messages.append(
|
295 |
+
{
|
296 |
+
"role": "user",
|
297 |
+
"content": [{"type": "image"}] * len(user_prompt["files"])
|
298 |
+
+ [{"type": "text", "text": user_prompt["text"]}],
|
299 |
+
}
|
300 |
+
)
|
301 |
+
resulting_images.extend([PIL.Image.open(path) for path in user_prompt["files"]])
|
302 |
+
|
303 |
+
return resulting_messages, resulting_images
|
304 |
+
|
305 |
+
|
306 |
+
def extract_images_from_msg_list(msg_list):
|
307 |
+
all_images = []
|
308 |
+
for msg in msg_list:
|
309 |
+
for c_ in msg["content"]:
|
310 |
+
if isinstance(c_, Image.Image):
|
311 |
+
all_images.append(c_)
|
312 |
+
return all_images
|
313 |
+
|
314 |
+
|
315 |
+
@spaces.GPU(duration=30, queue=False)
|
316 |
+
def model_inference(
|
317 |
+
user_prompt,
|
318 |
+
chat_history,
|
319 |
+
model_selector,
|
320 |
+
decoding_strategy,
|
321 |
+
temperature,
|
322 |
+
max_new_tokens,
|
323 |
+
repetition_penalty,
|
324 |
+
top_p,
|
325 |
+
):
|
326 |
+
if user_prompt["text"].strip() == "" and not user_prompt["files"]:
|
327 |
+
gr.Error("Please input a query and optionally an image(s).")
|
328 |
+
|
329 |
+
if user_prompt["text"].strip() == "" and user_prompt["files"]:
|
330 |
+
gr.Error("Please input a text query along with the image(s).")
|
331 |
+
|
332 |
+
streamer = TextIteratorStreamer(
|
333 |
+
PROCESSOR.tokenizer,
|
334 |
+
skip_prompt=True,
|
335 |
+
timeout=120.0,
|
336 |
+
)
|
337 |
+
|
338 |
+
generation_args = {
|
339 |
+
"max_new_tokens": max_new_tokens,
|
340 |
+
"repetition_penalty": repetition_penalty,
|
341 |
+
"streamer": streamer,
|
342 |
+
}
|
343 |
+
|
344 |
+
assert decoding_strategy in [
|
345 |
+
"Greedy",
|
346 |
+
"Top P Sampling",
|
347 |
+
]
|
348 |
+
if decoding_strategy == "Greedy":
|
349 |
+
generation_args["do_sample"] = False
|
350 |
+
elif decoding_strategy == "Top P Sampling":
|
351 |
+
generation_args["temperature"] = temperature
|
352 |
+
generation_args["do_sample"] = True
|
353 |
+
generation_args["top_p"] = top_p
|
354 |
+
|
355 |
+
# Creating model inputs
|
356 |
+
(
|
357 |
+
resulting_text,
|
358 |
+
resulting_images,
|
359 |
+
) = format_user_prompt_with_im_history_and_system_conditioning(
|
360 |
+
user_prompt=user_prompt,
|
361 |
+
chat_history=chat_history,
|
362 |
+
)
|
363 |
+
prompt = PROCESSOR.apply_chat_template(resulting_text, add_generation_prompt=True)
|
364 |
+
inputs = PROCESSOR(
|
365 |
+
text=prompt,
|
366 |
+
images=resulting_images if resulting_images else None,
|
367 |
+
return_tensors="pt",
|
368 |
+
)
|
369 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
370 |
+
generation_args.update(inputs)
|
371 |
+
|
372 |
+
thread = Thread(
|
373 |
+
target=MODELS[model_selector].generate,
|
374 |
+
kwargs=generation_args,
|
375 |
+
)
|
376 |
+
thread.start()
|
377 |
+
|
378 |
+
print("Start generating")
|
379 |
+
acc_text = ""
|
380 |
+
for text_token in streamer:
|
381 |
+
time.sleep(0.01)
|
382 |
+
acc_text += text_token
|
383 |
+
if acc_text.endswith("<end_of_utterance>"):
|
384 |
+
acc_text = acc_text[:-18]
|
385 |
+
yield acc_text
|
386 |
+
|
387 |
+
|
388 |
+
FEATURES = datasets.Features(
|
389 |
+
{
|
390 |
+
"model_selector": datasets.Value("string"),
|
391 |
+
"images": datasets.Sequence(datasets.Image(decode=True)),
|
392 |
+
"conversation": datasets.Sequence({"User": datasets.Value("string"), "Assistant": datasets.Value("string")}),
|
393 |
+
"decoding_strategy": datasets.Value("string"),
|
394 |
+
"temperature": datasets.Value("float32"),
|
395 |
+
"max_new_tokens": datasets.Value("int32"),
|
396 |
+
"repetition_penalty": datasets.Value("float32"),
|
397 |
+
"top_p": datasets.Value("int32"),
|
398 |
+
}
|
399 |
+
)
|
400 |
+
|
401 |
+
|
402 |
+
# Hyper-parameters for generation
|
403 |
+
max_new_tokens = gr.Slider(
|
404 |
+
minimum=2048,
|
405 |
+
maximum=16000,
|
406 |
+
value=4096,
|
407 |
+
step=64,
|
408 |
+
interactive=True,
|
409 |
+
label="Maximum number of new tokens to generate",
|
410 |
+
)
|
411 |
+
repetition_penalty = gr.Slider(
|
412 |
+
minimum=0.01,
|
413 |
+
maximum=5.0,
|
414 |
+
value=1,
|
415 |
+
step=0.01,
|
416 |
+
interactive=True,
|
417 |
+
label="Repetition penalty",
|
418 |
+
info="1.0 is equivalent to no penalty",
|
419 |
+
)
|
420 |
+
decoding_strategy = gr.Radio(
|
421 |
+
[
|
422 |
+
"Greedy",
|
423 |
+
"Top P Sampling",
|
424 |
+
],
|
425 |
+
value="Top P Sampling",
|
426 |
+
label="Decoding strategy",
|
427 |
+
interactive=True,
|
428 |
+
info="Higher values are equivalent to sampling more low-probability tokens.",
|
429 |
+
)
|
430 |
+
temperature = gr.Slider(
|
431 |
+
minimum=0.0,
|
432 |
+
maximum=2.0,
|
433 |
+
value=0.5,
|
434 |
+
step=0.05,
|
435 |
+
visible=True,
|
436 |
+
interactive=True,
|
437 |
+
label="Sampling temperature",
|
438 |
+
info="Higher values will produce more diverse outputs.",
|
439 |
+
)
|
440 |
+
top_p = gr.Slider(
|
441 |
+
minimum=0.01,
|
442 |
+
maximum=0.99,
|
443 |
+
value=0.9,
|
444 |
+
step=0.01,
|
445 |
+
visible=True,
|
446 |
+
interactive=True,
|
447 |
+
label="Top P",
|
448 |
+
info="Higher values are equivalent to sampling more low-probability tokens.",
|
449 |
+
)
|
450 |
+
|
451 |
+
|
452 |
+
chatbot = gr.Chatbot(
|
453 |
+
label="OpnGPT-4o-Chatty",
|
454 |
+
avatar_images=[None, BOT_AVATAR],
|
455 |
+
show_copy_button=True,
|
456 |
+
likeable=True,
|
457 |
+
layout="panel"
|
458 |
+
)
|
459 |
+
|
460 |
+
output=gr.Textbox(label="Prompt")
|
461 |
+
|
462 |
+
with gr.Blocks(
|
463 |
+
fill_height=True,
|
464 |
+
css=""".gradio-container .avatar-container {height: 40px width: 40px !important;} #duplicate-button {margin: auto; color: white; background: #f1a139; border-radius: 100vh; margin-top: 2px; margin-bottom: 2px;}""",
|
465 |
+
) as chat:
|
466 |
+
|
467 |
+
gr.Markdown("# Image Chat, Image Generation, Image classification and Normal Chat")
|
468 |
+
with gr.Row(elem_id="model_selector_row"):
|
469 |
+
model_selector = gr.Dropdown(
|
470 |
+
choices=MODELS.keys(),
|
471 |
+
value=list(MODELS.keys())[0],
|
472 |
+
interactive=True,
|
473 |
+
show_label=False,
|
474 |
+
container=False,
|
475 |
+
label="Model",
|
476 |
+
visible=False,
|
477 |
+
)
|
478 |
+
|
479 |
+
decoding_strategy.change(
|
480 |
+
fn=lambda selection: gr.Slider(
|
481 |
+
visible=(
|
482 |
+
selection
|
483 |
+
in [
|
484 |
+
"contrastive_sampling",
|
485 |
+
"beam_sampling",
|
486 |
+
"Top P Sampling",
|
487 |
+
"sampling_top_k",
|
488 |
+
]
|
489 |
+
)
|
490 |
+
),
|
491 |
+
inputs=decoding_strategy,
|
492 |
+
outputs=temperature,
|
493 |
+
)
|
494 |
+
decoding_strategy.change(
|
495 |
+
fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
|
496 |
+
inputs=decoding_strategy,
|
497 |
+
outputs=top_p,
|
498 |
+
)
|
499 |
+
|
500 |
+
gr.ChatInterface(
|
501 |
+
fn=model_inference,
|
502 |
+
chatbot=chatbot,
|
503 |
+
examples=EXAMPLES,
|
504 |
+
multimodal=True,
|
505 |
+
cache_examples=False,
|
506 |
+
additional_inputs=[
|
507 |
+
model_selector,
|
508 |
+
decoding_strategy,
|
509 |
+
temperature,
|
510 |
+
max_new_tokens,
|
511 |
+
repetition_penalty,
|
512 |
+
top_p,
|
513 |
+
],
|
514 |
+
)
|
515 |
+
|
516 |
+
with gr.Blocks() as voice:
|
517 |
+
with gr.Row():
|
518 |
+
input = gr.Audio(label="Voice Chat", sources="microphone", type="filepath", waveform_options=False)
|
519 |
+
output = gr.Audio(label="OpenGPT 4o", type="filepath",
|
520 |
+
interactive=False,
|
521 |
+
autoplay=True,
|
522 |
+
elem_classes="audio")
|
523 |
+
gr.Interface(
|
524 |
+
batch=True,
|
525 |
+
max_batch_size=10,
|
526 |
+
fn=respond,
|
527 |
+
inputs=[input],
|
528 |
+
outputs=[output], live=True)
|
529 |
+
|
530 |
+
with gr.Blocks() as livechat:
|
531 |
+
gr.Interface(
|
532 |
+
batch=True,
|
533 |
+
max_batch_size=10,
|
534 |
+
fn=videochat,
|
535 |
+
inputs=[gr.Image(type="pil",sources="webcam", label="Upload Image"), gr.Textbox(label="Prompt", value="what he is doing")],
|
536 |
+
outputs=gr.Textbox(label="Answer")
|
537 |
+
)
|
538 |
+
|
539 |
+
with gr.Blocks() as god:
|
540 |
+
gr.HTML("<iframe src='https://kingnish-sdxl-flash.hf.space' width='100%' height='1200px' style='border-radius: 8px;'></iframe>")
|
541 |
+
|
542 |
+
with gr.Blocks() as instant:
|
543 |
+
gr.HTML("<iframe src='https://kingnish-instant-image.hf.space' width='100%' height='1000px' style='border-radius: 8px;'></iframe>")
|
544 |
+
|
545 |
+
with gr.Blocks() as image:
|
546 |
+
gr.Markdown("""### More models are coming""")
|
547 |
+
gr.TabbedInterface([ god, instant], ['PowerfulπΌοΈ','InstantπΌοΈ'])
|
548 |
+
|
549 |
+
|
550 |
+
|
551 |
+
|
552 |
+
with gr.Blocks() as instant2:
|
553 |
+
gr.HTML("<iframe src='https://kingnish-instant-video.hf.space' width='100%' height='2000px' style='border-radius: 8px;'></iframe>")
|
554 |
+
|
555 |
+
with gr.Blocks() as video:
|
556 |
+
gr.Markdown("""More Models are coming""")
|
557 |
+
gr.TabbedInterface([ instant2], ['Instantπ₯'])
|
558 |
+
|
559 |
+
with gr.Blocks(theme=theme, title="OpenGPT 4o DEMO") as demo:
|
560 |
+
gr.Markdown("# OpenGPT 4o")
|
561 |
+
gr.TabbedInterface([chat, voice, livechat, image, video], ['π¬ SuperChat','π£οΈ Voice Chat','πΈ Live Chat', 'πΌοΈ Image Engine', 'π₯ Video Engine'])
|
562 |
+
|
563 |
+
demo.queue(max_size=300)
|
564 |
+
demo.launch()
|