Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,52 +4,46 @@ from transformers import AutoProcessor, AutoModelForCausalLM
|
|
| 4 |
hf_token = os.getenv("HF_TOKEN")
|
| 5 |
model_id = "google/functiongemma-270m-it"
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
processor = AutoProcessor.from_pretrained(model_id, token=hf_token)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
-
torch_dtype=torch.
|
| 12 |
-
device_map="
|
| 13 |
-
token=hf_token
|
| 14 |
-
low_cpu_mem_usage=True
|
| 15 |
)
|
| 16 |
|
| 17 |
def process_request(user_prompt, tools_json):
|
| 18 |
try:
|
| 19 |
tools = json.loads(tools_json) if tools_json.strip() else []
|
|
|
|
|
|
|
| 20 |
messages = [
|
| 21 |
{"role": "developer", "content": "You are a model that can do function calling with the following functions"},
|
| 22 |
{"role": "user", "content": user_prompt}
|
| 23 |
]
|
|
|
|
|
|
|
| 24 |
inputs = processor.apply_chat_template(
|
| 25 |
messages, tools=tools, add_generation_prompt=True,
|
| 26 |
return_dict=True, return_tensors="pt"
|
| 27 |
-
).to(
|
| 28 |
|
| 29 |
with torch.no_grad():
|
| 30 |
outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
|
| 31 |
|
| 32 |
input_len = inputs.input_ids.shape[1]
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
gr.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
output_text = gr.Code(label="Model Output")
|
| 44 |
-
|
| 45 |
-
submit_btn = gr.Button("Submit")
|
| 46 |
-
|
| 47 |
-
# CRITICAL: This 'api_name' must match what your client expects
|
| 48 |
-
submit_btn.click(
|
| 49 |
-
fn=process_request,
|
| 50 |
-
inputs=[prompt_input, tools_input],
|
| 51 |
-
outputs=output_text,
|
| 52 |
-
api_name="predict"
|
| 53 |
-
)
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 4 |
hf_token = os.getenv("HF_TOKEN")
|
| 5 |
model_id = "google/functiongemma-270m-it"
|
| 6 |
|
| 7 |
+
# 1. Load for CPU specifically
|
| 8 |
processor = AutoProcessor.from_pretrained(model_id, token=hf_token)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
+
torch_dtype=torch.float32, # CPU prefers float32
|
| 12 |
+
device_map={"": "cpu"}, # Forces everything onto CPU, avoiding "meta device"
|
| 13 |
+
token=hf_token
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
def process_request(user_prompt, tools_json):
|
| 17 |
try:
|
| 18 |
tools = json.loads(tools_json) if tools_json.strip() else []
|
| 19 |
+
|
| 20 |
+
# FunctionGemma format
|
| 21 |
messages = [
|
| 22 |
{"role": "developer", "content": "You are a model that can do function calling with the following functions"},
|
| 23 |
{"role": "user", "content": user_prompt}
|
| 24 |
]
|
| 25 |
+
|
| 26 |
+
# 2. Ensure inputs are on CPU
|
| 27 |
inputs = processor.apply_chat_template(
|
| 28 |
messages, tools=tools, add_generation_prompt=True,
|
| 29 |
return_dict=True, return_tensors="pt"
|
| 30 |
+
).to("cpu")
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
|
| 34 |
|
| 35 |
input_len = inputs.input_ids.shape[1]
|
| 36 |
+
decoded = processor.decode(outputs[0][input_len:], skip_special_tokens=True)
|
| 37 |
+
return decoded if decoded.strip() else "Model returned an empty string."
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error: {str(e)}"
|
| 41 |
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=process_request,
|
| 44 |
+
inputs=[gr.Textbox(label="User Prompt"), gr.Textbox(label="Tools (JSON Array)")],
|
| 45 |
+
outputs=gr.Code(label="Model Output"),
|
| 46 |
+
title="FunctionGemma CPU Fixed"
|
| 47 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
demo.launch()
|