Spaces:
Build error
Build error
| import os | |
| import gradio as gr | |
| from huggingface_hub import hf_hub_download | |
| from llama_cpp import Llama | |
| # --- Model location --- | |
| REPO_ID = os.getenv("GGUF_REPO_ID", "gladestudio/gladecore") | |
| FILENAME = os.getenv("GGUF_FILENAME", "emotion-run2-best.gguf") | |
| HF_TOKEN = os.getenv("HF_TOKEN") # not needed for public models | |
| MODEL_PATH = hf_hub_download( | |
| repo_id=REPO_ID, | |
| filename=FILENAME, | |
| repo_type="model", | |
| token=HF_TOKEN, | |
| ) | |
| # --- Llama init --- | |
| N_CTX = int(os.getenv("N_CTX", "4096")) | |
| N_THREADS = int(os.getenv("N_THREADS", str(os.cpu_count() or 4))) | |
| N_GPU_LAYERS = int(os.getenv("N_GPU_LAYERS", "0")) # >0 only on GPU Space | |
| llm = Llama( | |
| model_path=MODEL_PATH, | |
| n_ctx=N_CTX, | |
| n_threads=N_THREADS, | |
| n_gpu_layers=N_GPU_LAYERS, | |
| verbose=False, | |
| # If GGUF needs a template similar to Llama 2, uncomment: | |
| # chat_format="llama-2", | |
| ) | |
| def respond(message, history: list[dict[str, str]], system_message, max_tokens, temperature, top_p): | |
| messages = [{"role": "system", "content": system_message}] | |
| if history: | |
| messages.extend(history) | |
| messages.append({"role": "user", "content": message}) | |
| stream = llm.create_chat_completion( | |
| messages=messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=True, | |
| ) | |
| partial = "" | |
| for chunk in stream: | |
| token = ( | |
| chunk.get("choices", [{}])[0].get("delta", {}).get("content") | |
| or chunk.get("choices", [{}])[0].get("text", "") | |
| or "" | |
| ) | |
| if token: | |
| partial += token | |
| yield partial | |
| chatbot = gr.ChatInterface( | |
| respond, | |
| type="messages", | |
| additional_inputs=[ | |
| gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
| gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"), | |
| ], | |
| ) | |
| with gr.Blocks() as demo: | |
| chatbot.render() | |
| if __name__ == "__main__": | |
| demo.launch() | |