cyborgAipro commited on
Commit
b9540d8
·
verified ·
1 Parent(s): 57d2b98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -12
app.py CHANGED
@@ -1,19 +1,47 @@
1
  import gradio as gr
2
  from llama_cpp import Llama
3
 
4
- # Load your model
5
- llm = Llama(model_path="model.Q4_K_M.gguf", n_gpu_layers=0)
 
 
 
 
6
 
7
  def chat(message, history):
8
- """Chat function"""
9
- h = "".join([f"Q: {h}\nA: {h}\n" for h in history])
10
- r = llm(h + f"Q: {message}\nA:", max_tokens=512)
11
- return r["choices"]["text"].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Create chat interface
14
- gr.ChatInterface(
15
  chat,
16
- examples=["What is SQL injection?", "How to prevent XSS?"],
17
- title="🔒 Security Expert Llama-3",
18
- description="Ask about cybersecurity!",
19
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
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()