Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,76 @@
|
|
| 1 |
-
# app.py -
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
torch_dtype=torch.float16,
|
|
|
|
| 14 |
trust_remote_code=True
|
| 15 |
)
|
| 16 |
|
| 17 |
print("✅ Model loaded!")
|
| 18 |
|
| 19 |
def generate(prompt, max_tokens=200):
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
max_new_tokens=max_tokens,
|
| 23 |
temperature=0.7,
|
| 24 |
do_sample=True
|
| 25 |
)
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
gr.Interface(
|
| 30 |
generate,
|
| 31 |
[
|
|
@@ -39,4 +84,4 @@ gr.Interface(
|
|
| 39 |
["What is a firewall?"],
|
| 40 |
["How to create strong passwords?"]
|
| 41 |
]
|
| 42 |
-
).launch()
|
|
|
|
| 1 |
+
# app.py - FIXED VERSION
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
MODEL_ID = "fdtn-ai/Foundation-Sec-8B"
|
| 9 |
|
| 10 |
+
print("🚀 Loading model...")
|
| 11 |
+
|
| 12 |
+
# FIX: Download and patch config first
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
+
|
| 15 |
+
# Download config
|
| 16 |
+
config_path = hf_hub_download(
|
| 17 |
+
repo_id=MODEL_ID,
|
| 18 |
+
filename="config.json",
|
| 19 |
+
local_dir="./cache"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Read and fix config
|
| 23 |
+
with open(config_path, 'r') as f:
|
| 24 |
+
config_data = json.load(f)
|
| 25 |
+
|
| 26 |
+
# Fix rope_scaling for Llama 3
|
| 27 |
+
if 'rope_scaling' in config_data:
|
| 28 |
+
rope = config_data['rope_scaling']
|
| 29 |
+
if isinstance(rope, dict):
|
| 30 |
+
# Convert to standard format
|
| 31 |
+
rope_scaling = {
|
| 32 |
+
"type": rope.get("rope_type", "linear"),
|
| 33 |
+
"factor": rope.get("factor", 1.0)
|
| 34 |
+
}
|
| 35 |
+
config_data['rope_scaling'] = rope_scaling
|
| 36 |
+
|
| 37 |
+
# Save fixed config
|
| 38 |
+
os.makedirs("./fixed_config", exist_ok=True)
|
| 39 |
+
fixed_config_path = "./fixed_config/config.json"
|
| 40 |
+
with open(fixed_config_path, 'w') as f:
|
| 41 |
+
json.dump(config_data, f)
|
| 42 |
+
|
| 43 |
+
# Load with fixed config
|
| 44 |
+
from transformers import AutoConfig
|
| 45 |
+
config = AutoConfig.from_pretrained(fixed_config_path)
|
| 46 |
+
|
| 47 |
+
# Load tokenizer
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 49 |
+
|
| 50 |
+
# Load model
|
| 51 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 52 |
+
MODEL_ID,
|
| 53 |
+
config=config,
|
| 54 |
torch_dtype=torch.float16,
|
| 55 |
+
device_map="auto",
|
| 56 |
trust_remote_code=True
|
| 57 |
)
|
| 58 |
|
| 59 |
print("✅ Model loaded!")
|
| 60 |
|
| 61 |
def generate(prompt, max_tokens=200):
|
| 62 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 63 |
+
|
| 64 |
+
outputs = model.generate(
|
| 65 |
+
**inputs,
|
| 66 |
max_new_tokens=max_tokens,
|
| 67 |
temperature=0.7,
|
| 68 |
do_sample=True
|
| 69 |
)
|
| 70 |
+
|
| 71 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 72 |
|
| 73 |
+
# Create interface
|
| 74 |
gr.Interface(
|
| 75 |
generate,
|
| 76 |
[
|
|
|
|
| 84 |
["What is a firewall?"],
|
| 85 |
["How to create strong passwords?"]
|
| 86 |
]
|
| 87 |
+
).launch(server_name="0.0.0.0")
|