Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 3 |
|
| 4 |
# Automatically download model to local path
|
| 5 |
model_path = hf_hub_download(
|
|
@@ -9,3 +10,31 @@ model_path = hf_hub_download(
|
|
| 9 |
|
| 10 |
# Load the model
|
| 11 |
llm = Llama(model_path=model_path, n_ctx=2048)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
# Automatically download model to local path
|
| 6 |
model_path = hf_hub_download(
|
|
|
|
| 10 |
|
| 11 |
# Load the model
|
| 12 |
llm = Llama(model_path=model_path, n_ctx=2048)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def chatbot_response(message, history):
|
| 17 |
+
# Format history for chat template
|
| 18 |
+
formatted_history = ""
|
| 19 |
+
for user, bot in history[-4:]: # Last 4 turns
|
| 20 |
+
formatted_history += f"<|user|>\n{user}<|end|>\n<|assistant|>\n{bot}<|end|>\n"
|
| 21 |
+
prompt = formatted_history + f"<|user|>\n{message}<|end|>\n<|assistant|>\n"
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
output = llm(prompt, max_tokens=256, stop=["<|end|>"], temperature=0.7)
|
| 25 |
+
text = output["choices"][0]["text"].strip()
|
| 26 |
+
return text
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {e}"
|
| 29 |
+
|
| 30 |
+
demo = gr.ChatInterface(
|
| 31 |
+
fn=chatbot_response,
|
| 32 |
+
title="🧠 Free CPU Chatbot (OpenHermes-2.5)",
|
| 33 |
+
description="A lightweight, high-quality chatbot that runs on CPU using llama.cpp",
|
| 34 |
+
theme="soft",
|
| 35 |
+
examples=["What's your name?", "Tell me a joke", "What is Python used for?"]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch(share=True)
|
| 40 |
+
|