Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 7 |
# Load model and tokenizer
|
| 8 |
model_name = "Salesforce/xLAM-1b-fc-r"
|
| 9 |
|
| 10 |
-
title = f"Eval Model: {model_name}"
|
| 11 |
-
description = """"""
|
| 12 |
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -16,120 +15,33 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
| 16 |
# Set random seed for reproducibility
|
| 17 |
torch.random.manual_seed(0)
|
| 18 |
|
| 19 |
-
# Task and format instructions
|
| 20 |
-
task_instruction = """
|
| 21 |
-
Based on the previous context and API request history, generate an API request or a response as an AI assistant.""".strip()
|
| 22 |
-
|
| 23 |
-
format_instruction = """
|
| 24 |
-
The output should be of the JSON format, which specifies a list of generated function calls. The example format is as follows, please make sure the parameter type is correct. If no function call is needed, please make
|
| 25 |
-
tool_calls an empty list "[]".
|
| 26 |
-
```
|
| 27 |
-
{"thought": "the thought process, or an empty string", "tool_calls": [{"name": "api_name1", "arguments": {"argument1": "value1", "argument2": "value2"}}]}
|
| 28 |
-
```
|
| 29 |
-
""".strip()
|
| 30 |
-
|
| 31 |
-
# Example tools and query
|
| 32 |
-
example_tools = json.dumps([
|
| 33 |
-
{
|
| 34 |
-
"name": "get_weather",
|
| 35 |
-
"description": "Get the current weather for a location",
|
| 36 |
-
"parameters": {
|
| 37 |
-
"type": "object",
|
| 38 |
-
"properties": {
|
| 39 |
-
"location": {
|
| 40 |
-
"type": "string",
|
| 41 |
-
"description": "The city and state, e.g. San Francisco, New York"
|
| 42 |
-
},
|
| 43 |
-
"unit": {
|
| 44 |
-
"type": "string",
|
| 45 |
-
"enum": ["celsius", "fahrenheit"],
|
| 46 |
-
"description": "The unit of temperature to return"
|
| 47 |
-
}
|
| 48 |
-
},
|
| 49 |
-
"required": ["location"]
|
| 50 |
-
}
|
| 51 |
-
},
|
| 52 |
-
{
|
| 53 |
-
"name": "search",
|
| 54 |
-
"description": "Search for information on the internet",
|
| 55 |
-
"parameters": {
|
| 56 |
-
"type": "object",
|
| 57 |
-
"properties": {
|
| 58 |
-
"query": {
|
| 59 |
-
"type": "string",
|
| 60 |
-
"description": "The search query, e.g. 'latest news on AI'"
|
| 61 |
-
}
|
| 62 |
-
},
|
| 63 |
-
"required": ["query"]
|
| 64 |
-
}
|
| 65 |
-
}
|
| 66 |
-
], indent=2)
|
| 67 |
-
|
| 68 |
-
example_query = "What's the weather like in New York in fahrenheit?"
|
| 69 |
-
|
| 70 |
-
def convert_to_xlam_tool(tools):
|
| 71 |
-
if isinstance(tools, dict):
|
| 72 |
-
return {
|
| 73 |
-
"name": tools["name"],
|
| 74 |
-
"description": tools["description"],
|
| 75 |
-
"parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
|
| 76 |
-
}
|
| 77 |
-
elif isinstance(tools, list):
|
| 78 |
-
return [convert_to_xlam_tool(tool) for tool in tools]
|
| 79 |
-
else:
|
| 80 |
-
return tools
|
| 81 |
-
|
| 82 |
-
def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
|
| 83 |
-
prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
|
| 84 |
-
prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(tools)}\n[END OF AVAILABLE TOOLS]\n\n"
|
| 85 |
-
prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
|
| 86 |
-
prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
|
| 87 |
-
return prompt
|
| 88 |
-
|
| 89 |
@spaces.GPU
|
| 90 |
-
def generate_response(
|
| 91 |
-
try:
|
| 92 |
-
tools = json.loads(tools_input)
|
| 93 |
-
except json.JSONDecodeError:
|
| 94 |
-
return "Error: Invalid JSON format for tools input."
|
| 95 |
-
|
| 96 |
-
xlam_format_tools = convert_to_xlam_tool(tools)
|
| 97 |
-
content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)
|
| 98 |
-
|
| 99 |
messages = [
|
| 100 |
{'role': 'user', 'content': content}
|
| 101 |
]
|
| 102 |
|
| 103 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 104 |
outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
|
| 105 |
-
agent_action = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
| 106 |
|
| 107 |
-
return
|
| 108 |
|
| 109 |
# Gradio interface
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
gr.Markdown(title)
|
| 112 |
-
gr.Markdown(description)
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
with gr.Column():
|
| 116 |
-
tools_input = gr.Code(
|
| 117 |
-
label="Available Tools (JSON format)",
|
| 118 |
-
lines=20,
|
| 119 |
-
value=example_tools,
|
| 120 |
-
language='json'
|
| 121 |
-
)
|
| 122 |
query_input = gr.Textbox(
|
| 123 |
-
label="User
|
| 124 |
-
lines=
|
| 125 |
-
value=example_query
|
| 126 |
)
|
| 127 |
submit_button = gr.Button("Generate Response")
|
| 128 |
|
| 129 |
with gr.Column():
|
| 130 |
-
output = gr.Code(label="
|
| 131 |
|
| 132 |
-
submit_button.click(generate_response, inputs=[
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|
|
|
|
| 7 |
# Load model and tokenizer
|
| 8 |
model_name = "Salesforce/xLAM-1b-fc-r"
|
| 9 |
|
| 10 |
+
title = f"# Eval Model: {model_name}"
|
|
|
|
| 11 |
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 15 |
# Set random seed for reproducibility
|
| 16 |
torch.random.manual_seed(0)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@spaces.GPU
|
| 19 |
+
def generate_response(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
messages = [
|
| 21 |
{'role': 'user', 'content': content}
|
| 22 |
]
|
| 23 |
|
| 24 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 25 |
outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
|
|
|
|
| 26 |
|
| 27 |
+
return outputs
|
| 28 |
|
| 29 |
# Gradio interface
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
gr.Markdown(title)
|
|
|
|
| 32 |
|
| 33 |
with gr.Row():
|
| 34 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
query_input = gr.Textbox(
|
| 36 |
+
label="User Content",
|
| 37 |
+
lines=20
|
|
|
|
| 38 |
)
|
| 39 |
submit_button = gr.Button("Generate Response")
|
| 40 |
|
| 41 |
with gr.Column():
|
| 42 |
+
output = gr.Code(label="Response :", lines=10, language="json")
|
| 43 |
|
| 44 |
+
submit_button.click(generate_response, inputs=[query_input], outputs=output)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
demo.launch()
|