import gradio as gr from gpt4all import GPT4All from huggingface_hub import hf_hub_download title = "Malayalam-llama-7b-instruct-v0.1 (CPU Demo)" description = """ [Malayalam LLaMA 7B Instruct v0.1](https://huggingface.co/abhinand/malayalam-llama-7b-instruct-v0.1) Running on free CPU hardware. Suggest duplicating this space to run without a queue. **Note:** The inference is quite slow as it is running on CPU. """ model_path = "models" model_name = "malayalam-llama-7b-instruct-v0.1.Q5_K_M.gguf" hf_hub_download(repo_id="abhinand/malayalam-llama-7b-instruct-v0.1-GGUF", filename=model_name, local_dir=model_path, local_dir_use_symlinks=False) print("Start the model init process") model = model = GPT4All(model_name, model_path, allow_download = False, device="cpu") print("Finish the model init process") model.config["promptTemplate"] = """ഞാൻ ഒരു സഹായകരവും, സത്യസന്ധവും ആയ നിർമ്മിത ബുദ്ധി ആണ്. ഞാൻ എപ്പോഴും നിങ്ങളുടെ സുരക്ഷയെ മുൻനിർത്തി കഴിയുന്നത്ര സഹായകരമായ പ്രതികരണം നൽകുന്നു. നിങ്ങളുടെ പ്രതികരണം മറ്റുള്ളവർക്ക് ഏതൊരു വിധത്തിലും ദോഷം, ഉപദ്രവം, അപകടം ഉണ്ടാകുന്നത് അല്ല എന്ന് നിങ്ങൾ ഉറപ്പുവരുത്തണം. നിങ്ങളുടെ പ്രതികരണത്തിന്റെ ദോഷത്തെ കുറിച്ച് നിങ്ങൾക്ക് ബോധ്യം ഇല്ലെങ്കിൽ, നിങ്ങൾക്ക് ലഭിക്കുന്ന നിർദ്ദേശങ്ങൾ നിങ്ങൾ മറ്റുള്ളവരുമായി പങ്കുവെക്കാതിരിക്കുക. ### Instruction: {0} ### Response: """ model.config["systemPrompt"] = "" model._is_chat_session_activated = False max_new_tokens = 2048 def generator(message, history, temperature, top_p, top_k): prompt = "" for user_message, assistant_message in history: prompt += model.config["promptTemplate"].format(user_message) prompt += model.config["promptTemplate"].format(message) outputs = [] for token in model.generate(prompt=prompt, temp=temperature, top_k = top_k, top_p = top_p, max_tokens = max_new_tokens, streaming=True): outputs.append(token) yield "".join(outputs) def vote(data: gr.LikeData): if data.liked: return else: return chatbot = gr.Chatbot(bubble_full_width=False) additional_inputs=[ gr.Slider( label="temperature", value=0.2, minimum=0.0, maximum=2.0, step=0.05, interactive=True, info="Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.", ), gr.Slider( label="top_p", value=1.0, minimum=0.0, maximum=1.0, step=0.01, interactive=True, info="0.1 means only the tokens comprising the top 10% probability mass are considered. Suggest set to 1 and use temperature. 1 means 100% and will disable it", ), gr.Slider( label="top_k", value=40, minimum=0, maximum=1000, step=1, interactive=True, info="limits candidate tokens to a fixed number after sorting by probability. Setting it higher than the vocabulary size deactivates this limit.", ) ] iface = gr.ChatInterface( fn = generator, title=title, description = description, chatbot=chatbot, additional_inputs=additional_inputs, examples=[ ["നിങ്ങൾ ആരാണ് ?"], ["ഒരു list sort ചെയ്യാൻ ഉള്ള python function എഴുതുക."], ["ചുവപ്പും മഞ്ഞയും കൂടിചേർന്നാൽ ലഭിക്കുന്ന നിറം എന്ത്?"], ["പെട്ടന്ന് ഉറങ്ങാൻ എന്താണ് ചെയ്യേണ്ടത്?"] ] ) with gr.Blocks(css="css/index.css") as demo: chatbot.like(vote, None, None) iface.render() if __name__ == "__main__": demo.queue(max_size=3).launch()