Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,79 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
+
# Enable the high-speed Rust-based downloader from your script
|
| 7 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 8 |
|
| 9 |
+
# Your Model List from the original script
|
| 10 |
+
MODELS = {
|
| 11 |
+
"Llama 3.2 1B (Text)": {"repo": "unsloth/Llama-3.2-1B-Instruct-GGUF", "file": "Llama-3.2-1B-Instruct-Q4_K_M.gguf"},
|
| 12 |
+
"Llama 3.2 3B": {"repo": "bartowski/Llama-3.2-3B-Instruct-GGUF", "file": "Llama-3.2-3B-Instruct-Q4_K_M.gguf"},
|
| 13 |
+
"Qwen 2.5 1.5B (Code)": {"repo": "Qwen/Qwen2.5-1.5B-Instruct-GGUF", "file": "qwen2.5-1.5b-instruct-q4_k_m.gguf"},
|
| 14 |
+
"Phi-3.5 Mini (Microsoft)": {"repo": "bartowski/Phi-3.5-mini-instruct-GGUF", "file": "Phi-3.5-mini-instruct-Q4_K_M.gguf"},
|
| 15 |
+
"DeepSeek R1 1.5B": {"repo": "bartowski/DeepSeek-R1-Distill-Qwen-1.5B-GGUF", "file": "DeepSeek-R1-Distill-Qwen-1.5B-Q4_K_M.gguf"},
|
| 16 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Global LLM instance
|
| 19 |
+
llm_instance = None
|
| 20 |
|
| 21 |
+
def load_and_chat(message, history, hf_token, model_choice):
|
| 22 |
+
global llm_instance
|
| 23 |
+
|
| 24 |
+
if not hf_token or len(hf_token) < 10:
|
| 25 |
+
yield "⚠️ Please enter a valid Hugging Face 'Read' Token in the sidebar first."
|
| 26 |
+
return
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
# Download and Initialize only if not already loaded
|
| 30 |
+
if llm_instance is None:
|
| 31 |
+
yield f"⏳ Authenticating and downloading {model_choice}..."
|
| 32 |
+
path = hf_hub_download(
|
| 33 |
+
repo_id=MODELS[model_choice]["repo"],
|
| 34 |
+
filename=MODELS[model_choice]["file"],
|
| 35 |
+
token=hf_token # Uses the user's specific token
|
| 36 |
+
)
|
| 37 |
+
llm_instance = Llama(model_path=path, n_ctx=2048, n_threads=4, verbose=False)
|
| 38 |
+
|
| 39 |
+
# Generation Logic
|
| 40 |
+
prompt = f"<|user|>\n{message}\n<|assistant|>\n"
|
| 41 |
+
response = ""
|
| 42 |
+
for chunk in llm_instance(prompt, max_tokens=512, stream=True):
|
| 43 |
+
token = chunk["choices"][0]["text"]
|
| 44 |
+
response += token
|
| 45 |
+
yield response
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
yield f"❌ Error: {str(e)}. Check if your token has 'Read' permissions."
|
| 49 |
|
| 50 |
+
# UI Layout
|
| 51 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 52 |
+
gr.Markdown("# 🚀 BEN AI - Secure Local Intelligence")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column(scale=1):
|
| 56 |
+
gr.Markdown("### 🔑 Authentication")
|
| 57 |
+
user_hf_token = gr.Textbox(
|
| 58 |
+
label="Your HF 'Read' Token",
|
| 59 |
+
placeholder="paste hf_...",
|
| 60 |
+
type="password"
|
| 61 |
+
)
|
| 62 |
+
model_select = gr.Dropdown(
|
| 63 |
+
choices=list(MODELS.keys()),
|
| 64 |
+
value="Llama 3.2 1B (Text)",
|
| 65 |
+
label="Target Model"
|
| 66 |
+
)
|
| 67 |
+
gr.Markdown("---")
|
| 68 |
+
gr.Markdown("💡 **Note:** Your token is used only for this session to download the model weights.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
with gr.Column(scale=4):
|
| 71 |
+
gr.ChatInterface(
|
| 72 |
+
fn=load_and_chat,
|
| 73 |
+
additional_inputs=[user_hf_token, model_select],
|
| 74 |
+
title="Chat Studio",
|
| 75 |
+
description="Model weights will download once you send your first message with a valid token."
|
| 76 |
+
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|