import os from dotenv import load_dotenv import numpy as np from huggingface_hub import snapshot_download #from difflib import Differ import gradio as gr #from transformers import pipeline #from llama_cpp import Llama from ctransformers import AutoModelForCausalLM load_dotenv() task_asr=os.getenv('TASK_TXTGEN') model_id=os.getenv('MODEL_MISTRAL') model_file=os.getenv('MODEL_MISTRAL_FILE') cached_folder=snapshot_download( repo_id=model_id, allow_patterns=model_file, local_dir=None ) def get_cached_folder(): return cached_folder llm = AutoModelForCausalLM.from_pretrained( model_id, model_file=model_file, model_type="llama" # gpu_layers=50 ) B_INST, E_INST = "[INST]", "[/INST]" B_SYS, E_SYS = "<>\n", "\n<>\n\n" DEFAULT_SYSTEM_PROMPT = """\ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. Correct the grammar in the user text. """ SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS def get_prompt(instruction): prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST return prompt_template def improve_grammar(text_input): #https://huggingface.co/mzbac/mistral-grammar #https://llama-cpp-python.readthedocs.io/en/latest/ input_prompt = get_prompt(text_input) text_output = llm( input_prompt, temperature=0.0, # top_p=0.1, # top_k=40, # repeat_penalty=1.1, # max_tokens=2048, # n_ctx=2048, # echo=False ) return text_output if __name__ == "__main__": with gr.Blocks() as demo: mytextinput = gr.Textbox("This is a test box.") b1 = gr.Button("Click to improve text") mytextimproved = gr.Textbox( label="Improved text" ) b1.click( fn=improve_grammar, inputs=mytextinput, outputs=mytextimproved ) demo.queue() demo.launch( share=False, server_name=os.getenv('GRADIO_SERVER_IP'), server_port=int(os.getenv('GRADIO_SERVER_PORT')) )