Spaces:
Runtime error
Runtime error
Josep Pon Farreny
commited on
Commit
·
3b57587
1
Parent(s):
68b2c16
feat: Add two sample gradio tools
Browse files- app.py +7 -56
- tdagent/tools/__init__.py +0 -0
- tdagent/tools/get_url_content.py +65 -0
- tdagent/tools/letter_counter.py +23 -0
app.py
CHANGED
@@ -1,64 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
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 |
-
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
+
from tdagent.tools.get_url_content import gr_get_url_http_content
|
4 |
+
from tdagent.tools.letter_counter import gr_letter_counter
|
|
|
|
|
5 |
|
6 |
|
7 |
+
gr_app = gr.TabbedInterface(
|
8 |
+
[
|
9 |
+
gr_get_url_http_content,
|
10 |
+
gr_letter_counter,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
],
|
12 |
)
|
13 |
|
|
|
14 |
if __name__ == "__main__":
|
15 |
+
gr_app.launch(mcp_server=True)
|
tdagent/tools/__init__.py
ADDED
File without changes
|
tdagent/tools/get_url_content.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import enum
|
2 |
+
from collections.abc import Sequence
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import requests
|
6 |
+
|
7 |
+
|
8 |
+
class HttpContentType(str, enum.Enum):
|
9 |
+
"""Http content type values."""
|
10 |
+
|
11 |
+
HTML = "text/html"
|
12 |
+
JSON = "application/json"
|
13 |
+
|
14 |
+
|
15 |
+
def get_url_http_content(
|
16 |
+
url: str,
|
17 |
+
content_type: Sequence[HttpContentType] | None = None,
|
18 |
+
timeout: int = 30,
|
19 |
+
) -> tuple[str, str]:
|
20 |
+
"""Get the content of a URL using an HTTP GET request.
|
21 |
+
|
22 |
+
Args:
|
23 |
+
url: The URL to fetch the content from.
|
24 |
+
content_type: If given it should contain the expected
|
25 |
+
content types in the response body. The server may
|
26 |
+
not honor the requested content types.
|
27 |
+
timeout: Request timeout in seconds. Defaults to 30.
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
A pair of strings (content, error_message). If there is an
|
31 |
+
error getting content from the URL the `content` will be
|
32 |
+
empty and `error_message` will, usually, contain the error
|
33 |
+
cause. Otherwise, `error_message` will be empty and the
|
34 |
+
content will be filled with data fetched from the URL.
|
35 |
+
"""
|
36 |
+
headers = {}
|
37 |
+
|
38 |
+
if content_type:
|
39 |
+
headers["Accept"] = ",".join(content_type)
|
40 |
+
response = requests.get(
|
41 |
+
url,
|
42 |
+
headers=headers,
|
43 |
+
timeout=timeout,
|
44 |
+
)
|
45 |
+
|
46 |
+
try:
|
47 |
+
response.raise_for_status()
|
48 |
+
except requests.HTTPError as err:
|
49 |
+
return "", str(err)
|
50 |
+
|
51 |
+
return response.text, ""
|
52 |
+
|
53 |
+
|
54 |
+
gr_get_url_http_content = gr.Interface(
|
55 |
+
fn=get_url_http_content,
|
56 |
+
inputs=["text", "text"],
|
57 |
+
outputs="text",
|
58 |
+
title="Get the content of a URL using an HTTP GET request.",
|
59 |
+
description=(
|
60 |
+
"Get the content of a URL in one of the specified content types."
|
61 |
+
" The server may not honor the content type and if it fails the"
|
62 |
+
" reason should also be returned with the corresponding HTTP"
|
63 |
+
" error code."
|
64 |
+
),
|
65 |
+
)
|
tdagent/tools/letter_counter.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
+
def letter_counter(word: str, letter: str) -> int:
|
5 |
+
"""Count the occurrences of a specific letter in a word.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
word: The word or phrase to analyze
|
9 |
+
letter: The letter to count occurrences of
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
The number of times the letter appears in the word
|
13 |
+
"""
|
14 |
+
return word.lower().count(letter.lower())
|
15 |
+
|
16 |
+
|
17 |
+
gr_letter_counter = gr.Interface(
|
18 |
+
fn=letter_counter,
|
19 |
+
inputs=["text", "text"],
|
20 |
+
outputs="number",
|
21 |
+
title="Letter Counter",
|
22 |
+
description="Count how many times a letter appears in a word",
|
23 |
+
)
|