jabalarami commited on
Commit
efefc6e
·
verified ·
1 Parent(s): 293b192

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
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 model
8
  processor = AutoProcessor.from_pretrained(model_id, token=hf_token)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_id,
11
- torch_dtype=torch.float16,
12
- device_map="auto",
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(model.device)
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
- return processor.decode(outputs[0][input_len:], skip_special_tokens=True)
 
 
34
  except Exception as e:
35
  return f"Error: {str(e)}"
36
 
37
- # Create the UI using Blocks for better API support
38
- with gr.Blocks() as demo:
39
- gr.Markdown("# FunctionGemma API Server")
40
- with gr.Row():
41
- prompt_input = gr.Textbox(label="User Prompt")
42
- tools_input = gr.Textbox(label="Tools (JSON Array)")
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()