AllIllusion's picture
Update app.py
c7ea05f verified
#########################################################################
# 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-32B-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 Agent 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)."
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. Do not return anything other than the synthetic text and the direct output of the tool MCP-Server_TextSimilarity.
5. Return the synthetic note, and append the direct output of the tool MCP-Server_TextSimilarity at the end of the output.
6. Once you get the direct output of the tool MCP-Server_TextSimilarity, stop processing, no more steps, return immediately the final synthetic text and direct output of the tool MCP-Server_TextSimilarity.
7. Note, don't forget the append the direct output of the tool MCP-Server_TextSimilarity at the end of the output.
'''
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()