Mr-HASSAN commited on
Commit
414db61
·
verified ·
1 Parent(s): 7278ce3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -1,58 +1,65 @@
1
- # app.py - LOAD ON DEMAND
2
  import gradio as gr
3
  import subprocess
4
- import tempfile
5
- import os
 
6
 
7
  def generate(prompt):
8
- """Load model on-demand using transformers CLI"""
9
- # Create a temporary script
10
- script = f"""
11
  import torch
12
- from transformers import AutoModelForCausalLM, AutoTokenizer
13
 
14
- model = AutoModelForCausalLM.from_pretrained(
15
- "fdtn-ai/Foundation-Sec-8B",
 
 
 
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
- inputs = tokenizer('{prompt}', return_tensors="pt").to(model.device)
23
- outputs = model.generate(**inputs, max_new_tokens=200)
24
- print(tokenizer.decode(outputs[0], skip_special_tokens=True))
25
  """
26
 
27
- # Write to temp file
28
- with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
29
- f.write(script)
30
- script_path = f.name
31
 
32
  try:
33
- # Run script
34
  result = subprocess.run(
35
- ['python', script_path],
36
  capture_output=True,
37
  text=True,
38
- timeout=120
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 loading took too long"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # Launch interface
53
- gr.Interface(
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")