Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,65 @@
|
|
| 1 |
-
# app.py -
|
| 2 |
import gradio as gr
|
| 3 |
import subprocess
|
| 4 |
-
import
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
def generate(prompt):
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
script = f"""
|
| 11 |
import torch
|
| 12 |
-
from transformers import
|
| 13 |
|
| 14 |
-
model
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
torch_dtype=torch.float16,
|
| 17 |
-
device_map="auto",
|
| 18 |
trust_remote_code=True
|
| 19 |
)
|
| 20 |
-
tokenizer = AutoTokenizer.from_pretrained("fdtn-ai/Foundation-Sec-8B")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
with
|
| 29 |
-
f.write(
|
| 30 |
-
script_path = f.name
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
# Run
|
| 34 |
result = subprocess.run(
|
| 35 |
-
[
|
| 36 |
capture_output=True,
|
| 37 |
text=True,
|
| 38 |
-
timeout=
|
| 39 |
)
|
| 40 |
|
| 41 |
-
# Cleanup
|
| 42 |
-
os.unlink(script_path)
|
| 43 |
-
|
| 44 |
if result.returncode == 0:
|
| 45 |
-
return result.stdout.strip()
|
| 46 |
else:
|
| 47 |
return f"Error: {result.stderr}"
|
| 48 |
|
| 49 |
except subprocess.TimeoutExpired:
|
| 50 |
-
return "Timeout - Model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
generate,
|
| 55 |
-
gr.Textbox(label="Ask about cybersecurity:"),
|
| 56 |
-
gr.Textbox(label="Response", lines=10),
|
| 57 |
-
title="Foundation-Sec-8B (On-demand Loading)"
|
| 58 |
-
).launch(server_name="0.0.0.0")
|
|
|
|
| 1 |
+
# app.py - SIMPLEST WORKING VERSION
|
| 2 |
import gradio as gr
|
| 3 |
import subprocess
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
print("🚀 Starting Foundation-Sec-8B Space...")
|
| 7 |
|
| 8 |
def generate(prompt):
|
| 9 |
+
"""Generate text using transformers CLI (avoids loading issues)"""
|
| 10 |
+
python_code = f"""
|
|
|
|
| 11 |
import torch
|
| 12 |
+
from transformers import pipeline
|
| 13 |
|
| 14 |
+
print("Loading model...")
|
| 15 |
+
model = pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model="fdtn-ai/Foundation-Sec-8B",
|
| 18 |
+
device=0,
|
| 19 |
torch_dtype=torch.float16,
|
|
|
|
| 20 |
trust_remote_code=True
|
| 21 |
)
|
|
|
|
| 22 |
|
| 23 |
+
result = model("{prompt}", max_new_tokens=200, temperature=0.7)
|
| 24 |
+
print(result[0]['generated_text'])
|
|
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
+
# Save to temp file
|
| 28 |
+
with open("/tmp/generate.py", "w") as f:
|
| 29 |
+
f.write(python_code)
|
|
|
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
# Run in subprocess
|
| 33 |
result = subprocess.run(
|
| 34 |
+
["python", "/tmp/generate.py"],
|
| 35 |
capture_output=True,
|
| 36 |
text=True,
|
| 37 |
+
timeout=180
|
| 38 |
)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
if result.returncode == 0:
|
| 41 |
+
return result.stdout.strip().split('\n')[-1] # Get last line
|
| 42 |
else:
|
| 43 |
return f"Error: {result.stderr}"
|
| 44 |
|
| 45 |
except subprocess.TimeoutExpired:
|
| 46 |
+
return "Timeout - Model taking too long to load"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Exception: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# Create interface
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=generate,
|
| 53 |
+
inputs=gr.Textbox(label="Ask about cybersecurity:", lines=3),
|
| 54 |
+
outputs=gr.Textbox(label="Response", lines=10),
|
| 55 |
+
title="🔒 Foundation-Sec-8B",
|
| 56 |
+
description="Cybersecurity AI Assistant",
|
| 57 |
+
examples=[
|
| 58 |
+
["What is a firewall?"],
|
| 59 |
+
["Explain encryption:"],
|
| 60 |
+
["How to create strong passwords?"]
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch(server_name="0.0.0.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|