aditya.bhendavadekar commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import Repository
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Load the model from your Hugging Face account
|
| 7 |
+
model_name = "adi1193/mistral-postv6"
|
| 8 |
+
repository = Repository(model_name, clone_from="adi1193/mistral-postv6")
|
| 9 |
+
|
| 10 |
+
# Load the model
|
| 11 |
+
model_path = repository.local_dir
|
| 12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 14 |
+
|
| 15 |
+
def format_prompt(message, history, enable_hinglish=False):
|
| 16 |
+
prompt = "<s>"
|
| 17 |
+
# Adding the Hinglish prompt
|
| 18 |
+
if enable_hinglish and not any("[INST] You are a Hinglish LLM." in user_prompt for user_prompt, bot_response in history):
|
| 19 |
+
prompt += Hinglish_Prompt
|
| 20 |
+
|
| 21 |
+
for user_prompt, bot_response in history:
|
| 22 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 23 |
+
prompt += f" {bot_response} "
|
| 24 |
+
prompt += f"[INST] {message} [/INST]"
|
| 25 |
+
return prompt
|
| 26 |
+
|
| 27 |
+
def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, enable_hinglish=False):
|
| 28 |
+
temperature = float(temperature)
|
| 29 |
+
if temperature < 1e-2:
|
| 30 |
+
temperature = 1e-2
|
| 31 |
+
top_p = float(top_p)
|
| 32 |
+
|
| 33 |
+
generate_kwargs = {
|
| 34 |
+
"model": model,
|
| 35 |
+
"tokenizer": tokenizer,
|
| 36 |
+
"max_length": max_new_tokens + len(tokenizer.encode(prompt)),
|
| 37 |
+
"temperature": temperature,
|
| 38 |
+
"top_p": top_p,
|
| 39 |
+
"repetition_penalty": repetition_penalty,
|
| 40 |
+
"do_sample": True,
|
| 41 |
+
"seed": 42,
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
formatted_prompt = format_prompt(prompt, history, enable_hinglish)
|
| 45 |
+
input_ids = tokenizer.encode(formatted_prompt, return_tensors="pt")
|
| 46 |
+
output = model.generate(input_ids, **generate_kwargs)
|
| 47 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 48 |
+
|
| 49 |
+
additional_inputs=[
|
| 50 |
+
gr.Slider(
|
| 51 |
+
label="Temperature",
|
| 52 |
+
value=0.9,
|
| 53 |
+
minimum=0.0,
|
| 54 |
+
maximum=1.0,
|
| 55 |
+
step=0.05,
|
| 56 |
+
interactive=True,
|
| 57 |
+
info="Higher values produce more diverse outputs",
|
| 58 |
+
),
|
| 59 |
+
gr.Slider(
|
| 60 |
+
label="Max new tokens",
|
| 61 |
+
value=256,
|
| 62 |
+
minimum=0,
|
| 63 |
+
maximum=1048,
|
| 64 |
+
step=64,
|
| 65 |
+
interactive=True,
|
| 66 |
+
info="The maximum numbers of new tokens",
|
| 67 |
+
),
|
| 68 |
+
gr.Slider(
|
| 69 |
+
label="Top-p (nucleus sampling)",
|
| 70 |
+
value=0.90,
|
| 71 |
+
minimum=0.0,
|
| 72 |
+
maximum=1,
|
| 73 |
+
step=0.05,
|
| 74 |
+
interactive=True,
|
| 75 |
+
info="Higher values sample more low-probability tokens",
|
| 76 |
+
),
|
| 77 |
+
gr.Slider(
|
| 78 |
+
label="Repetition penalty",
|
| 79 |
+
value=1.2,
|
| 80 |
+
minimum=1.0,
|
| 81 |
+
maximum=2.0,
|
| 82 |
+
step=0.05,
|
| 83 |
+
interactive=True,
|
| 84 |
+
info="Penalize repeated tokens",
|
| 85 |
+
),
|
| 86 |
+
gr.Checkbox(
|
| 87 |
+
label="Hinglish",
|
| 88 |
+
value=False,
|
| 89 |
+
interactive=True,
|
| 90 |
+
info="Enables the MistralTalk to talk in Hinglish (Combination of Hindi and English)",
|
| 91 |
+
)
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
css = """
|
| 95 |
+
#mkd {
|
| 96 |
+
height: 500px;
|
| 97 |
+
overflow: auto;
|
| 98 |
+
border: 1px solid #ccc;
|
| 99 |
+
}
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
with gr.Blocks(css=css) as demo:
|
| 103 |
+
gr.HTML("<h1><center>MistralTalk🗣️<h1><center>")
|
| 104 |
+
gr.HTML("<h3><center>In this demo, you can chat with <a href='https://huggingface.co/adi1193/mistral-postv6'>Mistral-8x7B</a> model. 💬<h3><center>")
|
| 105 |
+
gr.HTML("<h3><center>Learn more about the model <a href='https://huggingface.co/docs/transformers/main/model_doc/mistral'>here</a>. 📚<h3><center>")
|
| 106 |
+
gr.ChatInterface(
|
| 107 |
+
generate,
|
| 108 |
+
additional_inputs=additional_inputs,
|
| 109 |
+
theme = gr.themes.Soft(),
|
| 110 |
+
examples=[["What is the interest?"], ["How does the universe work?"],["What can you do?"],["What is quantum mechanics?"],["Do you believe in an after life?"]]
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|