KishoreK commited on
Commit
ba5864f
1 Parent(s): 53ee5cc

changes for actiongemma

Browse files
Files changed (1) hide show
  1. app.py +73 -30
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
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
 
10
  def respond(
11
  message,
@@ -15,29 +17,77 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
 
38
 
39
- response += token
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 a friendly Chatbot.", label="System message"),
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