shubhamrooter commited on
Commit
2373c44
Β·
verified Β·
1 Parent(s): 3a4d669

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -122
app.py CHANGED
@@ -1,150 +1,149 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- import threading
4
- import time
5
 
6
  # Configuration
7
- MODEL_NAME = "trendmicro-ailab/Llama-Primus-Reasoning"
 
 
8
 
9
- class FastModelHandler:
10
- def __init__(self):
11
- self.pipe = None
12
- self.loaded = False
13
- self.loading = False
14
- self.status = "πŸ”„ Model loading..."
15
- # Auto-start loading
16
- self.start_loading()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- def start_loading(self):
19
- """Background mein model load kare"""
20
- def load_in_background():
21
- self.loading = True
22
- self.status = "πŸ”„ Model loading..."
23
- try:
24
- print("πŸš€ Model loading started...")
25
- # Simple pipeline - fastest approach
26
- self.pipe = pipeline(
27
- "text-generation",
28
- model=MODEL_NAME,
29
- device_map="auto"
30
- )
31
- self.loaded = True
32
- self.loading = False
33
- self.status = "βœ… Model Ready! Prompt likho aur generate karo"
34
- print("βœ… Model loaded successfully!")
35
- except Exception as e:
36
- print(f"❌ GPU Error: {e}")
37
- # Fallback to CPU
38
- try:
39
- self.pipe = pipeline(
40
- "text-generation",
41
- model=MODEL_NAME,
42
- device="cpu"
43
- )
44
- self.loaded = True
45
- self.loading = False
46
- self.status = "βœ… Model loaded on CPU!"
47
- print("βœ… Model loaded on CPU!")
48
- except Exception as e2:
49
- print(f"❌ CPU loading failed: {e2}")
50
- self.status = "❌ Model loading failed"
51
-
52
- thread = threading.Thread(target=load_in_background, daemon=True)
53
- thread.start()
54
 
55
- def get_status(self):
56
- """Current status return kare"""
57
- return self.status
58
 
59
- def generate_response(self, prompt, max_length=150):
60
- """Fast response generate kare"""
61
- if not self.loaded:
62
- return f"{self.status}\n\nThoda wait karein..."
63
-
64
- try:
65
- start_time = time.time()
66
-
67
- result = self.pipe(
68
- prompt,
69
- max_length=max_length,
70
- temperature=0.7,
71
- do_sample=True,
72
- num_return_sequences=1,
73
- pad_token_id=50256
74
- )
75
-
76
- response = result[0]['generated_text']
77
- time_taken = time.time() - start_time
78
-
79
- return f"{response}\n\n⏱️ Time: {time_taken:.1f}s"
80
-
81
- except Exception as e:
82
- return f"❌ Error: {str(e)}"
83
-
84
- # Auto initialize
85
- model = FastModelHandler()
86
 
87
- # Simple UI - No complex setup
88
- with gr.Blocks(title="Fast Reasoning AI") as demo:
89
  gr.Markdown("""
90
- # ⚑ Fast Reasoning Model
91
- **Model automatically load ho raha hai!**
92
 
93
- πŸ‘‡ **Bas prompt likho β†’ Generate dabao β†’ Response mil jayega!**
94
  """)
95
 
96
- # Status display
97
- status_text = gr.Textbox(
98
- label="Status",
99
- value=model.get_status(),
100
- interactive=False
101
- )
102
-
103
  with gr.Row():
104
- with gr.Column():
105
- prompt_box = gr.Textbox(
106
- label="Your Question",
107
- placeholder="Yahan apna sawal likho...",
108
- lines=3
 
 
 
 
109
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- with gr.Row():
112
- length_control = gr.Slider(50, 250, value=150, label="Response Length")
113
- generate_btn = gr.Button("πŸš€ Generate", variant="primary")
114
-
115
- with gr.Column():
116
- response_box = gr.Textbox(
117
- label="AI Response",
118
- lines=6,
 
 
 
 
 
119
  show_copy_button=True
120
  )
121
 
122
- # Quick examples
123
- gr.Examples(
124
  examples=[
125
- ["15 + 27 Γ— 3 kaise solve karein?"],
126
- ["Socrates insaan hai, sab insaan mortal hain. Socrates?"],
127
- ["Train 60 mph se 2 hours chale to kitna distance?"],
128
- ["Simple reasoning example batayein"]
 
129
  ],
130
- inputs=prompt_box,
131
- label="Quick Examples - Click karo"
 
 
132
  )
133
 
134
- # Refresh button for status
135
- refresh_btn = gr.Button("πŸ”„ Refresh Status", variant="secondary")
136
-
137
- # Events
138
  generate_btn.click(
139
- model.generate_response,
140
- inputs=[prompt_box, length_control],
141
- outputs=response_box
142
  )
143
 
144
- refresh_btn.click(
145
- model.get_status,
146
- outputs=status_text
 
 
147
  )
148
 
149
  if __name__ == "__main__":
150
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import os
 
4
 
5
  # Configuration
6
+ MODEL_REPO = "AlicanKiraz0/Cybersecurity-BaronLLM_Offensive_Security_LLM_Q6_K_GGUF"
7
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_REPO}"
8
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
9
 
10
+ headers = {
11
+ "Authorization": f"Bearer {HF_TOKEN}",
12
+ "Content-Type": "application/json"
13
+ }
14
+
15
+ def query_model(payload):
16
+ """
17
+ Query the model using Hugging Face Inference API
18
+ """
19
+ try:
20
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
21
+ response.raise_for_status()
22
+ return response.json()
23
+ except requests.exceptions.RequestException as e:
24
+ return {"error": f"API request failed: {str(e)}"}
25
+ except Exception as e:
26
+ return {"error": f"Unexpected error: {str(e)}"}
27
+
28
+ def generate_response(prompt, max_tokens=150, temperature=0.7):
29
+ """
30
+ Generate response using the model
31
+ """
32
+ if not prompt.strip():
33
+ return "Please enter a prompt."
34
 
35
+ payload = {
36
+ "inputs": prompt,
37
+ "parameters": {
38
+ "max_new_tokens": max_tokens,
39
+ "temperature": temperature,
40
+ "top_p": 0.9,
41
+ "do_sample": True,
42
+ "return_full_text": False
43
+ }
44
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ result = query_model(payload)
 
 
47
 
48
+ if "error" in result:
49
+ error_msg = result["error"]
50
+ if "loading" in error_msg.lower():
51
+ return f"Model is currently loading. Please wait a moment and try again.\n\nError details: {error_msg}"
52
+ return f"Error: {error_msg}"
53
+
54
+ if isinstance(result, list) and len(result) > 0:
55
+ if "generated_text" in result[0]:
56
+ return result[0]["generated_text"]
57
+ elif "text" in result[0]:
58
+ return result[0]["text"]
59
+
60
+ return "No response generated. Please try again."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ # Create Gradio interface
63
+ with gr.Blocks(title="Cybersecurity BaronLLM", theme=gr.themes.Soft()) as demo:
64
  gr.Markdown("""
65
+ # πŸ”’ Cybersecurity BaronLLM
66
+ **Offensive Security Language Model**
67
 
68
+ This interface uses the Cybersecurity BaronLLM model via Hugging Face Inference API.
69
  """)
70
 
 
 
 
 
 
 
 
71
  with gr.Row():
72
+ with gr.Column(scale=1):
73
+ gr.Markdown("### Configuration")
74
+ max_tokens = gr.Slider(
75
+ minimum=32,
76
+ maximum=512,
77
+ value=150,
78
+ step=32,
79
+ label="Max Tokens",
80
+ info="Maximum length of response"
81
  )
82
+ temperature = gr.Slider(
83
+ minimum=0.1,
84
+ maximum=1.0,
85
+ value=0.7,
86
+ step=0.1,
87
+ label="Temperature",
88
+ info="Higher values = more creative, lower values = more focused"
89
+ )
90
+
91
+ gr.Markdown("""
92
+ ### Example Prompts
93
+ - Explain SQL injection techniques
94
+ - What are common penetration testing methodologies?
95
+ - How to detect XSS attacks?
96
+ - Describe network security principles
97
+ """)
98
 
99
+ with gr.Column(scale=2):
100
+ prompt = gr.Textbox(
101
+ label="Enter your cybersecurity question or prompt:",
102
+ placeholder="Explain SQL injection techniques and prevention methods...",
103
+ lines=5,
104
+ max_lines=10
105
+ )
106
+
107
+ generate_btn = gr.Button("πŸ”’ Generate Response", variant="primary", size="lg")
108
+
109
+ output = gr.Textbox(
110
+ label="Model Response",
111
+ lines=8,
112
  show_copy_button=True
113
  )
114
 
115
+ # Examples
116
+ examples = gr.Examples(
117
  examples=[
118
+ ["What are the most common web application vulnerabilities and how can they be exploited?"],
119
+ ["Explain the difference between white hat, black hat, and gray hat hackers."],
120
+ ["Describe the steps involved in a penetration testing engagement."],
121
+ ["How does a buffer overflow attack work and what are modern defenses against it?"],
122
+ ["What are the key components of a cybersecurity risk assessment?"]
123
  ],
124
+ inputs=prompt,
125
+ outputs=output,
126
+ fn=generate_response,
127
+ cache_examples=False
128
  )
129
 
130
+ # Event handlers
 
 
 
131
  generate_btn.click(
132
+ fn=generate_response,
133
+ inputs=[prompt, max_tokens, temperature],
134
+ outputs=output
135
  )
136
 
137
+ # Also generate on Enter key
138
+ prompt.submit(
139
+ fn=generate_response,
140
+ inputs=[prompt, max_tokens, temperature],
141
+ outputs=output
142
  )
143
 
144
  if __name__ == "__main__":
145
+ demo.launch(
146
+ server_name="0.0.0.0",
147
+ server_port=7860,
148
+ share=False
149
+ )