Spaces:
Sleeping
Sleeping
Switch Analyse to free ZeroGPU local Qwen (no Inference Providers)
Browse files- README.md +3 -0
- app.py +38 -67
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -8,6 +8,9 @@ sdk_version: 6.13.0
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
license: other
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
# ⚡ The Metabolic Phoenix Framework
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
license: other
|
| 11 |
+
suggested_hardware: zero-a10g
|
| 12 |
+
tags:
|
| 13 |
+
- zerogpu
|
| 14 |
---
|
| 15 |
|
| 16 |
# ⚡ The Metabolic Phoenix Framework
|
app.py
CHANGED
|
@@ -19,84 +19,55 @@ from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
| 19 |
SAVE_DIR = Path("/data/saved_docs")
|
| 20 |
SAVE_DIR.mkdir(parents=True, exist_ok=True)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
|
| 25 |
|
| 26 |
# =========================
|
| 27 |
-
# SHARED:
|
| 28 |
# =========================
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
user_token,
|
| 34 |
-
os.environ.get("HF_TOKEN"),
|
| 35 |
-
os.environ.get("HUGGINGFACE_HUB_TOKEN"),
|
| 36 |
-
os.environ.get("HUGGING_FACE_HUB_TOKEN"),
|
| 37 |
-
]
|
| 38 |
-
for raw in candidates:
|
| 39 |
-
if not raw:
|
| 40 |
-
continue
|
| 41 |
-
token = str(raw).strip().strip('"').strip("'")
|
| 42 |
-
if token:
|
| 43 |
-
return token
|
| 44 |
-
return ""
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
| 47 |
def call_llama(prompt, user_token=None, max_tokens=1200):
|
| 48 |
"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
"""
|
| 53 |
-
token = _resolve_hf_token(user_token)
|
| 54 |
-
|
| 55 |
-
if not token:
|
| 56 |
-
return (
|
| 57 |
-
"Model error: HF_TOKEN missing. Create a Hugging Face access token at "
|
| 58 |
-
"https://huggingface.co/settings/tokens (needs Inference permission), "
|
| 59 |
-
"then add it as a Space secret named HF_TOKEN."
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# Do not hard-fail on prefix: classic tokens start with hf_, but some valid
|
| 63 |
-
# Hub credentials do not. Let the Router reject bad credentials instead.
|
| 64 |
-
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
if response.status_code != 200:
|
| 88 |
-
return f"Model error: Hugging Face Router returned {response.status_code}: {response.text[:1200]}"
|
| 89 |
-
|
| 90 |
-
data = response.json()
|
| 91 |
-
|
| 92 |
-
if "choices" in data and len(data["choices"]) > 0:
|
| 93 |
-
return data["choices"][0]["message"]["content"]
|
| 94 |
-
|
| 95 |
-
if "error" in data:
|
| 96 |
-
return f"Model error: {data['error']}"
|
| 97 |
-
|
| 98 |
-
return str(data)
|
| 99 |
-
|
| 100 |
except Exception as e:
|
| 101 |
return f"Model error: {e}"
|
| 102 |
|
|
|
|
| 19 |
SAVE_DIR = Path("/data/saved_docs")
|
| 20 |
SAVE_DIR.mkdir(parents=True, exist_ok=True)
|
| 21 |
|
| 22 |
+
# Local instruct model on ZeroGPU — no paid Inference Providers
|
| 23 |
+
LOCAL_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 24 |
|
| 25 |
|
| 26 |
# =========================
|
| 27 |
+
# SHARED: LOCAL MODEL / TTS
|
| 28 |
# =========================
|
| 29 |
|
| 30 |
+
import spaces
|
| 31 |
+
import torch
|
| 32 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
_tokenizer = AutoTokenizer.from_pretrained(LOCAL_MODEL_ID, trust_remote_code=True)
|
| 35 |
+
_model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
+
LOCAL_MODEL_ID,
|
| 37 |
+
torch_dtype=torch.bfloat16,
|
| 38 |
+
device_map="cuda",
|
| 39 |
+
trust_remote_code=True,
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
|
| 43 |
+
@spaces.GPU(duration=120)
|
| 44 |
def call_llama(prompt, user_token=None, max_tokens=1200):
|
| 45 |
"""
|
| 46 |
+
Runs Qwen2.5-1.5B on the Space's free ZeroGPU slice.
|
| 47 |
+
Does not call Hugging Face Inference Providers (no extra credit spend).
|
| 48 |
+
user_token is ignored and kept only for call-site compatibility.
|
| 49 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
+
messages = [{"role": "user", "content": str(prompt)[:12000]}]
|
| 52 |
+
chat = _tokenizer.apply_chat_template(
|
| 53 |
+
messages,
|
| 54 |
+
tokenize=False,
|
| 55 |
+
add_generation_prompt=True,
|
| 56 |
+
)
|
| 57 |
+
inputs = _tokenizer(chat, return_tensors="pt", truncation=True, max_length=4096)
|
| 58 |
+
inputs = {k: v.to(_model.device) for k, v in inputs.items()}
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
out = _model.generate(
|
| 61 |
+
**inputs,
|
| 62 |
+
max_new_tokens=min(int(max_tokens or 1200), 1024),
|
| 63 |
+
do_sample=True,
|
| 64 |
+
temperature=0.4,
|
| 65 |
+
top_p=0.9,
|
| 66 |
+
pad_token_id=_tokenizer.eos_token_id,
|
| 67 |
+
)
|
| 68 |
+
gen = out[0][inputs["input_ids"].shape[-1]:]
|
| 69 |
+
text = _tokenizer.decode(gen, skip_special_tokens=True).strip()
|
| 70 |
+
return text or "[Empty model response]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"Model error: {e}"
|
| 73 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
requests
|
| 3 |
gTTS
|
| 4 |
pypdf
|
|
|
|
| 1 |
gradio
|
| 2 |
+
spaces
|
| 3 |
+
torch
|
| 4 |
+
transformers>=4.45.0
|
| 5 |
+
accelerate
|
| 6 |
+
sentencepiece
|
| 7 |
+
einops
|
| 8 |
requests
|
| 9 |
gTTS
|
| 10 |
pypdf
|