Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
| 5 |
|
| 6 |
# 1. Download the highly optimized Llama 3.2 3B model from Hugging Face repository
|
| 7 |
-
print("Downloading model...
|
| 8 |
model_path = hf_hub_download(
|
| 9 |
repo_id="bartowski/Llama-3.2-3B-Instruct-GGUF",
|
| 10 |
-
filename="Llama-3.2-3B-Instruct-Q4_K_M.gguf"
|
| 11 |
)
|
| 12 |
|
| 13 |
# 2. Initialize the model on the CPU
|
| 14 |
print("Initializing model...")
|
| 15 |
llm = Llama(
|
| 16 |
model_path=model_path,
|
| 17 |
-
n_ctx=2048, # Context length
|
| 18 |
n_threads=2 # Utilizes both free CPU cores fully
|
| 19 |
)
|
| 20 |
|
|
@@ -25,7 +25,11 @@ def respond(message, chat_history):
|
|
| 25 |
formatted_prompt += "You are a helpful, direct, and honest AI assistant.<|eot_id|>"
|
| 26 |
|
| 27 |
# Inject chat history so the bot remembers the conversation context
|
| 28 |
-
for
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if user_msg:
|
| 30 |
formatted_prompt += f"<|start_header_id|>user<|end_header_id|>\n{user_msg}<|eot_id|>"
|
| 31 |
if bot_msg:
|
|
@@ -44,12 +48,12 @@ def respond(message, chat_history):
|
|
| 44 |
|
| 45 |
token_accumulator = ""
|
| 46 |
for token in output:
|
| 47 |
-
token_text = token["choices"][
|
| 48 |
token_accumulator += token_text
|
| 49 |
yield token_accumulator
|
| 50 |
|
| 51 |
-
# 4. Create the web dashboard layout using Gradio
|
| 52 |
-
demo =
|
| 53 |
fn=respond,
|
| 54 |
title="🤖 Free Llama 3.2 CPU Chatbot",
|
| 55 |
description="Running 24/7/365 for free on Hugging Face Spaces using CPU inference.",
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from llama_cpp import Llama
|
| 5 |
|
| 6 |
# 1. Download the highly optimized Llama 3.2 3B model from Hugging Face repository
|
| 7 |
+
print("Downloading model...")
|
| 8 |
model_path = hf_hub_download(
|
| 9 |
repo_id="bartowski/Llama-3.2-3B-Instruct-GGUF",
|
| 10 |
+
filename="Llama-3.2-3B-Instruct-Q4_K_M.gguf"
|
| 11 |
)
|
| 12 |
|
| 13 |
# 2. Initialize the model on the CPU
|
| 14 |
print("Initializing model...")
|
| 15 |
llm = Llama(
|
| 16 |
model_path=model_path,
|
| 17 |
+
n_ctx=2048, # Context length
|
| 18 |
n_threads=2 # Utilizes both free CPU cores fully
|
| 19 |
)
|
| 20 |
|
|
|
|
| 25 |
formatted_prompt += "You are a helpful, direct, and honest AI assistant.<|eot_id|>"
|
| 26 |
|
| 27 |
# Inject chat history so the bot remembers the conversation context
|
| 28 |
+
for turn in chat_history:
|
| 29 |
+
# Check if history is structured as objects or dicts (Gradio 6 style)
|
| 30 |
+
user_msg = turn.get("text") if isinstance(turn, dict) else turn[0]
|
| 31 |
+
bot_msg = turn.get("text") if isinstance(turn, dict) else turn[1]
|
| 32 |
+
|
| 33 |
if user_msg:
|
| 34 |
formatted_prompt += f"<|start_header_id|>user<|end_header_id|>\n{user_msg}<|eot_id|>"
|
| 35 |
if bot_msg:
|
|
|
|
| 48 |
|
| 49 |
token_accumulator = ""
|
| 50 |
for token in output:
|
| 51 |
+
token_text = token["choices"]["text"]
|
| 52 |
token_accumulator += token_text
|
| 53 |
yield token_accumulator
|
| 54 |
|
| 55 |
+
# 4. Create the web dashboard layout using Gradio ChatInterface
|
| 56 |
+
demo = gr.ChatInterface(
|
| 57 |
fn=respond,
|
| 58 |
title="🤖 Free Llama 3.2 CPU Chatbot",
|
| 59 |
description="Running 24/7/365 for free on Hugging Face Spaces using CPU inference.",
|