Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
+
from llama_cpp import Llama
|
4 |
+
|
5 |
+
# Define model details
|
6 |
+
MODEL_REPO = "TheBloke/vicuna-13B-v1.5-16K-GGUF" # You can swap this for Mistral-7B or another GGUF model
|
7 |
+
MODEL_FILE = "vicuna-13b-v1.5-16k.Q4_K_M.gguf" # 4-bit quantized model file
|
8 |
+
|
9 |
+
# Download the quantized model from Hugging Face
|
10 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
11 |
+
|
12 |
+
# Load the model with llama.cpp for CPU-only inference
|
13 |
+
llm = Llama(
|
14 |
+
model_path=model_path,
|
15 |
+
n_gpu_layers=0, # Set to 0 for CPU-only
|
16 |
+
n_threads=4, # Adjust based on CPU cores (e.g., 4 for quad-core)
|
17 |
+
n_batch=512, # Batch size for inference
|
18 |
+
n_ctx=2048, # Context length (adjust based on RAM; 2048 fits ~16 GB)
|
19 |
+
verbose=False # Reduce logging for cleaner output
|
20 |
+
)
|
21 |
+
|
22 |
+
# Define the inference function
|
23 |
+
def generate_text(prompt, max_tokens=256, temperature=0.8, top_p=0.95):
|
24 |
+
try:
|
25 |
+
output = llm(
|
26 |
+
prompt,
|
27 |
+
max_tokens=max_tokens,
|
28 |
+
temperature=temperature,
|
29 |
+
top_p=top_p,
|
30 |
+
repeat_penalty=1.1
|
31 |
+
)
|
32 |
+
return output["choices"][0]["text"].strip()
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error: {str(e)}"
|
35 |
+
|
36 |
+
# Create Gradio interface
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=generate_text,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
|
41 |
+
gr.Slider(label="Max Tokens", minimum=50, maximum=512, value=256, step=10),
|
42 |
+
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, value=0.8, step=0.1),
|
43 |
+
gr.Slider(label="Top P", minimum=0.1, maximum=1.0, value=0.95, step=0.05)
|
44 |
+
],
|
45 |
+
outputs=gr.Textbox(label="Generated Text"),
|
46 |
+
title="Quantized LLM on Hugging Face Spaces",
|
47 |
+
description="Run a 4-bit quantized Vicuna-13B model on CPU using llama.cpp",
|
48 |
+
theme="default"
|
49 |
+
)
|
50 |
+
|
51 |
+
# Launch the app
|
52 |
+
if __name__ == "__main__":
|
53 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|