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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from threading import Thread
|
| 8 |
+
from huggingface_hub import login
|
| 9 |
+
from icrawler.builtin import BingImageCrawler
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
MODEL_LIST = ["mistralai/Mistral-Nemo-Instruct-2407"]
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 14 |
+
login(token=HF_TOKEN)
|
| 15 |
+
#MODEL = os.environ.get("MODEL_ID")
|
| 16 |
+
MODEL = "mistralai/Mistral-Nemo-Instruct-2407"
|
| 17 |
+
|
| 18 |
+
TITLE = "<h1><center>Mistral-Nemo</center></h1>"
|
| 19 |
+
|
| 20 |
+
PLACEHOLDER = """
|
| 21 |
+
<center>
|
| 22 |
+
<p>The Mistral-Nemo is a pretrained generative text model of 12B parameters trained jointly by Mistral AI and NVIDIA.</p>
|
| 23 |
+
</center>
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
CSS = """
|
| 28 |
+
.duplicate-button {
|
| 29 |
+
margin: auto !important;
|
| 30 |
+
color: white !important;
|
| 31 |
+
background: black !important;
|
| 32 |
+
border-radius: 100vh !important;
|
| 33 |
+
}
|
| 34 |
+
h3 {
|
| 35 |
+
text-align: center;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
#output_video {
|
| 39 |
+
display: block;
|
| 40 |
+
margin-left: auto!important;
|
| 41 |
+
margin-right: auto !important;
|
| 42 |
+
width: 20vw !important;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
footer{visibility: hidden}
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
device = "cuda" # for GPU usage or "cpu" for CPU usage
|
| 49 |
+
|
| 50 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 51 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 52 |
+
MODEL,
|
| 53 |
+
torch_dtype=torch.bfloat16,
|
| 54 |
+
device_map="auto",
|
| 55 |
+
ignore_mismatched_sizes=True)
|
| 56 |
+
|
| 57 |
+
@spaces.GPU()
|
| 58 |
+
def get_response(conversation):
|
| 59 |
+
temperature = 0.3
|
| 60 |
+
max_new_tokens = 512
|
| 61 |
+
top_p = 1.0
|
| 62 |
+
top_k = 20
|
| 63 |
+
penalty = 1.2
|
| 64 |
+
|
| 65 |
+
input_text=tokenizer.apply_chat_template(conversation, tokenize=False)
|
| 66 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 67 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 68 |
+
|
| 69 |
+
generate_kwargs = dict(
|
| 70 |
+
input_ids=inputs,
|
| 71 |
+
max_new_tokens = max_new_tokens,
|
| 72 |
+
do_sample = False if temperature == 0 else True,
|
| 73 |
+
top_p = top_p,
|
| 74 |
+
top_k = top_k,
|
| 75 |
+
temperature = temperature,
|
| 76 |
+
streamer=streamer,
|
| 77 |
+
repetition_penalty=penalty,
|
| 78 |
+
pad_token_id = 10,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
with torch.no_grad():
|
| 82 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 83 |
+
thread.start()
|
| 84 |
+
|
| 85 |
+
buffer = ""
|
| 86 |
+
for new_text in streamer:
|
| 87 |
+
buffer += new_text
|
| 88 |
+
#yield buffer
|
| 89 |
+
|
| 90 |
+
return buffer
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@spaces.GPU()
|
| 94 |
+
def stream_chat(history, character_a, character_b):
|
| 95 |
+
|
| 96 |
+
# From A to B
|
| 97 |
+
if(history==[]):
|
| 98 |
+
conversation_a = [{"role": "system", "content": "You should respond like " + character_b + ". You should have a meaningful conversation. Don't repeat yourself. You should only output your response. You don't need to put quotes around what your saying. You don't need to put your name at the beginning of your response."}]
|
| 99 |
+
for answer, prompt in history:
|
| 100 |
+
conversation_a.extend([
|
| 101 |
+
{"role": "user", "content": prompt},
|
| 102 |
+
{"role": "assistant", "content": answer},
|
| 103 |
+
])
|
| 104 |
+
conversation_a.append({"role": "user", "content": "You are having a conversation with " + character_a + ". Introduce yourself."})
|
| 105 |
+
response_b = get_response(conversation_a)
|
| 106 |
+
print('response_b', response_b)
|
| 107 |
+
else:
|
| 108 |
+
conversation_a = [{"role": "system", "content": "You should respond like " + character_b + ". You should have a meaningful conversation. Don't repeat yourself. You should only output your response. You don't need to put quotes around what your saying. You don't need to put your name at the beginning of your response."}]
|
| 109 |
+
for answer, prompt in history:
|
| 110 |
+
conversation_a.extend([
|
| 111 |
+
{"role": "user", "content": prompt},
|
| 112 |
+
{"role": "assistant", "content": answer},
|
| 113 |
+
])
|
| 114 |
+
conversation_a.append({"role": "user", "content": history[-1][0] })
|
| 115 |
+
response_b = get_response(conversation_a)
|
| 116 |
+
print('response_b', response_b)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# From B to A
|
| 120 |
+
conversation_b = [{"role": "system", "content": "You should respond like " + character_a + ". You should have a meaningful conversation. Don't repeat yourself. You should only output your response. You don't need to put quotes around what your saying. You don't need to put your name at the beginning of your response."}]
|
| 121 |
+
for prompt, answer in history:
|
| 122 |
+
conversation_b.extend([
|
| 123 |
+
{"role": "user", "content": prompt},
|
| 124 |
+
{"role": "assistant", "content": answer},
|
| 125 |
+
])
|
| 126 |
+
conversation_b.append({"role": "user", "content": response_b })
|
| 127 |
+
response_a = get_response(conversation_b)
|
| 128 |
+
print('response_a', response_a)
|
| 129 |
+
|
| 130 |
+
# Append responses to history
|
| 131 |
+
history.append((response_b , response_a ))
|
| 132 |
+
print('history', history)
|
| 133 |
+
|
| 134 |
+
return history
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def get_img(keyword):
|
| 138 |
+
path = './' + keyword
|
| 139 |
+
os.makedirs(path, exist_ok=True)
|
| 140 |
+
bing_crawler = BingImageCrawler(storage={'root_dir': path})
|
| 141 |
+
bing_crawler.crawl(keyword=keyword, max_num=1)
|
| 142 |
+
|
| 143 |
+
# Look for image files in the folder
|
| 144 |
+
for file_name in os.listdir(path):
|
| 145 |
+
if file_name.lower().endswith(('.png', '.PNG', '.JPG', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')):
|
| 146 |
+
return os.path.join(path, file_name)
|
| 147 |
+
|
| 148 |
+
# If no image is found
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
+
def set_characters(a, b):
|
| 152 |
+
img_a = get_img(a)
|
| 153 |
+
img_b = get_img(b)
|
| 154 |
+
return img_a, img_b, gr.update(avatar_images=(img_b, img_a))
|
| 155 |
+
|
| 156 |
+
chatbot = gr.Chatbot(height=600,show_label=False)
|
| 157 |
+
|
| 158 |
+
theme = gr.themes.Base(
|
| 159 |
+
).set(
|
| 160 |
+
body_background_fill="#e1fceb",
|
| 161 |
+
color_accent_soft="#ffffff",
|
| 162 |
+
border_color_accent="#e1fceb",
|
| 163 |
+
border_color_primary="#e1fceb",
|
| 164 |
+
background_fill_secondary="#e1fceb",
|
| 165 |
+
button_secondary_background_fill="#ffffff",
|
| 166 |
+
button_primary_background_fill="#ffffff",
|
| 167 |
+
button_primary_text_color="#1f2937",
|
| 168 |
+
input_background_fill="#f8f8f8",
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
with gr.Blocks(css=CSS, theme=theme) as demo:
|
| 172 |
+
gr.HTML("""
|
| 173 |
+
|
| 174 |
+
<center> <h1> Bot vs Bot </h1> </center>
|
| 175 |
+
|
| 176 |
+
<center> by <a href="https://www.tonyassi.com/">Tony Assi</a> </center>
|
| 177 |
+
|
| 178 |
+
<center> <h3> Pick two icons and watch them have a conversation </h3> </center>
|
| 179 |
+
""")
|
| 180 |
+
with gr.Row():
|
| 181 |
+
character_a = gr.Textbox(label='Character A', info='Choose a person', placeholder='Socrates, Edgar Allen Poe, George Washington')
|
| 182 |
+
character_b = gr.Textbox(label='Character B', info='Choose a person', placeholder='Madonna, Paris Hilton, Liza Minnelli')
|
| 183 |
+
character_button = gr.Button('Initiate Characters')
|
| 184 |
+
#characters_html = gr.HTML()
|
| 185 |
+
with gr.Row():
|
| 186 |
+
image_a = gr.Image(show_label=False, interactive=False, show_fullscreen_button=False, show_share_button=False, show_download_button=False)
|
| 187 |
+
gr.Markdown(' ')
|
| 188 |
+
image_b = gr.Image(show_label=False, interactive=False, show_fullscreen_button=False, show_share_button=False, show_download_button=False)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
chat = gr.Chatbot(show_label=False)
|
| 192 |
+
submit_button = gr.Button()
|
| 193 |
+
|
| 194 |
+
character_button.click(set_characters, inputs=[character_a, character_b], outputs=[image_a, image_b, chat])
|
| 195 |
+
submit_button.click(stream_chat, inputs=[chat, character_a, character_b], outputs=[chat])
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
#gr.HTML(TITLE)
|
| 199 |
+
#gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
| 200 |
+
#video = gr.HTML("""
|
| 201 |
+
# <video src="https://huggingface.co/spaces/ahjvdjf33/moods/resolve/main/Paris-Moods-2/passive3.mp4"
|
| 202 |
+
# playsinline autoplay loop muted></video>
|
| 203 |
+
#""", elem_id="output_video")
|
| 204 |
+
"""
|
| 205 |
+
gr.ChatInterface(
|
| 206 |
+
fn=stream_chat,
|
| 207 |
+
chatbot=chatbot,
|
| 208 |
+
textbox=gr.Textbox(visible=False),
|
| 209 |
+
fill_height=True,
|
| 210 |
+
retry_btn=None,
|
| 211 |
+
undo_btn=None,
|
| 212 |
+
clear_btn=None,
|
| 213 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False, visible=False),
|
| 214 |
+
additional_inputs=[
|
| 215 |
+
gr.Slider(
|
| 216 |
+
minimum=0,
|
| 217 |
+
maximum=1,
|
| 218 |
+
step=0.1,
|
| 219 |
+
value=0.3,
|
| 220 |
+
label="Temperature",
|
| 221 |
+
render=False,
|
| 222 |
+
),
|
| 223 |
+
gr.Slider(
|
| 224 |
+
minimum=128,
|
| 225 |
+
maximum=8192,
|
| 226 |
+
step=1,
|
| 227 |
+
value=1024,
|
| 228 |
+
label="Max new tokens",
|
| 229 |
+
render=False,
|
| 230 |
+
),
|
| 231 |
+
gr.Slider(
|
| 232 |
+
minimum=0.0,
|
| 233 |
+
maximum=1.0,
|
| 234 |
+
step=0.1,
|
| 235 |
+
value=1.0,
|
| 236 |
+
label="top_p",
|
| 237 |
+
render=False,
|
| 238 |
+
),
|
| 239 |
+
gr.Slider(
|
| 240 |
+
minimum=1,
|
| 241 |
+
maximum=20,
|
| 242 |
+
step=1,
|
| 243 |
+
value=20,
|
| 244 |
+
label="top_k",
|
| 245 |
+
render=False,
|
| 246 |
+
),
|
| 247 |
+
gr.Slider(
|
| 248 |
+
minimum=0.0,
|
| 249 |
+
maximum=2.0,
|
| 250 |
+
step=0.1,
|
| 251 |
+
value=1.2,
|
| 252 |
+
label="Repetition penalty",
|
| 253 |
+
render=False,
|
| 254 |
+
),
|
| 255 |
+
],
|
| 256 |
+
)
|
| 257 |
+
"""
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
if __name__ == "__main__":
|
| 262 |
+
demo.launch()
|