File size: 5,647 Bytes
a4d7c03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#########################################################################
# Copyright (C)                                                       	#
# 2025-June Sen Li (Sen.Li.Sprout@gmail.com)		     			    #
# Permission given to modify the code only for Non-Profit Research		#
# as long as you keep this declaration at the top 						#
#########################################################################
import os

import gradio as gr
import smolagents

# Launch the interface and MCP server
if __name__ == "__main__":
    print(f"os.getcwd() = {os.getcwd()}")
    os.system(f"echo ls -al {os.getcwd()} && ls -al {os.getcwd()}")
    os.system(f"echo ls -al /: && ls -al /")
    os.system(f"echo ls -al /home/: && ls -al /home/")

    dictServerParams_TextSimilarity = {
        "url":          "https://allillusion-mcp-server-textsimilarity.hf.space/gradio_api/mcp/sse",
        "transport":    "sse",
    }  # either HF-Space or Local  {"url": "http://localhost:7860/gradio_api/mcp/sse", "transport": ...}

    try:
        mcpClient_SyntheticText_Similarity = smolagents.mcp_client.MCPClient(dictServerParams_TextSimilarity)
        print(f"type(mcpClient_SyntheticText_Similarity) = {type(mcpClient_SyntheticText_Similarity)}")

        list_MCPAdaptTools_SyntheticText_Similarity = mcpClient_SyntheticText_Similarity.get_tools()
        print(f"len(list_MCPAdaptTools_SyntheticText_Similarity) = {len(list_MCPAdaptTools_SyntheticText_Similarity)}")
        print(f"list_MCPAdaptTools_SyntheticText_Similarity[0]   = {list_MCPAdaptTools_SyntheticText_Similarity[0]}")

        clientModel_Qwen25_Inference = smolagents.InferenceClientModel(model_id = "Qwen/Qwen2.5-Coder-3B-Instruct")
        print(f"clientModel_Qwen25_Inference.model_id   = {clientModel_Qwen25_Inference.model_id}\n")
        print(f"clientModel_Qwen25_Inference.client     = {clientModel_Qwen25_Inference.client}")

        codeAgent_Qwen25_SentimentalAnalysis = smolagents.CodeAgent(tools = list_MCPAdaptTools_SyntheticText_Similarity,
                                                                    model = clientModel_Qwen25_Inference)

        ''' 401 Client Error: Unauthorized for url: https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B..
                    On the space settings, go to Variables and secrets, and create a new secret named HF_TOKEN
                            The value of the secret should be your access token
                    If you need to create an access token, go to your HF profile page
                            On the left menu, go to the access token option, then the Create new token, on the top right
        '''
        str_Description = "A simple MCP-Client, Qwen2.5-3B Agent (NOT 32B!) calling MCP-Server_TextSimilarity as an MCP tool to Generate Synthetic Text with Similarity."   \
                          " https://huggingface.co/spaces/AllIllusion/MCP-Server_TextSimilarity" \
                          "  If you see 'Error', that's because my account has exceeded the monthly included credits for Inference Providers (Qwen2.5-3B)." \
                          "  Change from default 32B to 3B, maybe less expensive :)"

        def func_genSyntheticText_Similarity(str_RealText):
            strTask_Message = f'''You are learning and practicing synthetic note writing. Your task is to generate synthetic notes, modeled on real note structure and content. You will learn from a pseudonymized note to guide your language, structure, and reasoning.

            To avoid generating synthetic text from nowhere, you have been provided with pseudonymized, real note for use as a learning example. 

            ### Learning Example (quoted by <Example>...</Example>):

            <Example>{str_RealText}</Example>

            ### Instructions:
            1. Study the provided learning example.
            2. Generate a synthetic note that:
               - Mimics the sentence types and key characteristic distribution of the selected example.
               - Follows a common sense plausible structure and progression.
            3. Compare the similarity using the tool MCP-Server_TextSimilarity, between the provided Learning Example and your generated synthetic text.
            4. Return only the synthetic note, and the direct output of the tool MCP-Server_TextSimilarity.
            '''
            print(f"strTask_Message = {strTask_Message}")

            return str(codeAgent_Qwen25_SentimentalAnalysis.run(strTask_Message))


        # 03. Gradio UI elements
        with gr.Blocks(title="MCP-Client: SyntheticText + Tool-Similarity") as grBlocks_SentenceSimilarity__MCP_Server:
            gr.Markdown(str_Description)

            with gr.Row():
                grTextBox_RealText      = gr.Textbox(label="Real Text Input", lines=20,
                                               placeholder="Put your real text here ...", show_label=True)
                grTextBox_SyntheticText = gr.Textbox(label="Synthetic Text Output with Similarity", lines=20,
                                               placeholder="Waiting for Real Text Input ...", show_label=True)

            # Set button functionality
            grTextBox_RealText.change(fn=func_genSyntheticText_Similarity, inputs=grTextBox_RealText, outputs=grTextBox_SyntheticText)
            gr.Button("Translate").click(fn=func_genSyntheticText_Similarity, inputs=grTextBox_RealText, outputs=grTextBox_SyntheticText)


        # 04. Launch Gradio MCP server
        grBlocks_SentenceSimilarity__MCP_Server.launch(mcp_server=True, share=True)

        
    finally:
        mcpClient_SyntheticText_Similarity.disconnect()