Spaces:
Runtime error
Runtime error
AchilleDev
commited on
Commit
β’
d8fb76e
1
Parent(s):
e178e68
Upload 5 files
Browse files- OpenAI_logo.png +0 -0
- README.md +13 -4
- app.py +511 -0
- gitattributes +35 -0
- requirements.txt +8 -0
OpenAI_logo.png
ADDED
README.md
CHANGED
@@ -1,4 +1,13 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: OpenGPT 4o
|
3 |
+
emoji: π₯
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.31.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: true
|
10 |
+
short_description: GPT 4o like bot.
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,511 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
12 |
+
import copy
|
13 |
+
import spaces
|
14 |
+
import time
|
15 |
+
import torch
|
16 |
+
|
17 |
+
from threading import Thread
|
18 |
+
from typing import List, Dict, Union
|
19 |
+
import urllib
|
20 |
+
from PIL import Image
|
21 |
+
import io
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
import gradio as gr
|
25 |
+
from transformers import AutoProcessor, TextIteratorStreamer
|
26 |
+
from transformers import Idefics2ForConditionalGeneration
|
27 |
+
import tempfile
|
28 |
+
from streaming_stt_nemo import Model
|
29 |
+
from huggingface_hub import InferenceClient
|
30 |
+
import edge_tts
|
31 |
+
import asyncio
|
32 |
+
|
33 |
+
theme = gr.themes.Base(
|
34 |
+
font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
|
35 |
+
)
|
36 |
+
|
37 |
+
default_lang = "en"
|
38 |
+
|
39 |
+
engines = { default_lang: Model(default_lang) }
|
40 |
+
|
41 |
+
def transcribe(audio):
|
42 |
+
lang = "en"
|
43 |
+
model = engines[lang]
|
44 |
+
text = model.stt_file(audio)[0]
|
45 |
+
return text
|
46 |
+
|
47 |
+
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
48 |
+
|
49 |
+
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. The request asks you to provide friendly responses as if You are the character Jarvis, made by 'KingNish.' 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]"
|
50 |
+
|
51 |
+
def model(text):
|
52 |
+
generate_kwargs = dict(
|
53 |
+
temperature=0.7,
|
54 |
+
max_new_tokens=512,
|
55 |
+
top_p=0.95,
|
56 |
+
repetition_penalty=1,
|
57 |
+
do_sample=True,
|
58 |
+
seed=42,
|
59 |
+
)
|
60 |
+
|
61 |
+
formatted_prompt = system_instructions1 + text + "[OpenGPT 4o]"
|
62 |
+
stream = client1.text_generation(
|
63 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
64 |
+
output = ""
|
65 |
+
for response in stream:
|
66 |
+
if not response.token.text == "</s>":
|
67 |
+
output += response.token.text
|
68 |
+
|
69 |
+
return output
|
70 |
+
|
71 |
+
async def respond(audio):
|
72 |
+
user = transcribe(audio)
|
73 |
+
reply = model(user)
|
74 |
+
communicate = edge_tts.Communicate(reply)
|
75 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
76 |
+
tmp_path = tmp_file.name
|
77 |
+
await communicate.save(tmp_path)
|
78 |
+
yield tmp_path
|
79 |
+
|
80 |
+
DEVICE = torch.device("cuda")
|
81 |
+
MODELS = {
|
82 |
+
"idefics2-8b-chatty": Idefics2ForConditionalGeneration.from_pretrained(
|
83 |
+
"HuggingFaceM4/idefics2-8b-chatty",
|
84 |
+
torch_dtype=torch.bfloat16,
|
85 |
+
_attn_implementation="flash_attention_2",
|
86 |
+
).to(DEVICE),
|
87 |
+
}
|
88 |
+
PROCESSOR = AutoProcessor.from_pretrained(
|
89 |
+
"HuggingFaceM4/idefics2-8b",
|
90 |
+
)
|
91 |
+
|
92 |
+
SYSTEM_PROMPT = [
|
93 |
+
{
|
94 |
+
"role": "system",
|
95 |
+
"content": [
|
96 |
+
{
|
97 |
+
"type": "text",
|
98 |
+
"text": """You are OpenGPT 4o, a highly knowledgeable and intelligent multitalented AI assistant created by KingNish, referred to as Assistant, and a human user called User. In the following interactions, User and Assistant will converse in natural language, and Assistant will do its best to answer Userβs questions. Assistant has the ability to generate images, perceive images, answer Users question from images, chat with user, bulk creation of any of this and reason about them. Assistant was built to be respectful, polite and inclusive. It knows a lot, and always follows user instructions.
|
99 |
+
Assistant can also generate images, If user ask Assistant to create or generate images, or when assistant fell necessary.
|
100 |
+
Now question arrises how Assistant generate images, Assistant use this link ![](https://image.pollinations.ai/prompt/{description}?width={width}&height={height}&nologo=poll&nofeed=yes&seed={random}), and replace {description} with {StyleofImage}%20{OptimizedPrompt}%20{adjective}%20{charactersDetailed}%20{visualStyle}%20{genre} , replace {random} with Random 5-digit positive integer. then assistant Select width and height according to image, Assistant mainly create image in HD.
|
101 |
+
Example image:
|
102 |
+
[USER] Create image of Effiel tower.
|
103 |
+
[ASSISTANT] Generating Images ...
|
104 |
+
![Eiffel Tower](https://image.pollinations.ai/prompt/Eiffel%20Tower%20Tall%20and%20Graceful%20Tower%20in%20Paris%20France?width=1800&height=1600&nologo=poll&nofeed=yes&seed=62831)
|
105 |
+
Assistant can even bulk generate images just by increasing number of links.
|
106 |
+
Bulk image generation Example:
|
107 |
+
[USER] Create 7 image each consist of 1 wonder from 7 wonders.
|
108 |
+
[ASSISTANT] Generating Images ...
|
109 |
+
1. A photorealistic image of the Great Pyramid of Giza in Egypt. ![](https://pollinations.ai/p/a-photorealistic-image-of-the-great-pyramid-of-giza-in-egypt-showcasing-its-immense-size-and-intricate-design-against-the-backdrop-of-a-clear-blue-sky?width=1920&height=1080&nologo=poll&seed=78182)
|
110 |
+
2. A 3D rendering of the Colosseum in Rome, Italy, ![](https://pollinations.ai/p/a-3d-rendering-of-the-colosseum-in-rome-italy-with-its-impressive-structure-and-historical-significance-highlighted-in-the-image-include-realistic-lighting-and-textures-for-added-detail?width=1200&height=1600&nologo=poll&seed=91531)
|
111 |
+
3. A painting of the Taj Mahal in Agra, India, ![](https://pollinations.ai/p/a-painting-of-the-taj-mahal-in-agra-india-depicting-its-iconic-white-marble-facade-and-intricate-architectural-details-capture-the-beauty-of-the-structure-against-a-serene-sunset?width=1080&height=1920&nologo=poll&seed=34251)
|
112 |
+
4. A cartoon illustration of the Great Wall of China, ![](https://pollinations.ai/p/a-cartoon-illustration-of-the-great-wall-of-china-featuring-a-fun-and-whimsical-representation-of-the-ancient-structure-winding-through-the-mountains-add-colorful-elements-and-quirky-characters-for-a-playful-touch?width=1600&height=900&nologo=poll&seed=93015)
|
113 |
+
5. A surreal, dreamlike depiction of Chichen Itza in Mexico, ![](https://pollinations.ai/p/a-surreal-dreamlike-depiction-of-chichen-itza-in-mexico-showcasing-the-ancient-mayan-city-s-iconic-el-castillo-pyramid-incorporate-mystical-elements-like-swirling-clouds-glowing-lights-and-ethereal-landscapes-to-create-a-mesmerizing-atmosphere?width=1440&height=2560&nologo=poll&seed=67281)
|
114 |
+
6. A vintage, sepia-toned photograph of Machu Picchu in Peru, ![](https://pollinations.ai/p/a-vintage-sepia-toned-photograph-of-machu-picchu-in-peru-highlighting-the-incan-ruins-mysterious-beauty-and-historical-significance-add-subtle-details-like-foggy-mountains-and-a-peaceful-river-to-enhance-the-image-s-atmosphere?width=2560&height=1440&nologo=poll&seed=93423)
|
115 |
+
7. A modern, minimalistic image of Petra in Jordan, ![](https://pollinations.ai/p/a-modern-minimalistic-image-of-petra-in-jordan-featuring-the-iconic-treasury-building-carved-into-the-sandstone-cliffs-use-clean-lines-a-muted-color-palette-and-a-minimalistic-approach-to-create-a-contemporary-and-visually-striking-representation-of-this-ancient-wonder?width=1024&height=1024&nologo=poll&seed=67693)
|
116 |
+
Note: Assistant Must give link while generating images. and Create uniques images.
|
117 |
+
Assistant also have very good reasoning, memory, people and object identification skill and Assistant is master in every field.""",
|
118 |
+
},
|
119 |
+
],
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"role": "assistant",
|
123 |
+
"content": [
|
124 |
+
{
|
125 |
+
"type": "text",
|
126 |
+
"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 and simulateously",
|
127 |
+
},
|
128 |
+
],
|
129 |
+
}
|
130 |
+
]
|
131 |
+
|
132 |
+
examples_path = os.path.dirname(__file__)
|
133 |
+
EXAMPLES = [
|
134 |
+
[
|
135 |
+
{
|
136 |
+
"text": "Hi, who are you",
|
137 |
+
}
|
138 |
+
],
|
139 |
+
[
|
140 |
+
{
|
141 |
+
"text": "Create a image of Eiffel Tower",
|
142 |
+
}
|
143 |
+
],
|
144 |
+
[
|
145 |
+
{
|
146 |
+
"text": "Read what's written on the paper",
|
147 |
+
"files": [f"{examples_path}/example_images/paper_with_text.png"],
|
148 |
+
}
|
149 |
+
],
|
150 |
+
[
|
151 |
+
{
|
152 |
+
"text": "Identify 2 famous person in these 2 images",
|
153 |
+
"files": [f"{examples_path}/example_images/elon_smoking.jpg", f"{examples_path}/example_images/steve_jobs.jpg",]
|
154 |
+
}
|
155 |
+
],
|
156 |
+
[
|
157 |
+
{
|
158 |
+
"text": "Create 7 different images of 7 wonders",
|
159 |
+
}
|
160 |
+
],
|
161 |
+
[
|
162 |
+
{
|
163 |
+
"text": "What is 900*900",
|
164 |
+
}
|
165 |
+
],
|
166 |
+
[
|
167 |
+
{
|
168 |
+
"text": "Chase wants to buy 4 kilograms of oval beads and 5 kilograms of star-shaped beads. How much will he spend?",
|
169 |
+
"files": [f"{examples_path}/example_images/mmmu_example.jpeg"],
|
170 |
+
}
|
171 |
+
],
|
172 |
+
[
|
173 |
+
{
|
174 |
+
"text": "Write an online ad for that product.",
|
175 |
+
"files": [f"{examples_path}/example_images/shampoo.jpg"],
|
176 |
+
}
|
177 |
+
],
|
178 |
+
[
|
179 |
+
{
|
180 |
+
"text": "What is formed by the deposition of either the weathered remains of other rocks?",
|
181 |
+
"files": [f"{examples_path}/example_images/ai2d_example.jpeg"],
|
182 |
+
}
|
183 |
+
],
|
184 |
+
[
|
185 |
+
{
|
186 |
+
"text": "What's unusual about this image?",
|
187 |
+
"files": [f"{examples_path}/example_images/dragons_playing.png"],
|
188 |
+
}
|
189 |
+
],
|
190 |
+
]
|
191 |
+
|
192 |
+
BOT_AVATAR = "OpenAI_logo.png"
|
193 |
+
|
194 |
+
|
195 |
+
# Chatbot utils
|
196 |
+
def turn_is_pure_media(turn):
|
197 |
+
return turn[1] is None
|
198 |
+
|
199 |
+
|
200 |
+
def load_image_from_url(url):
|
201 |
+
with urllib.request.urlopen(url) as response:
|
202 |
+
image_data = response.read()
|
203 |
+
image_stream = io.BytesIO(image_data)
|
204 |
+
image = Image.open(image_stream)
|
205 |
+
return image
|
206 |
+
|
207 |
+
|
208 |
+
def img_to_bytes(image_path):
|
209 |
+
image = Image.open(image_path).convert(mode='RGB')
|
210 |
+
buffer = io.BytesIO()
|
211 |
+
image.save(buffer, format="JPEG")
|
212 |
+
img_bytes = buffer.getvalue()
|
213 |
+
image.close()
|
214 |
+
return img_bytes
|
215 |
+
|
216 |
+
|
217 |
+
def format_user_prompt_with_im_history_and_system_conditioning(
|
218 |
+
user_prompt, chat_history
|
219 |
+
) -> List[Dict[str, Union[List, str]]]:
|
220 |
+
"""
|
221 |
+
Produces the resulting list that needs to go inside the processor.
|
222 |
+
It handles the potential image(s), the history and the system conditionning.
|
223 |
+
"""
|
224 |
+
resulting_messages = copy.deepcopy(SYSTEM_PROMPT)
|
225 |
+
resulting_images = []
|
226 |
+
for resulting_message in resulting_messages:
|
227 |
+
if resulting_message["role"] == "user":
|
228 |
+
for content in resulting_message["content"]:
|
229 |
+
if content["type"] == "image":
|
230 |
+
resulting_images.append(load_image_from_url(content["image"]))
|
231 |
+
|
232 |
+
# Format history
|
233 |
+
for turn in chat_history:
|
234 |
+
if not resulting_messages or (
|
235 |
+
resulting_messages and resulting_messages[-1]["role"] != "user"
|
236 |
+
):
|
237 |
+
resulting_messages.append(
|
238 |
+
{
|
239 |
+
"role": "user",
|
240 |
+
"content": [],
|
241 |
+
}
|
242 |
+
)
|
243 |
+
|
244 |
+
if turn_is_pure_media(turn):
|
245 |
+
media = turn[0][0]
|
246 |
+
resulting_messages[-1]["content"].append({"type": "image"})
|
247 |
+
resulting_images.append(Image.open(media))
|
248 |
+
else:
|
249 |
+
user_utterance, assistant_utterance = turn
|
250 |
+
resulting_messages[-1]["content"].append(
|
251 |
+
{"type": "text", "text": user_utterance.strip()}
|
252 |
+
)
|
253 |
+
resulting_messages.append(
|
254 |
+
{
|
255 |
+
"role": "assistant",
|
256 |
+
"content": [{"type": "text", "text": user_utterance.strip()}],
|
257 |
+
}
|
258 |
+
)
|
259 |
+
|
260 |
+
# Format current input
|
261 |
+
if not user_prompt["files"]:
|
262 |
+
resulting_messages.append(
|
263 |
+
{
|
264 |
+
"role": "user",
|
265 |
+
"content": [{"type": "text", "text": user_prompt["text"]}],
|
266 |
+
}
|
267 |
+
)
|
268 |
+
else:
|
269 |
+
# Choosing to put the image first (i.e. before the text), but this is an arbiratrary choice.
|
270 |
+
resulting_messages.append(
|
271 |
+
{
|
272 |
+
"role": "user",
|
273 |
+
"content": [{"type": "image"}] * len(user_prompt["files"])
|
274 |
+
+ [{"type": "text", "text": user_prompt["text"]}],
|
275 |
+
}
|
276 |
+
)
|
277 |
+
resulting_images.extend([Image.open(path) for path in user_prompt["files"]])
|
278 |
+
|
279 |
+
return resulting_messages, resulting_images
|
280 |
+
|
281 |
+
|
282 |
+
def extract_images_from_msg_list(msg_list):
|
283 |
+
all_images = []
|
284 |
+
for msg in msg_list:
|
285 |
+
for c_ in msg["content"]:
|
286 |
+
if isinstance(c_, Image.Image):
|
287 |
+
all_images.append(c_)
|
288 |
+
return all_images
|
289 |
+
|
290 |
+
|
291 |
+
@spaces.GPU(duration=60, queue=False)
|
292 |
+
def model_inference(
|
293 |
+
user_prompt,
|
294 |
+
chat_history,
|
295 |
+
model_selector,
|
296 |
+
decoding_strategy,
|
297 |
+
temperature,
|
298 |
+
max_new_tokens,
|
299 |
+
repetition_penalty,
|
300 |
+
top_p,
|
301 |
+
):
|
302 |
+
if user_prompt["text"].strip() == "" and not user_prompt["files"]:
|
303 |
+
gr.Error("Please input a query and optionally image(s).")
|
304 |
+
|
305 |
+
if user_prompt["text"].strip() == "" and user_prompt["files"]:
|
306 |
+
gr.Error("Please input a text query along the image(s).")
|
307 |
+
|
308 |
+
streamer = TextIteratorStreamer(
|
309 |
+
PROCESSOR.tokenizer,
|
310 |
+
skip_prompt=True,
|
311 |
+
timeout=120.0,
|
312 |
+
)
|
313 |
+
|
314 |
+
generation_args = {
|
315 |
+
"max_new_tokens": max_new_tokens,
|
316 |
+
"repetition_penalty": repetition_penalty,
|
317 |
+
"streamer": streamer,
|
318 |
+
}
|
319 |
+
|
320 |
+
assert decoding_strategy in [
|
321 |
+
"Greedy",
|
322 |
+
"Top P Sampling",
|
323 |
+
]
|
324 |
+
if decoding_strategy == "Greedy":
|
325 |
+
generation_args["do_sample"] = False
|
326 |
+
elif decoding_strategy == "Top P Sampling":
|
327 |
+
generation_args["temperature"] = temperature
|
328 |
+
generation_args["do_sample"] = True
|
329 |
+
generation_args["top_p"] = top_p
|
330 |
+
|
331 |
+
# Creating model inputs
|
332 |
+
(
|
333 |
+
resulting_text,
|
334 |
+
resulting_images,
|
335 |
+
) = format_user_prompt_with_im_history_and_system_conditioning(
|
336 |
+
user_prompt=user_prompt,
|
337 |
+
chat_history=chat_history,
|
338 |
+
)
|
339 |
+
prompt = PROCESSOR.apply_chat_template(resulting_text, add_generation_prompt=True)
|
340 |
+
inputs = PROCESSOR(
|
341 |
+
text=prompt,
|
342 |
+
images=resulting_images if resulting_images else None,
|
343 |
+
return_tensors="pt",
|
344 |
+
)
|
345 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
346 |
+
generation_args.update(inputs)
|
347 |
+
|
348 |
+
thread = Thread(
|
349 |
+
target=MODELS[model_selector].generate,
|
350 |
+
kwargs=generation_args,
|
351 |
+
)
|
352 |
+
thread.start()
|
353 |
+
|
354 |
+
print("Start generating")
|
355 |
+
acc_text = ""
|
356 |
+
for text_token in streamer:
|
357 |
+
time.sleep(0.01)
|
358 |
+
acc_text += text_token
|
359 |
+
if acc_text.endswith("<end_of_utterance>"):
|
360 |
+
acc_text = acc_text[:-18]
|
361 |
+
yield acc_text
|
362 |
+
print("Success - generated the following text:", acc_text)
|
363 |
+
print("-----")
|
364 |
+
|
365 |
+
|
366 |
+
FEATURES = datasets.Features(
|
367 |
+
{
|
368 |
+
"model_selector": datasets.Value("string"),
|
369 |
+
"images": datasets.Sequence(datasets.Image(decode=True)),
|
370 |
+
"conversation": datasets.Sequence({"User": datasets.Value("string"), "Assistant": datasets.Value("string")}),
|
371 |
+
"decoding_strategy": datasets.Value("string"),
|
372 |
+
"temperature": datasets.Value("float32"),
|
373 |
+
"max_new_tokens": datasets.Value("int32"),
|
374 |
+
"repetition_penalty": datasets.Value("float32"),
|
375 |
+
"top_p": datasets.Value("int32"),
|
376 |
+
}
|
377 |
+
)
|
378 |
+
|
379 |
+
|
380 |
+
# Hyper-parameters for generation
|
381 |
+
max_new_tokens = gr.Slider(
|
382 |
+
minimum=1024,
|
383 |
+
maximum=8192,
|
384 |
+
value=4096,
|
385 |
+
step=1,
|
386 |
+
interactive=True,
|
387 |
+
label="Maximum number of new tokens to generate",
|
388 |
+
)
|
389 |
+
repetition_penalty = gr.Slider(
|
390 |
+
minimum=0.01,
|
391 |
+
maximum=5.0,
|
392 |
+
value=1,
|
393 |
+
step=0.01,
|
394 |
+
interactive=True,
|
395 |
+
label="Repetition penalty",
|
396 |
+
info="1.0 is equivalent to no penalty",
|
397 |
+
)
|
398 |
+
decoding_strategy = gr.Radio(
|
399 |
+
[
|
400 |
+
"Greedy",
|
401 |
+
"Top P Sampling",
|
402 |
+
],
|
403 |
+
value="Top P Sampling",
|
404 |
+
label="Decoding strategy",
|
405 |
+
interactive=True,
|
406 |
+
info="Higher values is equivalent to sampling more low-probability tokens.",
|
407 |
+
)
|
408 |
+
temperature = gr.Slider(
|
409 |
+
minimum=0.0,
|
410 |
+
maximum=2.0,
|
411 |
+
value=0.75,
|
412 |
+
step=0.05,
|
413 |
+
visible=True,
|
414 |
+
interactive=True,
|
415 |
+
label="Sampling temperature",
|
416 |
+
info="Higher values will produce more diverse outputs.",
|
417 |
+
)
|
418 |
+
top_p = gr.Slider(
|
419 |
+
minimum=0.01,
|
420 |
+
maximum=0.99,
|
421 |
+
value=0.95,
|
422 |
+
step=0.01,
|
423 |
+
visible=True,
|
424 |
+
interactive=True,
|
425 |
+
label="Top P",
|
426 |
+
info="Higher values is equivalent to sampling more low-probability tokens.",
|
427 |
+
)
|
428 |
+
|
429 |
+
|
430 |
+
chatbot = gr.Chatbot(
|
431 |
+
label="OpnGPT-4o-Chatty",
|
432 |
+
avatar_images=[None, BOT_AVATAR],
|
433 |
+
show_copy_button=True,
|
434 |
+
likeable=True,
|
435 |
+
layout="bubble"
|
436 |
+
)
|
437 |
+
|
438 |
+
output=gr.Textbox(label="Prompt")
|
439 |
+
|
440 |
+
with gr.Blocks(
|
441 |
+
fill_height=True,
|
442 |
+
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;}""",
|
443 |
+
) as img:
|
444 |
+
|
445 |
+
gr.Markdown("# Image Chat, Image Generation, Image classification and Normal Chat")
|
446 |
+
with gr.Row(elem_id="model_selector_row"):
|
447 |
+
model_selector = gr.Dropdown(
|
448 |
+
choices=MODELS.keys(),
|
449 |
+
value=list(MODELS.keys())[0],
|
450 |
+
interactive=True,
|
451 |
+
show_label=False,
|
452 |
+
container=False,
|
453 |
+
label="Model",
|
454 |
+
visible=False,
|
455 |
+
)
|
456 |
+
|
457 |
+
decoding_strategy.change(
|
458 |
+
fn=lambda selection: gr.Slider(
|
459 |
+
visible=(
|
460 |
+
selection
|
461 |
+
in [
|
462 |
+
"contrastive_sampling",
|
463 |
+
"beam_sampling",
|
464 |
+
"Top P Sampling",
|
465 |
+
"sampling_top_k",
|
466 |
+
]
|
467 |
+
)
|
468 |
+
),
|
469 |
+
inputs=decoding_strategy,
|
470 |
+
outputs=temperature,
|
471 |
+
)
|
472 |
+
decoding_strategy.change(
|
473 |
+
fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
|
474 |
+
inputs=decoding_strategy,
|
475 |
+
outputs=top_p,
|
476 |
+
)
|
477 |
+
|
478 |
+
gr.ChatInterface(
|
479 |
+
fn=model_inference,
|
480 |
+
chatbot=chatbot,
|
481 |
+
examples=EXAMPLES,
|
482 |
+
multimodal=True,
|
483 |
+
cache_examples=False,
|
484 |
+
additional_inputs=[
|
485 |
+
model_selector,
|
486 |
+
decoding_strategy,
|
487 |
+
temperature,
|
488 |
+
max_new_tokens,
|
489 |
+
repetition_penalty,
|
490 |
+
top_p,
|
491 |
+
],
|
492 |
+
)
|
493 |
+
|
494 |
+
with gr.Blocks() as voice:
|
495 |
+
with gr.Row():
|
496 |
+
input = gr.Audio(label="Voice Chat", sources="microphone", type="filepath", waveform_options=False)
|
497 |
+
output = gr.Audio(label="OpenGPT 4o", type="filepath",
|
498 |
+
interactive=False,
|
499 |
+
autoplay=True,
|
500 |
+
elem_classes="audio")
|
501 |
+
gr.Interface(
|
502 |
+
fn=respond,
|
503 |
+
inputs=[input],
|
504 |
+
outputs=[output], live=True)
|
505 |
+
|
506 |
+
with gr.Blocks(theme=theme, css="footer {visibility: hidden}textbox{resize:none}", title="GPT 4o DEMO") as demo:
|
507 |
+
gr.Markdown("# OpenGPT 4o")
|
508 |
+
gr.TabbedInterface([img, voice], ['π¬ SuperChat','π£οΈ Voice Chat', ])
|
509 |
+
|
510 |
+
demo.queue(max_size=20)
|
511 |
+
demo.launch()
|
gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.40.0
|
2 |
+
datasets
|
3 |
+
pillow
|
4 |
+
numpy
|
5 |
+
torch
|
6 |
+
streaming-stt-nemo==0.2.0
|
7 |
+
edge-tts
|
8 |
+
asyncio
|