Spaces:
Sleeping
Sleeping
changes for actiongemma
Browse files
app.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
7 |
-
|
8 |
-
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,29 +17,77 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
42 |
"""
|
43 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
@@ -45,17 +95,10 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
|
|
|
|
59 |
)
|
60 |
|
61 |
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import json
|
5 |
|
6 |
"""
|
7 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
8 |
"""
|
9 |
+
model = AutoModelForCausalLM.from_pretrained("KishoreK/ActionGemma-9B", load_in_4bit=True, device_map="auto")
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("KishoreK/ActionGemma-9B")
|
11 |
|
12 |
def respond(
|
13 |
message,
|
|
|
17 |
temperature,
|
18 |
top_p,
|
19 |
):
|
20 |
+
task_instruction = """
|
21 |
+
You are an expert in composing functions. You are given a question and a set of possible functions.
|
22 |
+
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
|
23 |
+
If none of the functions can be used, point it out and refuse to answer.
|
24 |
+
If the given question lacks the parameters required by the function, also point it out.
|
25 |
+
""".strip()
|
26 |
+
|
27 |
+
get_weather_api = {
|
28 |
+
"name": "get_weather",
|
29 |
+
"description": "Get the current weather for a location",
|
30 |
+
"parameters": {
|
31 |
+
"type": "object",
|
32 |
+
"properties": {
|
33 |
+
"location": {
|
34 |
+
"type": "string",
|
35 |
+
"description": "The city and state, e.g. San Francisco, New York"
|
36 |
+
},
|
37 |
+
"unit": {
|
38 |
+
"type": "string",
|
39 |
+
"enum": ["celsius", "fahrenheit"],
|
40 |
+
"description": "The unit of temperature to return"
|
41 |
+
}
|
42 |
+
},
|
43 |
+
"required": ["location"]
|
44 |
+
}
|
45 |
+
}
|
46 |
|
47 |
+
search_api = {
|
48 |
+
"name": "search",
|
49 |
+
"description": "Search for information on the internet",
|
50 |
+
"parameters": {
|
51 |
+
"type": "object",
|
52 |
+
"properties": {
|
53 |
+
"query": {
|
54 |
+
"type": "string",
|
55 |
+
"description": "The search query, e.g. 'latest news on AI'"
|
56 |
+
}
|
57 |
+
},
|
58 |
+
"required": ["query"]
|
59 |
+
}
|
60 |
+
}
|
61 |
|
62 |
+
openai_format_tools = [get_weather_api, search_api]
|
63 |
|
64 |
+
def convert_to_xlam_tool(tools):
|
65 |
+
''''''
|
66 |
+
if isinstance(tools, dict):
|
67 |
+
return {
|
68 |
+
"name": tools["name"],
|
69 |
+
"description": tools["description"],
|
70 |
+
"parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
|
71 |
+
}
|
72 |
+
elif isinstance(tools, list):
|
73 |
+
return [convert_to_xlam_tool(tool) for tool in tools]
|
74 |
+
else:
|
75 |
+
return tools
|
76 |
|
77 |
+
user_query = message
|
78 |
+
tools = openai_format_tools
|
79 |
+
messages = [{
|
80 |
+
"role" : "system",
|
81 |
+
"content" : task_instruction
|
82 |
+
},{
|
83 |
+
"role" : "user",
|
84 |
+
"content" : user_query
|
85 |
+
},{
|
86 |
+
"role": "tools",
|
87 |
+
"content": json.dumps(convert_to_xlam_tool(tools))
|
88 |
+
}]
|
89 |
|
90 |
+
return tokenizer.decode(tokenizer.apply_chat_template(messages, add_generation_prompt=True))
|
|
|
91 |
|
92 |
"""
|
93 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
95 |
demo = gr.ChatInterface(
|
96 |
respond,
|
97 |
additional_inputs=[
|
98 |
+
gr.Textbox(value="You are an expert in composing functions.", label="System message"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
],
|
100 |
+
examples=["अमेरिका के राष्ट्रपति कौन है?"],
|
101 |
+
description="This is ActionGemma, LAM with multi-lingual capabilities. currently this model is prompted with only 2 tools available : get_weather_api and search_api. Integrations for more api's will be coming soon."
|
102 |
)
|
103 |
|
104 |
|