Update app.py
Browse files
app.py
CHANGED
|
@@ -1,150 +1,149 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import time
|
| 5 |
|
| 6 |
# Configuration
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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 |
-
|
| 56 |
-
"""Current status return kare"""
|
| 57 |
-
return self.status
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if
|
| 62 |
-
return f"
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
result
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 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 |
-
#
|
| 88 |
-
with gr.Blocks(title="
|
| 89 |
gr.Markdown("""
|
| 90 |
-
#
|
| 91 |
-
**
|
| 92 |
|
| 93 |
-
|
| 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 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
show_copy_button=True
|
| 120 |
)
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
gr.Examples(
|
| 124 |
examples=[
|
| 125 |
-
["
|
| 126 |
-
["
|
| 127 |
-
["
|
| 128 |
-
["
|
|
|
|
| 129 |
],
|
| 130 |
-
inputs=
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
refresh_btn = gr.Button("π Refresh Status", variant="secondary")
|
| 136 |
-
|
| 137 |
-
# Events
|
| 138 |
generate_btn.click(
|
| 139 |
-
|
| 140 |
-
inputs=[
|
| 141 |
-
outputs=
|
| 142 |
)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|