RemVdH commited on
Commit
e310d6a
·
verified ·
1 Parent(s): 85e53ab

Update app.py

Browse files

Prepare for workshop

Files changed (1) hide show
  1. app.py +76 -55
app.py CHANGED
@@ -1,64 +1,85 @@
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
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
+ from tools.final_answer import FinalAnswerTool
3
+ from tools.youtube_search import YoutubeSearchTool
4
+ from tools.play_music import PlaySomeMusicMeastro
5
+ from tools.download_music import DownloadSomeMusicMeastro
6
+ from typing import List
7
+ from Gradio_UI import GradioUI
8
 
9
+ import os
10
+ import base64
11
+ import yaml
12
+ import wikipedia
13
 
14
+ @tool
15
+ def search_wikipedia_pages(search_string: str) -> List[str]:
16
+ """This tool helps to search for the names of potential names of wikipedia pages that relate to a search string.
17
+ This doesn't guarantee that every returned string is a perfect, exact page title that wikipedia.page() will accept without any issue.
18
+ This tool sends a query to the Wikipedia API's search endpoint. This endpoint performs a full-text search across Wikipedia's article titles and content.
19
+ This will not retrieve the actual pages, but gives oversight of the different keywords on wikipedia to find more information.
20
+ This tool will output the first 10 strings related to the search string as a list of strings.
21
+ Args:
22
+ search_string: the search string
23
+ """
24
+ try:
25
+ page_titles = wikipedia.search(search_string, results=10)
26
+ return page_titles
27
+ except wikipedia.exceptions.PageError as err:
28
+ s_error = f"PageError: {err}"
29
+ except wikipedia.exceptions.DisambiguationError as err:
30
+ s_error = f"DisambiguationError {err}"
31
+ print (f"An error occured {s_error}")
32
 
33
+ @tool
34
+ def get_wikipedia_page_content(page_name: str, p_summary: bool = True) -> str:
35
+ """This tool helps to get the content of wikipedia pages by their name.
36
+ If you use this tool based on the output of search_wikipedia_pages
37
+ than remember to try a next string that was the output of search_wikipedia_pages if you get a error: Page id
38
+ The output is a string with the actual content of the wikipedia page.
39
+ Args:
40
+ page_name: the name of the page in wikipedia
41
+ p_summary: do you want the full content or only the summary of the content
42
+ """
43
+ s_error = ""
44
+ try:
45
+ a_page = wikipedia.page(title=page_name)
46
+ if p_summary:
47
+ return a_page.summary
48
+ else:
49
+ return a_page.content
50
+ except wikipedia.exceptions.PageError as err:
51
+ s_error = f"PageError: {err}"
52
+ except wikipedia.exceptions.DisambiguationError as err:
53
+ s_error = f"DisambiguationError {err}"
54
+ print (f"An error occured {s_error}")
55
+
56
+ final_answer = FinalAnswerTool()
57
 
58
+ model = HfApiModel(
59
+ max_tokens=2096,
60
+ temperature=0.5,
61
+ #model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
62
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
63
+ custom_role_conversions=None,
64
+ )
65
 
66
+ # Import tool from Hub
67
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
68
 
69
+ with open("prompts.yaml", 'r') as stream:
70
+ prompt_templates = yaml.safe_load(stream)
71
 
72
+ agent = CodeAgent(
73
+ model=model,
74
+ tools=[final_answer, search_wikipedia_pages, get_wikipedia_page_content, PlaySomeMusicMeastro(), YoutubeSearchTool(), DownloadSomeMusicMeastro(), image_generation_tool],
75
+ max_steps=12,
76
+ verbosity_level=1,
77
+ grammar=None,
78
+ planning_interval=None,
79
+ name=None,
80
+ description=None,
81
+ prompt_templates=prompt_templates,
82
+ add_base_tools=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  )
84
 
85
+ GradioUI(agent).launch()