Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Code is an excerpt of https://huggingface.co/spaces/vericudebuget/mixtral-46.7b-chat
|
2 |
+
# Props to vericudebuget for the codebase, show them some love! https://huggingface.co/vericudebuget
|
3 |
+
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
import gradio as gr
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
# Initialize the InferenceClient
|
9 |
+
client = InferenceClient(
|
10 |
+
"cognitivecomputations/DeepMixtral-8x7b-Instruct"
|
11 |
+
)
|
12 |
+
|
13 |
+
def format_prompt(message, history, system_prompt):
|
14 |
+
prompt = "<s>"
|
15 |
+
for user_prompt, bot_response in history:
|
16 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
17 |
+
prompt += f" {bot_response}</s> "
|
18 |
+
prompt += f"[INST] {message} [/INST]"
|
19 |
+
# Add the system prompt only once at the beginning
|
20 |
+
prompt = system_prompt + prompt
|
21 |
+
return prompt
|
22 |
+
|
23 |
+
def generate(
|
24 |
+
prompt, history, user_system_prompt, temperature=0.9, max_new_tokens=8192, top_p=0.95, repetition_penalty=1.0
|
25 |
+
):
|
26 |
+
system_prompt = "Instructions: Everything else is from the user. You are Colet, an AI assistant created by Arcanum in 2024 (he/him). Be friendly and empathetic, matching the user's tone and vibe. Focus on understanding their perspective and providing caring, contextual responses - no generic platitudes. Keep it conversational, not overly formal. Be friendly and funny, use ASCII emojis or normal ones. Be lovely, kind, and have a great sense of humour. Do not talk about yourself unless asked. Never respond to a message with 'as an AI' or something similar. Don't talk a lot!" + str(user_system_prompt)
|
27 |
+
|
28 |
+
temperature = float(temperature)
|
29 |
+
if temperature < 1e-2:
|
30 |
+
temperature = 1e-2
|
31 |
+
|
32 |
+
top_p = float(top_p)
|
33 |
+
generate_kwargs = dict(
|
34 |
+
temperature=temperature,
|
35 |
+
max_new_tokens=max_new_tokens,
|
36 |
+
top_p=top_p,
|
37 |
+
repetition_penalty=repetition_penalty,
|
38 |
+
do_sample=True,
|
39 |
+
seed=42,
|
40 |
+
)
|
41 |
+
|
42 |
+
formatted_prompt = format_prompt(f"{prompt}", history, system_prompt)
|
43 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
44 |
+
|
45 |
+
output = ""
|
46 |
+
for response in stream:
|
47 |
+
output += response.token.text
|
48 |
+
|
49 |
+
yield output
|
50 |
+
|
51 |
+
additional_inputs = [
|
52 |
+
gr.Textbox(label="System Prompt", max_lines=1, interactive=True),
|
53 |
+
gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs"),
|
54 |
+
gr.Slider(label="Max new tokens", value=9048, minimum=256, maximum=9048, step=64, interactive=True, info="The maximum numbers of new tokens"),
|
55 |
+
gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens"),
|
56 |
+
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
|
57 |
+
]
|
58 |
+
|
59 |
+
avatar_images = ("https://i.postimg.cc/jjxNNGSD/user-circ.png", "https://i.postimg.cc/P5zYCtTG/colet-circ.png")
|
60 |
+
|
61 |
+
gr.ChatInterface(
|
62 |
+
fn=generate,
|
63 |
+
chatbot=gr.Chatbot(show_label=True, show_share_button=False, show_copy_button=True, likeable=True, layout="panel", height="auto", avatar_images=avatar_images),
|
64 |
+
additional_inputs=additional_inputs,
|
65 |
+
title="Colet",
|
66 |
+
submit_btn="➢ Submit",
|
67 |
+
retry_btn="⟳ Retry",
|
68 |
+
undo_btn="↩ Undo",
|
69 |
+
clear_btn="Clear (New chat)",
|
70 |
+
stop_btn="Stop ▢",
|
71 |
+
concurrency_limit=20,
|
72 |
+
theme=gr.themes.Soft(primary_hue=gr.themes.colors.lime),
|
73 |
+
).launch(show_api=False)
|