Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def chat(message, history):
|
| 8 |
-
"""Chat function"""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
gr.ChatInterface(
|
| 15 |
chat,
|
| 16 |
-
examples=[
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
+
try:
|
| 5 |
+
# Load your model
|
| 6 |
+
llm = Llama(model_path="model.Q4_K_M.gguf", n_gpu_layers=0)
|
| 7 |
+
except Exception as e:
|
| 8 |
+
print(f"Error loading model: {e}")
|
| 9 |
+
llm = None
|
| 10 |
|
| 11 |
def chat(message, history):
|
| 12 |
+
"""Chat function with history"""
|
| 13 |
+
if llm is None:
|
| 14 |
+
return "Error: Model not loaded. Make sure model.Q4_K_M.gguf is uploaded."
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Build conversation history
|
| 18 |
+
chat_history = ""
|
| 19 |
+
for user_msg, bot_msg in history:
|
| 20 |
+
chat_history += f"Q: {user_msg}\nA: {bot_msg}\n"
|
| 21 |
+
|
| 22 |
+
# Create prompt
|
| 23 |
+
prompt = chat_history + f"Q: {message}\nA:"
|
| 24 |
+
|
| 25 |
+
# Get response
|
| 26 |
+
response = llm(prompt, max_tokens=512)
|
| 27 |
+
return response["choices"]["text"].strip()
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error: {str(e)}"
|
| 30 |
|
| 31 |
+
# Launch Gradio interface
|
| 32 |
+
demo = gr.ChatInterface(
|
| 33 |
chat,
|
| 34 |
+
examples=[
|
| 35 |
+
"What is SQL injection?",
|
| 36 |
+
"How to prevent XSS attacks?",
|
| 37 |
+
"What is CSRF?",
|
| 38 |
+
"Best security practices"
|
| 39 |
+
],
|
| 40 |
+
title="🔒 Security Expert - Llama-3",
|
| 41 |
+
description="Ask me anything about cybersecurity!",
|
| 42 |
+
theme=gr.themes.Soft(),
|
| 43 |
+
chatbot=gr.Chatbot(label="Chat History")
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|