AliA1997 commited on
Commit
fc8b9a4
·
1 Parent(s): bc8fe0f

Tested locally, completed tutorial locally.

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. __pycache__/tools.cpython-313.pyc +0 -0
  3. app.py +36 -48
  4. requirements.txt +5 -0
  5. tools.py +41 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
__pycache__/tools.cpython-313.pyc ADDED
Binary file (2.54 kB). View file
 
app.py CHANGED
@@ -1,44 +1,45 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def respond(
6
  message,
7
  history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = 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
@@ -46,23 +47,10 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
46
  chatbot = gr.ChatInterface(
47
  respond,
48
  type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
  )
62
 
63
  with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
  chatbot.render()
67
 
68
 
 
1
+ import os
2
  import gradio as gr
3
+ from huggingface_hub import login
4
+ from smolagents import DuckDuckGoSearchTool, InferenceClientModel, CodeAgent
5
+ from tools import best_city, ClassifierTool
6
+
7
+ web_search_tool = DuckDuckGoSearchTool()
8
+ classifier_tool = ClassifierTool()
9
+
10
+ hf_token = os.environ.get('HF_TOKEN')
11
+ if hf_token:
12
+ login(token=hf_token)
13
+
14
+ model = InferenceClientModel(model_id='Qwen/Qwen3-4B-Instruct-2507', token=hf_token)
15
+
16
+ tools = [
17
+ web_search_tool,
18
+ classifier_tool,
19
+ best_city
20
+ ]
21
+
22
+ my_aiagent = CodeAgent(
23
+ tools=tools,
24
+ # For the purpose of this tutorial, just have tools you integrated.
25
+ # Also by default when teh add_base_tools is set to true, it will integrate DuckDuckGo Search.
26
+ add_base_tools=False,
27
+ model=model
28
+ )
29
 
30
  def respond(
31
  message,
32
  history: list[dict[str, str]],
33
+ system_message
 
 
 
 
34
  ):
35
+ full_prompt = f"{system_message}\n\nChat history:\n{history}\n\nUser: {message}"
36
+ response = my_aiagent.run(
37
+ full_prompt,
38
+ max_steps=5,
39
+ stream=False,
40
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ yield response
43
 
44
  """
45
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
47
  chatbot = gr.ChatInterface(
48
  respond,
49
  type="messages",
50
+ additional_inputs=[],
 
 
 
 
 
 
 
 
 
 
 
51
  )
52
 
53
  with gr.Blocks() as demo:
 
 
54
  chatbot.render()
55
 
56
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ smolagents
2
+ torch
3
+ transformers
4
+ huggingface_hub
5
+ ddgs
tools.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # All the tools you exported
2
+ from transformers import pipeline
3
+ from smolagents import tool, Tool
4
+
5
+ @tool
6
+ def best_city(input:str) -> str:
7
+ """
8
+ Suggests a the best city regardless of country
9
+ Args:
10
+ input (str): Any prompt indicating to get the best city. Allowed values are:
11
+ - Kuala Lumpar, Malaysia
12
+ """
13
+ return "Kuala Lumpar, Malaysia"
14
+
15
+
16
+ class ClassifierTool(Tool):
17
+ name = "zero_shot_classifier_tool"
18
+ description = "Classifies a sequence into given labels to determine if it is about a location or city."
19
+ inputs = {
20
+ "text": {"type": "string", "description": "The sequence to classify."}
21
+ }
22
+ output_type = "string"
23
+
24
+ def __init__(self, *args, **kwargs):
25
+ super().__init__(*args, **kwargs)
26
+ # Perform heavy computations such as initializing pipeline
27
+ self.classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
28
+ self.location_labels = ['Favorite City', "Location", "City", 'Favorite Location', 'Best City', 'Best Location']
29
+ self.candidate_labels = [*self.location_labels, 'Other']
30
+
31
+ def forward(self, text: str) -> str:
32
+ print(f"Before Response::")
33
+ response = classifier(text, self.candidate_labels, multi_label=True, hypothesis_template="This prompt is about {}.")
34
+ print(f"Respoonse::: {response}")
35
+ # Check if any location labels meets the requirement
36
+ for label, score in zip(response['labels'], response['scores']):
37
+ print(f"Label: {label}, Score: {score:.4f}")
38
+ if label != 'Other' and score > 0.7:
39
+ return f"Match found: {label} (Confidence: {score:.4f})"
40
+
41
+ return "No location match found."