Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,43 +4,52 @@ from transformers import AutoProcessor, AutoModelForCausalLM
|
|
| 4 |
hf_token = os.getenv("HF_TOKEN")
|
| 5 |
model_id = "google/functiongemma-270m-it"
|
| 6 |
|
|
|
|
| 7 |
processor = AutoProcessor.from_pretrained(model_id, token=hf_token)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
model_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
def process_request(user_prompt, tools_json):
|
| 13 |
try:
|
| 14 |
-
# 1. Parse the tools sent from the client
|
| 15 |
tools = json.loads(tools_json) if tools_json.strip() else []
|
| 16 |
-
|
| 17 |
-
# 2. Build the message history
|
| 18 |
messages = [
|
| 19 |
{"role": "developer", "content": "You are a model that can do function calling with the following functions"},
|
| 20 |
{"role": "user", "content": user_prompt}
|
| 21 |
]
|
| 22 |
-
|
| 23 |
-
# 3. Apply template with DYNAMIC tools
|
| 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 |
-
# 4. Generate
|
| 30 |
with torch.no_grad():
|
| 31 |
outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
|
| 32 |
|
| 33 |
-
# 5. Decode just the new part
|
| 34 |
input_len = inputs.input_ids.shape[1]
|
| 35 |
return processor.decode(outputs[0][input_len:], skip_special_tokens=True)
|
| 36 |
-
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error: {str(e)}"
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|