Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import json
|
|
|
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
import tempfile
|
|
@@ -15,13 +16,12 @@ from ddgs import DDGS
|
|
| 15 |
DEFAULT_API_URL = "https://hf.space"
|
| 16 |
MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
| 17 |
|
|
|
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
MODEL_ID,
|
| 21 |
torch_dtype=torch.bfloat16,
|
| 22 |
-
device_map="auto",
|
| 23 |
)
|
| 24 |
-
# Keep Whisper on CPU so it consumes 0 seconds of ZeroGPU quota
|
| 25 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cpu")
|
| 26 |
|
| 27 |
def download_file(task_id: str, file_name: str) -> str | None:
|
|
@@ -42,7 +42,7 @@ def download_file(task_id: str, file_name: str) -> str | None:
|
|
| 42 |
def read_attached_file(file_path: str) -> str:
|
| 43 |
if not file_path or not os.path.exists(file_path):
|
| 44 |
return ""
|
| 45 |
-
ext = os.path.splitext(file_path)
|
| 46 |
try:
|
| 47 |
if ext == ".py":
|
| 48 |
res = subprocess.run(["python3", file_path], capture_output=True, text=True, timeout=15)
|
|
@@ -87,26 +87,19 @@ def search_web_and_wiki(question: str) -> str:
|
|
| 87 |
pass
|
| 88 |
return "\n".join(snippets)
|
| 89 |
|
| 90 |
-
SYSTEM_PROMPT = """You are
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
-
|
| 95 |
-
- If
|
| 96 |
-
- If
|
| 97 |
-
- If a comma-separated list is requested, follow exact alphabetical or ascending order and spacing (e.g., FINAL_ANSWER: apple, banana).
|
| 98 |
"""
|
| 99 |
|
| 100 |
-
def extract_final_answer(raw_text: str) -> str:
|
| 101 |
-
match = re.search(r"FINAL_ANSWER:\s*(.+)", raw_text, re.IGNORECASE)
|
| 102 |
-
if match:
|
| 103 |
-
return match.group(1).strip().strip('"').strip("'")
|
| 104 |
-
lines = [line.strip() for line in raw_text.strip().splitlines() if line.strip()]
|
| 105 |
-
return lines[-1] if lines else raw_text.strip()
|
| 106 |
-
|
| 107 |
-
# Requests only 90s so it fits inside your remaining 190s free quota!
|
| 108 |
@spaces.GPU(duration=90)
|
| 109 |
def process_all_on_gpu(prompts: list[str]) -> list[str]:
|
|
|
|
|
|
|
| 110 |
answers = []
|
| 111 |
for user_prompt in prompts:
|
| 112 |
messages = [
|
|
@@ -117,11 +110,11 @@ def process_all_on_gpu(prompts: list[str]) -> list[str]:
|
|
| 117 |
inputs = tokenizer([text], return_tensors="pt").to("cuda")
|
| 118 |
|
| 119 |
with torch.no_grad():
|
| 120 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
| 121 |
|
| 122 |
-
generated_tokens = outputs[
|
| 123 |
-
|
| 124 |
-
answers.append(
|
| 125 |
|
| 126 |
return answers
|
| 127 |
|
|
@@ -150,7 +143,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 150 |
if not questions_data:
|
| 151 |
return "Error: Could not load questions.", None
|
| 152 |
|
| 153 |
-
# All file reading, Whisper transcription, and web/Wiki search run on CPU first
|
| 154 |
valid_items = []
|
| 155 |
prompts = []
|
| 156 |
for item in questions_data:
|
|
@@ -167,8 +159,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 167 |
|
| 168 |
try:
|
| 169 |
final_answers = process_all_on_gpu(prompts)
|
| 170 |
-
except Exception
|
| 171 |
-
return f"ZeroGPU execution failed:
|
| 172 |
|
| 173 |
results_log = []
|
| 174 |
answers_payload = []
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
+
import traceback
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
import tempfile
|
|
|
|
| 16 |
DEFAULT_API_URL = "https://hf.space"
|
| 17 |
MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
| 18 |
|
| 19 |
+
# Load on plain CPU at startup (NO device_map="auto" so ZeroGPU forks cleanly)
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
MODEL_ID,
|
| 23 |
torch_dtype=torch.bfloat16,
|
|
|
|
| 24 |
)
|
|
|
|
| 25 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cpu")
|
| 26 |
|
| 27 |
def download_file(task_id: str, file_name: str) -> str | None:
|
|
|
|
| 42 |
def read_attached_file(file_path: str) -> str:
|
| 43 |
if not file_path or not os.path.exists(file_path):
|
| 44 |
return ""
|
| 45 |
+
ext = os.path.splitext(file_path).lower()
|
| 46 |
try:
|
| 47 |
if ext == ".py":
|
| 48 |
res = subprocess.run(["python3", file_path], capture_output=True, text=True, timeout=15)
|
|
|
|
| 87 |
pass
|
| 88 |
return "\n".join(snippets)
|
| 89 |
|
| 90 |
+
SYSTEM_PROMPT = """You are a precise AI assistant solving GAIA benchmark questions.
|
| 91 |
+
CRITICAL OUTPUT RULES:
|
| 92 |
+
- Reply with ONLY the exact final answer and nothing else.
|
| 93 |
+
- Do NOT write 'FINAL ANSWER', explanations, full sentences, or trailing periods.
|
| 94 |
+
- If a number is requested, output only the digits (e.g., 42).
|
| 95 |
+
- If USD with two decimal places is requested, output e.g. 123.45.
|
| 96 |
+
- If a comma-separated list is requested, follow exact alphabetical or ascending order and spacing (e.g., apple, banana).
|
|
|
|
| 97 |
"""
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
@spaces.GPU(duration=90)
|
| 100 |
def process_all_on_gpu(prompts: list[str]) -> list[str]:
|
| 101 |
+
model.to("cuda")
|
| 102 |
+
model.eval()
|
| 103 |
answers = []
|
| 104 |
for user_prompt in prompts:
|
| 105 |
messages = [
|
|
|
|
| 110 |
inputs = tokenizer([text], return_tensors="pt").to("cuda")
|
| 111 |
|
| 112 |
with torch.no_grad():
|
| 113 |
+
outputs = model.generate(**inputs, max_new_tokens=64, do_sample=False)
|
| 114 |
|
| 115 |
+
generated_tokens = outputs[inputs.input_ids.shape:]
|
| 116 |
+
ans = tokenizer.decode(generated_tokens, skip_special_tokens=True).strip().strip('"').strip("'")
|
| 117 |
+
answers.append(ans)
|
| 118 |
|
| 119 |
return answers
|
| 120 |
|
|
|
|
| 143 |
if not questions_data:
|
| 144 |
return "Error: Could not load questions.", None
|
| 145 |
|
|
|
|
| 146 |
valid_items = []
|
| 147 |
prompts = []
|
| 148 |
for item in questions_data:
|
|
|
|
| 159 |
|
| 160 |
try:
|
| 161 |
final_answers = process_all_on_gpu(prompts)
|
| 162 |
+
except Exception:
|
| 163 |
+
return f"ZeroGPU execution failed:\n{traceback.format_exc()}", None
|
| 164 |
|
| 165 |
results_log = []
|
| 166 |
answers_payload = []
|