Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
SYSTEM_PROMPT = """You are a
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
def respond(message, history):
|
| 20 |
-
# Format prompt
|
| 21 |
-
|
| 22 |
-
<|user|>{message}</s>
|
| 23 |
-
<|assistant|>"""
|
| 24 |
|
| 25 |
# Generate response
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
temperature=0.
|
| 30 |
-
stop=["
|
| 31 |
)
|
| 32 |
|
| 33 |
-
return
|
| 34 |
|
| 35 |
-
# Gradio interface
|
| 36 |
-
gr.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from ctransformers import AutoModelForCausalLM
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Download model (runs on first launch)
|
| 6 |
+
MODEL_PATH = "Phi-3.5-mini-3.8B-ArliAI-RPMax-v1.1-Q6_K_L.gguf"
|
| 7 |
+
if not os.path.exists(MODEL_PATH):
|
| 8 |
+
os.system(f"wget https://huggingface.co/ArliAI/Phi-3.5-mini-3.8B-ArliAI-RPMax-v1.1-GGUF/resolve/main/{MODEL_PATH}")
|
| 9 |
+
|
| 10 |
+
# Load GGUF model with ctransformers
|
| 11 |
+
llm = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_PATH,
|
| 13 |
+
model_type="phi3",
|
| 14 |
+
gpu_layers=50, # Offload to GPU (set to 0 for CPU)
|
| 15 |
+
context_length=2048
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# System prompt to restrict knowledge
|
| 19 |
+
SYSTEM_PROMPT = """[SYSTEM] You are a compliance assistant. Follow these rules:
|
| 20 |
+
1. ONLY use data from '/data/company_policies.pdf' (provided in this Space's files)
|
| 21 |
+
2. If asked about unverified information, respond: "I can only reference approved documents"
|
| 22 |
+
3. Keep answers under 2 sentences."""
|
| 23 |
|
| 24 |
def respond(message, history):
|
| 25 |
+
# Format Phi-3 prompt template
|
| 26 |
+
prompt = f"{SYSTEM_PROMPT}\n[USER]{message}\n[ASSISTANT]"
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Generate response
|
| 29 |
+
response = llm(
|
| 30 |
+
prompt,
|
| 31 |
+
max_new_tokens=100,
|
| 32 |
+
temperature=0.3, # Low for deterministic answers
|
| 33 |
+
stop=["[USER]", "\n\n"]
|
| 34 |
)
|
| 35 |
|
| 36 |
+
return response
|
| 37 |
|
| 38 |
+
# Gradio interface with file upload for knowledge base
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("## Phi-3.5 Mini - Restricted Knowledge Assistant")
|
| 41 |
+
with gr.Tab("Chat"):
|
| 42 |
+
chat_interface = gr.ChatInterface(respond)
|
| 43 |
+
with gr.Tab("Upload Source"):
|
| 44 |
+
gr.File(label="Upload PDF/JSON for reference", file_count="single")
|
| 45 |
+
|
| 46 |
+
demo.launch()
|