kokluch commited on
Commit
542f4c2
Β·
1 Parent(s): 88c90c5

MCP server for Confusables

Browse files
Files changed (3) hide show
  1. README.md +2 -4
  2. app.py +95 -60
  3. requirements.txt +2 -1
README.md CHANGED
@@ -1,13 +1,11 @@
1
  ---
2
- title: Confusables Mcp
3
  emoji: πŸ’¬
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.0.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  ---
12
-
13
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
1
  ---
2
+ title: Confusables Tools MCP
3
  emoji: πŸ’¬
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.35.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  ---
 
 
app.py CHANGED
@@ -1,64 +1,99 @@
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 confusables import is_confusable, confusable_characters, confusable_regex, normalize
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ def gr_is_confusable(str1: str, str2: str) -> bool:
5
+ """
6
+ Check if two strings are visually confusable (i.e., they look similar to a human, but may use different Unicode characters).
7
+
8
+ Args:
9
+ str1: The first string to compare.
10
+ str2: The second string to compare.
11
+
12
+ Returns:
13
+ True if the two strings are visually confusable, False otherwise.
14
+
15
+ Example:
16
+ gr_is_confusable("rover", "Ʀỏ𝕍3β„›") # True
17
+ """
18
+ return is_confusable(str1, str2)
19
+
20
+ def gr_confusable_characters(char: str) -> list[str]:
21
+ """
22
+ List all Unicode characters or character sequences that are visually confusable with the given character.
23
+
24
+ Args:
25
+ char: The character to analyze (must be a single character).
26
+
27
+ Returns:
28
+ A list of confusable characters or sequences.
29
+
30
+ Example:
31
+ gr_confusable_characters("c") # ['Δ‹', 'α΄„', '𝔠', ...]
32
+ """
33
+ return confusable_characters(char)
34
+
35
+ def gr_confusable_regex(string: str, include_character_padding: bool) -> str:
36
+ """
37
+ Generate a regular expression string that matches all visually confusable variants of the input string.
38
+
39
+ Args:
40
+ string: The reference string.
41
+ include_character_padding: If True, the regex will allow for character padding (extra confusable characters between the main ones).
42
+
43
+ Returns:
44
+ A regex pattern string.
45
+
46
+ Example:
47
+ gr_confusable_regex("bore", True)
48
+ """
49
+ return confusable_regex(string, include_character_padding)
50
+
51
+ def gr_normalize(string: str, prioritize_alpha: bool) -> list[str]:
52
+ """
53
+ Return all possible normalized forms of a string, converting confusable Unicode characters to their closest ASCII or Latin-alphabet equivalents.
54
+
55
+ Args:
56
+ string: The string to normalize.
57
+ prioritize_alpha: If True, prioritizes conversion to Latin alphabet characters.
58
+
59
+ Returns:
60
+ A list of possible normalized forms.
61
+
62
+ Example:
63
+ gr_normalize("Ʀỏ𝕍3β„›", True) # ['rov3r', 'rover']
64
+ """
65
+ return normalize(string, prioritize_alpha)
66
+
67
+ with gr.Blocks() as demo:
68
+ gr.Markdown("""# MCP Server - Confusables API\nMain features of the confusables library exposed as tools.""")
69
+ with gr.Tab("is_confusable"):
70
+ gr.Interface(
71
+ fn=gr_is_confusable,
72
+ inputs=[gr.Textbox(label="String 1"), gr.Textbox(label="String 2")],
73
+ outputs=gr.Textbox(label="Confusable? (True/False)"),
74
+ allow_flagging="never"
75
+ ).render()
76
+ with gr.Tab("confusable_characters"):
77
+ gr.Interface(
78
+ fn=gr_confusable_characters,
79
+ inputs=gr.Textbox(label="Character"),
80
+ outputs=gr.Textbox(label="Confusable characters"),
81
+ allow_flagging="never"
82
+ ).render()
83
+ with gr.Tab("confusable_regex"):
84
+ gr.Interface(
85
+ fn=gr_confusable_regex,
86
+ inputs=[gr.Textbox(label="String"), gr.Checkbox(label="Include character padding")],
87
+ outputs=gr.Textbox(label="Regex"),
88
+ allow_flagging="never"
89
+ ).render()
90
+ with gr.Tab("normalize"):
91
+ gr.Interface(
92
+ fn=gr_normalize,
93
+ inputs=[gr.Textbox(label="String"), gr.Checkbox(label="Prioritize alpha")],
94
+ outputs=gr.Textbox(label="Normalized forms"),
95
+ allow_flagging="never"
96
+ ).render()
97
 
98
  if __name__ == "__main__":
99
+ demo.launch(mcp_server=True)
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- huggingface_hub==0.25.2
 
 
1
+ gradio[mcp]
2
+ confusables