Mtchmann commited on
Commit
2d1f0d6
·
verified ·
1 Parent(s): 8c6c712

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -68
app.py CHANGED
@@ -1,95 +1,48 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
-
4
- # Import necessary libraries
5
  import random
6
  from smolagents import CodeAgent
7
-
8
- # Import our custom tools from their modules
9
  from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
10
  from retriever import load_guest_dataset
11
 
12
- """
13
- 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
14
- """
15
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
16
 
 
 
 
 
 
17
 
18
- def respond(
19
- message,
20
- history: list[tuple[str, str]],
21
- system_message,
22
- max_tokens,
23
- temperature,
24
- top_p,
25
- ):
26
- messages = [{"role": "system", "content": system_message}]
27
 
 
 
 
28
  for val in history:
29
  if val[0]:
30
- messages.append({"role": "user", "content": val[0]})
31
  if val[1]:
32
- messages.append({"role": "assistant", "content": val[1]})
33
-
34
- messages.append({"role": "user", "content": message})
35
-
36
- response = ""
37
 
38
- for message in client.chat_completion(
39
- messages,
40
- max_tokens=max_tokens,
41
- stream=True,
42
- temperature=temperature,
43
- top_p=top_p,
44
- ):
45
- token = message.choices[0].delta.content
46
 
47
- response += token
48
- yield response
49
-
50
-
51
- """
52
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
53
- """
54
  demo = gr.ChatInterface(
55
  respond,
56
  additional_inputs=[
57
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
58
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
59
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
60
- gr.Slider(
61
- minimum=0.1,
62
- maximum=1.0,
63
- value=0.95,
64
- step=0.05,
65
- label="Top-p (nucleus sampling)",
66
- ),
67
  ],
68
  )
69
 
70
-
71
  if __name__ == "__main__":
72
  demo.launch()
73
-
74
-
75
-
76
-
77
- # Initialize the web serarch tool
78
- search_tool = DuckDuckGoSearchTool()
79
-
80
- # Initialize the weather tool
81
- weather_info_tool = WeatherInfoTool()
82
-
83
- # Initialize the Hub stats tool
84
- hub_stats_tool = HubStatsTool()
85
-
86
- # Load the guest dataset and initialize the guest info tool
87
- guest_info_tool = load_guest_dataset()
88
-
89
- # Create Alfred with all the tools
90
- alfred = CodeAgent(
91
- tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
92
- model=client,
93
- add_base_tools=True, # Add any additional base tools
94
- planning_interval=3 # Enable planning every 3 steps
95
- )
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
  import random
4
  from smolagents import CodeAgent
 
 
5
  from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
6
  from retriever import load_guest_dataset
7
 
 
 
 
8
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
 
10
+ # Initialize tools and agent BEFORE creating the Gradio interface
11
+ search_tool = DuckDuckGoSearchTool()
12
+ weather_info_tool = WeatherInfoTool()
13
+ hub_stats_tool = HubStatsTool()
14
+ guest_info_tool = load_guest_dataset()
15
 
16
+ alfred = CodeAgent(
17
+ tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
18
+ model=client,
19
+ add_base_tools=True,
20
+ planning_interval=3
21
+ )
 
 
 
22
 
23
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
24
+ # Format the conversation history and new message into a prompt
25
+ prompt = f"System: {system_message}\n"
26
  for val in history:
27
  if val[0]:
28
+ prompt += f"User: {val[0]}\n"
29
  if val[1]:
30
+ prompt += f"Assistant: {val[1]}\n"
31
+ prompt += f"User: {message}\nAssistant:"
 
 
 
32
 
33
+ # Use alfred to generate the response
34
+ response = alfred.run(prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
35
+ yield response
 
 
 
 
 
36
 
 
 
 
 
 
 
 
37
  demo = gr.ChatInterface(
38
  respond,
39
  additional_inputs=[
40
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
41
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
42
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
43
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
44
  ],
45
  )
46
 
 
47
  if __name__ == "__main__":
48
  demo.launch()