ankush13r commited on
Commit
a255fea
·
verified ·
1 Parent(s): 118ce78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -48
app.py CHANGED
@@ -1,64 +1,150 @@
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,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
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
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
 
1
  import gradio as gr
2
+ from gradio import ChatMessage
3
 
4
+ import json
5
+ from openai import OpenAI
6
+ from tools import tools, oitools
7
+ from dotenv import load_dotenv
8
+ import os
9
+ load_dotenv(".env")
10
+ HF_TOKEN = os.environ["HF_TOKEN"]
11
+ BASE_URL = os.environ["BASE_URL"]
12
+
13
+ SYSTEM_PROMPT_TEMPLATE = """
14
+ You are an AI assistant designed to assist users with a hotel booking and information system. Your role is to provide detailed and accurate information about the hotel, including available accommodations, facilities, dining options, and reservation services. You can check room availability, assist with bookings, modify or cancel reservations, and answer general inquiries about the hotel.
15
+
16
+ Maintain clarity, conciseness, and relevance in your responses, ensuring a seamless user experience. Always respond in the same **language as the user’s query** to preserve their preferred language.
17
  """
 
 
 
18
 
19
+ client = OpenAI(
20
+ base_url=BASE_URL + "/v1",
21
+ api_key=HF_TOKEN
22
+ )
23
 
 
 
 
 
 
 
 
 
 
24
 
25
+ def complation(history, model, tools=None):
26
+ system_prompt = SYSTEM_PROMPT_TEMPLATE
27
+ messages = [{"role": "system", "content": system_prompt}]
28
+ for msg in history:
29
+ if type(msg) == dict:
30
+ msg = ChatMessage(**msg)
31
+ if msg.role == "assistant" and len(msg.options) > 0 and msg.options[0]["label"] == "tool_calls":
32
+ tools_calls = json.loads(msg.options[0]["value"])
33
+ messages.append({"role": "assistant", "tool_calls": tools_calls})
34
+ messages.append({"role": "tool", "content": msg.content})
35
+ else:
36
+ messages.append({"role": msg.role, "content": msg.content})
37
+
38
+ if not tools:
39
+ return client.chat.completions.create(
40
+ model=model,
41
+ messages=messages,
42
+ stream=True,
43
+ max_tokens=1000,
44
+ temperature=0.4,
45
+ frequency_penalty=1,
46
+ # stop=["<|em_end|>"],
47
+ extra_body = {
48
+ "repetition_penalty": 1.1,
49
+ }
50
+ )
51
+ return client.chat.completions.create(
52
+ model=model,
53
+ messages=messages,
54
+ stream=True,
55
+ max_tokens=1000,
56
+ temperature=0.4,
57
+ tool_choice="auto",
58
+ tools=tools,
59
+ frequency_penalty=1,
60
+ # stop=["<|em_end|>"],
61
+ extra_body = {
62
+ "repetition_penalty": 1.1,
63
+ }
64
+ )
65
 
66
+ def respond(
67
+ message:any,
68
+ history:any,
69
+ ):
70
+ try:
71
+ models = client.models.list()
72
+ model = models.data[0].id
73
+ except Exception as err:
74
+ gr.Warning("The model is initializing. Please wait; this may take 5 to 10 minutes ⏳.", duration=20)
75
+ raise err
76
 
77
  response = ""
78
+ arguments = ""
79
+ name = ""
80
+ history.append(
81
+ ChatMessage(
82
+ role="user",
83
+ content=message,
84
+ )
85
+ )
86
+ completion = complation(history=history, tools=oitools, model=model)
87
+ appended = False
88
+ for chunk in completion:
89
+ if len(chunk.choices) > 0 and chunk.choices[0].delta.tool_calls and len(chunk.choices[0].delta.tool_calls) > 0 :
90
+ call = chunk.choices[0].delta.tool_calls[0]
91
+ if call.function.name:
92
+ name=call.function.name
93
+ if call.function.arguments:
94
+ arguments += call.function.arguments
95
 
96
+ elif chunk.choices[0].delta.content:
97
+ response += chunk.choices[0].delta.content
98
+ if not appended:
99
+ history.append(
100
+ ChatMessage(
101
+ role="assistant",
102
+ content="",
103
+ )
104
+ )
105
+ appended = True
106
+
107
+ history[-1].content = response
108
+ yield history[-1]
109
+
110
+ if not arguments:
111
+ arguments = "{}"
112
+ if name:
113
+ json_arguments = json.loads(arguments)
114
+ result = f"💥 Error using tool {name}, tools doesn't exists"
115
+ if tools.get(name):
116
+ result = str(tools[name].invoke(input=json_arguments))
117
+ result = json.dumps({name: result}, ensure_ascii=False)
118
+ history.append(
119
+ ChatMessage(
120
+ role="assistant",
121
+ content=result,
122
+ metadata= {"title": f"🛠️ Used tool '{name}'"},
123
+ options=[{"label":"tool_calls", "value": json.dumps([{"id": "call_FthC9qRpsL5kBpwwyw6c7j4k","function": {"arguments": arguments,"name": name},"type": "function"}])}]
124
+ )
125
+ )
126
+ yield history[-1]
127
 
128
+ completion = complation(history=history, tools=oitools, model=model)
129
+ result = ""
130
+ appended = False
131
+ for chunk in completion:
132
+ result += chunk.choices[0].delta.content
133
+ if not appended:
134
+ history.append(
135
+ ChatMessage(
136
+ role="assistant",
137
+ content="",
138
+ )
139
+ )
140
+ appended = True
141
+
142
+ history[-1].content = result
143
+ yield history[-2:]
144
 
145
  """
146
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
147
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  if __name__ == "__main__":
149
+ demo = gr.ChatInterface(respond, type="messages")
150
+ demo.launch()