File size: 1,279 Bytes
f20a9c0
 
 
b9540d8
 
 
 
 
 
f20a9c0
 
b9540d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f20a9c0
b9540d8
 
f20a9c0
b9540d8
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
48
import gradio as gr
from llama_cpp import Llama

try:
    # Load your model
    llm = Llama(model_path="model.Q4_K_M.gguf", n_gpu_layers=0)
except Exception as e:
    print(f"Error loading model: {e}")
    llm = None

def chat(message, history):
    """Chat function with history"""
    if llm is None:
        return "Error: Model not loaded. Make sure model.Q4_K_M.gguf is uploaded."
    
    try:
        # Build conversation history
        chat_history = ""
        for user_msg, bot_msg in history:
            chat_history += f"Q: {user_msg}\nA: {bot_msg}\n"
        
        # Create prompt
        prompt = chat_history + f"Q: {message}\nA:"
        
        # Get response
        response = llm(prompt, max_tokens=512)
        return response["choices"]["text"].strip()
    except Exception as e:
        return f"Error: {str(e)}"

# Launch Gradio interface
demo = gr.ChatInterface(
    chat,
    examples=[
        "What is SQL injection?",
        "How to prevent XSS attacks?",
        "What is CSRF?",
        "Best security practices"
    ],
    title="🔒 Security Expert - Llama-3",
    description="Ask me anything about cybersecurity!",
    theme=gr.themes.Soft(),
    chatbot=gr.Chatbot(label="Chat History")
)

if __name__ == "__main__":
    demo.launch()